【问题标题】:When I decrypt with AES in ASP.Net Core I lose some data当我在 ASP.Net Core 中使用 AES 解密时,我丢失了一些数据
【发布时间】:2021-09-08 22:52:41
【问题描述】:

我的两个功能:

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(Encoding.UTF8.GetBytes(text) , 0, text.Length);

    string textoEncrypted = Convert.ToBase64String(encrypted_bytes);
    //string textoEncrypted = Encoding.UTF8.GetString(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.GetEncoding("iso-8859-1")
    {
        // lectura del contenido del archivo
        text = reader.ReadToEnd();
    }

    ICryptoTransform transform = _crypt_provider.CreateDecryptor();

    byte[] encrypted_bytes = Convert.FromBase64String(text);
    //byte[] encrypted_bytes = Encoding.UTF8.GetBytes(text);
    byte[] dencrypted_bytes = transform.TransformFinalBlock(encrypted_bytes, 0, encrypted_bytes.Length);
    string textoDesencriptado = Encoding.UTF8.GetString(dencrypted_bytes);
    //File.WriteAllText(uploadFile.FileName, textoDesencriptado);
    Debug.WriteLine($"El texto ingresado = {text} \nEl texto desencriptado = {textoDesencriptado}");
    return textoDesencriptado;
}

这是我的 .csv 输入:

50001,José,,Peréz,Gonzáles,1998-07-06
50002,Miguel,,Hermandez,Hermandez,1998-07-06
50003,Raúl,,Ramirez,Gutierrez,1998-07-06
50004,Laura2,Araceli,Ordoñez,Alcazar,1998-07-06
50005,Yazmin2,,Herrera,García,1998-07-06
50007,Prb2, Prb,Prb3,Herreras,1998-07-06

还有输出:

50001,José,,Peréz,Gonzáles,1998-07-06
50002,Miguel,,Hermandez,Hermandez,1998-07-06
50003,Raúl,,Ramirez,Gutierrez,1998-07-06
50004,Laura2,Araceli,Ordoñez,Alcazar,1998-07-06
50005,Yazmin2,,Herrera,García,1998-07-06
50007,Prb2, Prb,Prb3,Herreras,1998

50007 中缺少 -07-06。

我使用 UTF-8 是因为我需要加密特殊字符。

【问题讨论】:

    标签: asp.net aes asp.net-core-3.1


    【解决方案1】:

    一般来说,如果解密失败,您将什么也读不到。 所以错误必须在读取/加密上。 将文本上的这一行更改为加密 - 不给出 text.Length,而是给出数组长度。

    byte[] toEncryptArray = ASCIIEncoding.BigEndianUnicode.GetBytes(toEncrypt);
    
    
    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多