Rijndael encryption - only part of the string is decrypted

Only part of the string gets decryption, I think it is related to my encoding.

Here's 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); 

RESULT: this is a brown fox that 㴘 㴘 čŖ´ í˜Ŋ ΉâĒģ ㆉ ㆉ ㆉ → → →.

 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; } 
+2
c # encryption rijndael
source share
3 answers

I solved the problem using the base64 string for encryption - I can look at other parameters, but I need these methods only 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; } 
+1
source share

Here you go:

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

You cannot try to interpret the encrypted bunch of bytes as a Unicode string. Store them as bytes. The decrypted version can be converted back to a string.

Also note the placement of disposable items below. You may encounter excessive resources or leaks if you do not release them using using() or 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()); } } 
+3
source share

Not sure about your specific code snippet, but Jeff Atwood made a small small library that I used before:

http://www.codeproject.com/KB/security/SimpleEncryption.aspx

It is worth seeing how this simplifies the process of encrypting things, I actually had to port to C #, because I did not see the port. However, there is now a C # port (in the comments section).

0
source share

All Articles