Rijndael加密 - 只有字符串的一部分被解密 [英] rijndael encryption - only part of the string is decrypted
本文介绍了Rijndael加密 - 只有字符串的一部分被解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只有字符串的一部分是越来越解密,我认为这与我的编码做
下面是发生了什么:
字符串s =棕色狐狸跳过了绿蛙;
线K =urieurut;
串ENC = EncryptString(S,K);
串DEC = DecryptString(ENC,K);
结果是这样的:棕色狐狸juϼ㴘裴혽Ή⪻ㆉ - [Rth≸摹⟤een蛙
公共静态字符串EncryptString(字符串stringToEncrypt,串的encryptionKey)
{
字符串加密=的String.Empty;
UnicodeEncoding UE =新UnicodeEncoding();
的byte []键= UE.GetBytes(的encryptionKey);
RijndaelManaged的RMCrypto =新RijndaelManaged的();
的MemoryStream毫秒=新的MemoryStream();
CryptoStream的CS =新的CryptoStream(MS,RMCrypto.CreateEncryptor(键,键),CryptoStreamMode.Write);
字节[] = encryptedString UE.GetBytes(stringToEncrypt);
cs.Write(encryptedString,0,encryptedString.Length);
cs.FlushFinalBlock();
cs.Close();
加密= UE.GetString(ms.ToArray());
返回加密;
}
公共静态字符串DecryptString(字符串stringToDecrypt,串的encryptionKey)
{解密=的String.Empty
串;
UnicodeEncoding UE =新UnicodeEncoding();
的byte []键= UE.GetBytes(的encryptionKey);
字节[]数据= UE.GetBytes(stringToDecrypt);
RijndaelManaged的RMCrypto =新RijndaelManaged的();
的MemoryStream毫秒=新的MemoryStream();
CryptoStream的CS =新的CryptoStream(MS,RMCrypto.CreateDecryptor(键,键),CryptoStreamMode.Write);
cs.Write(数据,0,data.Length);
cs.FlushFinalBlock();
cs.Close();
解密= UE.GetString(ms.ToArray());
返回解密;
}
解决方案
我解决我的问题,通过使用对于加密的base64字符串 - 我可以看看其他的选择,但我只需要这些方法对数据量小,这里是最终的代码:
公共静态字符串EncryptString(字符串stringToEncrypt,串的encryptionKey)
{加密=的String.Empty
串;
的byte []键= Encoding.Unicode.GetBytes(的encryptionKey);
RijndaelManaged的RMCrypto =新RijndaelManaged的();
RMCrypto.Padding = PaddingMode.PKCS7;
的MemoryStream毫秒=新的MemoryStream();
CryptoStream的CS =新的CryptoStream(MS,RMCrypto.CreateEncryptor(键,键),CryptoStreamMode.Write);
字节[] = encryptedString Encoding.ASCII.GetBytes(stringToEncrypt);
cs.Write(encryptedString,0,encryptedString.Length);
cs.FlushFinalBlock();
cs.Close();
//加密= Encoding.ASCII.GetString(ms.ToArray());
返回Convert.ToBase64String(ms.ToArray());
}
公共静态字符串DecryptString(字符串stringToDecrypt,串的encryptionKey)
{解密=的String.Empty
串;
的byte []键= Encoding.Unicode.GetBytes(的encryptionKey);
字节[]数据= Convert.FromBase64String(stringToDecrypt);
RijndaelManaged的RMCrypto =新RijndaelManaged的();
RMCrypto.Padding = PaddingMode.PKCS7;
的MemoryStream毫秒=新的MemoryStream();
CryptoStream的CS =新的CryptoStream(MS,RMCrypto.CreateDecryptor(键,键),CryptoStreamMode.Write);
cs.Write(数据,0,data.Length);
cs.FlushFinalBlock();
cs.Close();
解密= Encoding.ASCII.GetString(ms.ToArray());
返回解密;
}
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;
}
解决方案
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:
public static string EncryptString(string stringToEncrypt, string encryptionKey)
{
string encrypted = String.Empty;
byte[] key = Encoding.Unicode.GetBytes(encryptionKey);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Padding = PaddingMode.PKCS7;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
byte[] encryptedString = Encoding.ASCII.GetBytes(stringToEncrypt);
cs.Write(encryptedString, 0, encryptedString.Length);
cs.FlushFinalBlock();
cs.Close();
//encrypted = Encoding.ASCII.GetString(ms.ToArray());
return Convert.ToBase64String(ms.ToArray());
}
public static string DecryptString(string stringToDecrypt, string encryptionKey)
{
string decrypted = String.Empty;
byte[] key = Encoding.Unicode.GetBytes(encryptionKey);
byte[] data = Convert.FromBase64String(stringToDecrypt);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Padding = PaddingMode.PKCS7;
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 = Encoding.ASCII.GetString(ms.ToArray());
return decrypted;
}
这篇关于Rijndael加密 - 只有字符串的一部分被解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文