首页 > 解决方案 > AES 加密不接受特殊字符?

问题描述

当我在 Asp.Net Core 3.1 中使用 AES 解密 .xlsx 或 .csv 时,它不会返回被替换为“?”的特殊字符(á、é、í、ó、ú)。

这是我的代码。示例:José González 解密后返回 Jos? 冈萨雷斯

 public static void encrypt(IFormFile uploadFile)
    {
        
        ICryptoTransform transform = _crypt_provider.CreateEncryptor();

        //Extraer los datos del csv en un string
        string text;
        using (var reader = new StreamReader(uploadFile.OpenReadStream(), Encoding.UTF8))
        {
            // lectura del contenido del archivo
            text = reader.ReadToEnd();
        }


        byte[] encrypted_bytes = transform.TransformFinalBlock(ASCIIEncoding.ASCII.GetBytes(text) , 0, text.Length);

        string textoEncrypted = Convert.ToBase64String(encrypted_bytes);

        Debug.WriteLine($"El texto ingresado = {text} \nEl texto encriptado = {textoEncrypted}");

        File.WriteAllText(uploadFile.FileName, textoEncrypted);
      
    }


 public static string decrypt(IFormFile uploadFile)
    {
        
        string text;
        using (var reader = new StreamReader(uploadFile.OpenReadStream()))//, Encoding.UTF8
        {
            // lectura del contenido del archivo
            text = reader.ReadToEnd();
        }

        ICryptoTransform transform = _crypt_provider.CreateDecryptor();

        byte[] encrypted_bytes = Convert.FromBase64String(text);

        byte[] dencrypted_bytes = transform.TransformFinalBlock(encrypted_bytes, 0, encrypted_bytes.Length);

        string textoDesencriptado = ASCIIEncoding.ASCII.GetString(dencrypted_bytes);

        //File.WriteAllText(uploadFile.FileName, textoDesencriptado);

        Debug.WriteLine($"El texto ingresado = {text} \nEl texto desencriptado = {textoDesencriptado}");

        return textoDesencriptado;
    }

标签: c#asp.net-coreencryptionaes

解决方案


推荐阅读