rijndael 加密 - 仅解密部分字符串
只有部分字符串被解密,我认为这与我的编码有关。
发生的事情是这样的:
string s = "The brown fox jumped over the green frog";
string k = "urieurut";
string enc = EncryptString(s, k);
string dec = DecryptString(enc, k);
结果是这样的: The Brown Fox 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;
}
Only part of the string is getting decrypted, i think it has to do with my encoding.
Here is what happens:
string s = "The brown fox jumped over the green frog";
string k = "urieurut";
string enc = EncryptString(s, k);
string dec = DecryptString(enc, k);
The RESULT is this: The brown fox 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
给你:
您不能尝试将加密的字节串解释为 Unicode 字符串。将它们保留为字节。解密后的版本可以转换回字符串。
另请注意以下一次性物品的处理。如果您没有使用
using()
或Dispose()
正确释放某些资源,则可能会导致某些资源保留时间过长或泄漏。Here you go:
You can't attempt to interpret an encrypted bunch of bytes as a Unicode string. Keep them as bytes. The decrypted version can be converted back to string.
Also note the disposing of disposable objects below. You could wind up with some resources being held too long or leak if you don't release them properly with
using()
orDispose()
.我通过使用 base64 字符串进行加密解决了我的问题 - 我可能会考虑其他选项,但我只需要这些方法来处理少量数据,这里是最终代码:
I solved my issue by using base64 string for the encryption - i may look at other options but i only needed these methods for a small amount of data, here is the final code:
不确定你的具体代码块,但 Jeff Atwood 做了一个我以前使用过的漂亮的小库:
http://www.codeproject.com/KB/security/SimpleEncryption.aspx
值得一看,因为它大大简化了加密过程,我实际上不得不移植到 C#,因为没有'当我看到它时,没有可用的端口。不过,现在有一个 C# 端口(在评论部分)。
Not sure about your specific code chunk, but Jeff Atwood did a nice little library that I've used before:
http://www.codeproject.com/KB/security/SimpleEncryption.aspx
It's worth a look as it simplifies the process of encrypting things a lot, I actually had to port to C# as there wasn't a port available when I saw it. However there is now a C# port (in the comments section).