gpt4 book ai didi

c# - AES(rijndael)解密

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:42 25 4
gpt4 key购买 nike

我有一个编码和解码字符串的安全类,但是当我尝试解码时 - 出现了问题。

这是我的安全类:

class Security
{
public static String encrypt(String imput, String key)
{
String cipherText;
var rijndael = new RijndaelManaged()
{
Key = Encoding.Unicode.GetBytes(key),
Mode = CipherMode.ECB,
BlockSize = 128,
Padding = PaddingMode.Zeros,
};
ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, null);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (var streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(imput);
streamWriter.Flush();
}
cipherText = Convert.ToBase64String(memoryStream.ToArray());
}
}
return cipherText;
}

public static String decrypt(String imput, String key)
{
byte[] data = Convert.FromBase64String(imput);
String decrypted;

using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Encoding.UTF8.GetBytes(key);
rijAlg.Mode = CipherMode.ECB;
rijAlg.BlockSize = 128;
rijAlg.Padding = PaddingMode.Zeros;

ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, null);
using (MemoryStream msDecrypt = new MemoryStream(data))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
decrypted = srDecrypt.ReadToEnd();
}
}
}
}
return decrypted;
}
}

在program.cs中:

String A = Security.encrypt("text", "1234567891234567");
A = Security.decrypt(A, "1234567891234567");

最后它返回类似的东西:��%����;\0��\a����f6,但我需要原始字符串。我哪里做错了?

最佳答案

在两种方法中使用相同的编码,Encoding.Unicode 或 Encoding.UTF8

关于c# - AES(rijndael)解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27943335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com