首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我在ASP.Net核心中用AES解密时,我丢失了一些数据

当我在ASP.Net核心中用AES解密时,我丢失了一些数据
EN

Stack Overflow用户
提问于 2021-09-08 22:52:41
回答 1查看 125关注 0票数 0

我的两个函数:

代码语言:javascript
复制
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输入:

代码语言:javascript
复制
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

和输出:

代码语言:javascript
复制
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在50007.

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

EN

回答 1

Stack Overflow用户

发布于 2021-09-09 01:54:11

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

代码语言:javascript
复制
byte[] toEncryptArray = ASCIIEncoding.BigEndianUnicode.GetBytes(toEncrypt);


byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69110469

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档