gpt4 book ai didi

c# - Rijndael 类加密然后解密数据引发 "Rijndael class to encrypt and then decrypt data"错误

转载 作者:行者123 更新时间:2023-11-30 22:27:36 27 4
gpt4 key购买 nike

我使用函数 EncryptStringToBytes 将纯文本加密为字节数组,并且最后将字节数组转换为字符串并返回。

我使用另一个函数将加密文本解密为纯文本。

我尝试用 RC2 对文本进行打乱,但它引发了这个错误:

Rijndael class to encrypt and then decrypt data

代码如下:

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace RC2CryptoServiceProvider_Examples
{
class MyMainClass
{
static string EncryptStringToBytes(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");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
byte[] encrypted;
// Create an Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;

// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.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 the encrypted bytes from the memory stream.
return System.Text.Encoding.UTF8.GetString(encrypted);**

}
static string DecryptStringFromBytes(string Codedtext, byte[] Key, byte[] IV)
{
byte[] cipherText = System.Text.Encoding.UTF8.GetBytes(Codedtext);
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");

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

// Create an Rijndael object
// with the specified key and IV.
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;

// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.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;

}

public static void Main()
{

try
{
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();

string original = "Here is some data to encrypt!";

// Create a new instance of the Rijndael
// class. This generates a new key and initialization
// vector (IV).
using (Rijndael myRijndael = Rijndael.Create())
{
// Encrypt the string to an array of bytes.
string encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
Console.WriteLine("Round Trip: {0}", roundtrip);
}

}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
Console.ReadLine();
}
}
}

最佳答案

这就是问题所在(或者至少是一个问题):

return System.Text.Encoding.UTF8.GetString(encrypted);

加密数据不是 UTF-8 文本。它是任意二进制数据。 不要将其视为编码文本。

如果您真的需要将任意二进制数据作为字符串传递,请使用 Base64:

return Convert.ToBase64String(encrypted);

然后在 DecryptStringFromBytes 中,您将使用:

byte[] cipherText = Convert.FromBase64String(codedText);

(参数名称更改为符合约定。)

关于c# - Rijndael 类加密然后解密数据引发 "Rijndael class to encrypt and then decrypt data"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11222355/

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