c# - 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;
}
解决方案
推荐阅读
- visual-studio-code - 通过在集成终端中执行的脚本关闭 vscode 窗口
- google-sheets - 如何反转 Google 表格中的列顺序?
- java - Java:输入两个错误值后程序崩溃
- ruby-on-rails - ReCaptcha 对表单很好,但它也无需确认即可注册
- c++ - SDL2 2D 纹理分配/池化
- javascript - 链接firebase函数的问题
- javascript - 无法从委托函数访问类属性
- python - 使用 CNN+LSTM 模型和 TimeDistributed 层包装器的 Keras 时间序列预测
- mysql - 如何在 typeorm 中建立 OneToMany 关系?
- kendo-ui - 从选定的计划项目中获取选定的标题和描述信息