Rijandeal 和特殊字符

Rijandeal and special characters

我正在使用 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。

encrypting/decrypting 的代码按预期工作,控制台输出是问题所在。

设置 outputEncoding 以正确编码显示字符

Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine(dec);
Console.ReadKey();