我的两个函数:
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失踪-07-06在50007.
我使用UTF-8是因为我需要加密特殊字符。
发布于 2021-09-09 01:54:11
一般来说,如果解密失败,你就什么也看不见。因此,错误必须在读取/加密。将文本上的这一行更改为加密--不是给出text.Length,而是给出数组长度。
byte[] toEncryptArray = ASCIIEncoding.BigEndianUnicode.GetBytes(toEncrypt);
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);https://stackoverflow.com/questions/69110469
复制相似问题