【发布时间】: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