我有简单的加密函数,它接受字符串,将其转换为字节,对其进行异或并应用 base64。
爪哇:
String key = "1234";
public String encrypt(String plainText) throws Exception {
byte [] input = plainText.getBytes("UTF8");
byte [] output = new byte[input.length];
for(int i=0; i<input.length; i++)
output[i] = ((byte)(input[i] ^ key.charAt(i % key.length())));
String utf8 = new String(output);
return Utils.encode(utf8);
}
然后我将它保存到一个文件中,并使用以下解密方法在 C# 中的另一个应用程序中打开它:
C#:
string key="1234";
public string Decrypt(string CipherText)
{
var decoded = System.Convert.FromBase64String(CipherText);
var dexored = xor(decoded, key);
return Encoding.UTF8.GetString(dexored);
}
byte[] xor(byte[] text, string key)
{
byte[] res = new byte[text.Length];
for (int c = 0; c < text.Length; c++)
{
res[c] = (byte)((uint)text[c] ^ (uint)key[c % key.Length]);
}
return res;
}
问题是像ěščřžýáí 这样的重音字符无法解码。
您是否知道如何确定问题来自哪个部分或如何找出问题?在我看来,它与 UTF-8 有关。
我不需要更好的加密建议。我已经在使用 AES,但由于性能问题,我想切换到 xored base64。
谢谢你。