gpt4 book ai didi

c# - 为什么 RijndaelManaged 加密后返回垃圾数据?

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

我已经从 http://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptostream.aspx 中获取了解密代码并修改如下。我有一个加密的例子,它在解码时工作得很好。但是当使用加密函数时,它返回带有奇怪符号的垃圾字符串。下面是加密/解密函数。

加密字符串“hey”的示例:“???U?b???z?Y???”
再次解码时: "ûc{ÁpÅ`ñ""Â"

我正在使用这段代码将字节数组转换为字符串:

private string ByteArrayToString(byte[] input)
{
ASCIIEncoding dec = new ASCIIEncoding();
return dec.GetString(input);
}

这里是加密/解密函数。解密功能工作正常。

private string DecryptStringFromBytesAes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");

// Declare the string used to hold
// the decrypted text.
string plaintext = null;

// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged aesAlg = new RijndaelManaged())
{
aesAlg.Key = Key;
aesAlg.Padding = PaddingMode.Zeros;
aesAlg.Mode = CipherMode.ECB;

// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{

// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}

}

return plaintext;

}




private byte[] EncryptStringToBytesAes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged aesAlg = new RijndaelManaged())
{
aesAlg.Key = Key;
aesAlg.Padding = PaddingMode.Zeros;
aesAlg.Mode = CipherMode.ECB;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{

//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;

}

最佳答案

您观察到的是将任意字节(在 0-255 范围内)映射到字符的问题。有意义的字符仅在 32-255 范围内,甚至仅在 32-127 (ASCII) 范围内。低于 32 的值是所谓的不可打印字符,高于 127 的值取决于您使用的字符编码。这就是加密文本看起来像垃圾的原因。因此,Mast 密码系统将字节转换为合理的 ASCII 范围。一种这样的算法是 BASE64。因此,通过 BASE64 处理加密的字节会得到所有可打印的字符,并且可以毫无问题地通过电子邮件发送。在解密之前,您必须撤消 BASE64 编码。

另一种使加密结果看起来更好的方法是显示它的十六进制表示。例如,如果您的字节值为 15,则打印 0F。您可以使用它来表示您的十六进制字节数组:

private string ByteArrayToHexString(byte[] data)
{
return String.Concat(data.Select(b => b.ToString("x2")));
}

关于c# - 为什么 RijndaelManaged 加密后返回垃圾数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11690783/

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