我有使用C#编写的代码,想尝试转换为php代码,但在php版本中总是得到不同的结果
C#
private static string encryptDecrypt(string input)
{
char[] key = { 'X' }; //Any chars will work, in an array of any size
char[] output = new char[input.Length];
for (int i = 0; i < input.Length; i++)
{
output[i] = (char)(input[i] ^ key[i % key.Length]);
}
return new string(output);
}
string vb64 = "DAAdAAAADAAVAAsAHwA=";
string decrypted = encryptDecrypt(Encoding.Unicode.GetString(Convert.FromBase64String(vb64)));
Console.WriteLine("Decrypted:" + decrypted);
输出已解密:TEXTMSG
PHP
function encryptDecrypt($input) {
$key = ['X'];
$output = '';
for ($i = 0; $i < strlen($input); $i++) {
$output .= $input[$i] ^ $key[$i % count($key)];
}
return $output;
}
$vb64 = "DAAdAAAADAAVAAsAHwA=";
$decrypted = encryptDecrypt(base64_decode($vb64));
echo "decrypted ".$decrypted.PHP_EOL;
解密的TXEXXXTXMXSXGX
编辑:需要添加mb_convert_encoding以设置为UTF-16 cryptoDecrypt(mb_convert_encoding(base64_decode($ vb64),“”,“ UTF-16LE”))