gpt4 book ai didi

c# - rijndael 加密 - 只有部分字符串被解密

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

只有部分字符串被解密,我认为这与我的编码有关。

这是发生了什么:

        string s = "The brown fox jumped over the green frog";
string k = "urieurut";
string enc = EncryptString(s, k);
string dec = DecryptString(enc, k);

结果是这样的:棕色狐狸juϼ㴘裴혽テ⪻ㆉr th≸ g⟤een frog

public static string EncryptString(string stringToEncrypt, string encryptionKey)
{
string encrypted = String.Empty;

UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(encryptionKey);

RijndaelManaged RMCrypto = new RijndaelManaged();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);

byte[] encryptedString = UE.GetBytes(stringToEncrypt);
cs.Write(encryptedString, 0, encryptedString.Length);
cs.FlushFinalBlock();
cs.Close();

encrypted = UE.GetString(ms.ToArray());
return encrypted;
}

public static string DecryptString(string stringToDecrypt, string encryptionKey)
{
string decrypted = String.Empty;

UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(encryptionKey);
byte[] data = UE.GetBytes(stringToDecrypt);

RijndaelManaged RMCrypto = new RijndaelManaged();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);
cs.FlushFinalBlock();
cs.Close();

decrypted = UE.GetString(ms.ToArray());

return decrypted;
}

最佳答案

给你:

    string s = "The brown fox jumped over the green frog";
string k = "urieurut";
byte[] enc = EncryptString(s, k);
string dec = DecryptString(enc, k);

您不能尝试将一串加密的字节解释为 Unicode 字符串。将它们保存为字节。解密后的版本可以转换回字符串。

另请注意以下一次性元素的处理。如果您没有使用 using()Dispose() 正确释放它们,您可能会导致某些资源持有时间过长或泄漏。

public static byte[] EncryptString(string stringToEncrypt, string encryptionKey)
{
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(encryptionKey);

using (RijndaelManaged RMCrypto = new RijndaelManaged())
using (MemoryStream ms = new MemoryStream())
using (ICryptoTransform encryptor = RMCrypto.CreateEncryptor(key, key))
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
byte[] encryptedString = UE.GetBytes(stringToEncrypt);
cs.Write(encryptedString, 0, encryptedString.Length);
cs.FlushFinalBlock();
return ms.ToArray();
}
}

public static string DecryptString(byte[] stringToDecrypt, string encryptionKey)
{
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(encryptionKey);

using (RijndaelManaged RMCrypto = new RijndaelManaged())
using (MemoryStream ms = new MemoryStream())
using (ICryptoTransform decryptor = RMCrypto.CreateDecryptor(key, key))
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
{
cs.Write(stringToDecrypt, 0, stringToDecrypt.Length);
cs.FlushFinalBlock();
return UE.GetString(ms.ToArray());
}
}

关于c# - rijndael 加密 - 只有部分字符串被解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2975472/

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