我正在使用Rijandeal进行编码。而且我注意到某些特殊字符的管理不正确。
代码如下:
static void Main(string[] args)
{
string enc = RijanENC("šđčćž");
string dec = RijanDEC(enc);
Console.WriteLine(dec);
Console.ReadKey();
}
private static string RijanENC(string texto_puro)
{
byte[] key = System.Text.Encoding.Default.GetBytes("123abc12");
byte[] iv = System.Text.Encoding.Default.GetBytes("0123456789abcdef");
byte[] stringToEncrypt = System.Text.Encoding.UTF32.GetBytes(texto_puro);
Rijndael rjnAlg = Rijndael.Create();
System.IO.MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rjnAlg.CreateEncryptor(key, iv), CryptoStreamMode.Write);
cs.Write(stringToEncrypt, 0, stringToEncrypt.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
private static string RijanDEC(string texto_encriptado)
{
byte[] key = System.Text.Encoding.Default.GetBytes("123abc12");
byte[] iv = System.Text.Encoding.Default.GetBytes("0123456789abcdef");
byte[] stringToDecrypt = new byte[texto_encriptado.Length];
Rijndael rjnAlg = Rijndael.Create();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, rjnAlg.CreateDecryptor(key, iv), CryptoStreamMode.Write);
stringToDecrypt = Convert.FromBase64String(texto_encriptado);
cs.Write(stringToDecrypt, 0, stringToDecrypt.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF32;
return encoding.GetString(ms.ToArray());
}
所以我要输入的文字。包含一些变音符号šđčćž。解码后,即使使用UTF32,我也会得到sdccz。
答案 0 :(得分:1)
用于加密/解密的代码按预期工作,控制台输出是问题。
设置outputEncoding以正确的编码显示字符
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine(dec);
Console.ReadKey();