Note that there are some explanatory texts on larger screens.

plurals
  1. POC# 2-way Encryption Class - Rijndael possible initialization vector issue (jibberish output)
    primarykey
    data
    text
    <p>I am having problems with an encryption class that I made:</p> <pre><code>public static class Encryption { public static string EncryptToString(string TextToEncrypt, byte[] Key, byte[] IV = null) { return ByteArrToString(EncryptStringToBytes(StrToByteArray(TextToEncrypt), Key, IV)); } public static string EncryptToString(byte[] BytesToEncrypt, byte[] Key, byte[] IV = null) { return ByteArrToString(EncryptStringToBytes(BytesToEncrypt, Key, IV)); } public static byte[] EncryptToBytes(string TextToEncrypt, byte[] Key, byte[] IV = null) { return EncryptStringToBytes(StrToByteArray(TextToEncrypt), Key, IV); } public static byte[] EncryptToBytes(byte[] BytesToEncrypt, byte[] Key, byte[] IV = null) { return EncryptStringToBytes(BytesToEncrypt, Key, IV); } public static string DecryptToString(string EncryptedText, byte[] Key,byte[] IV=null) { return ByteArrToString(DecryptStringFromBytes(StrToByteArray(EncryptedText), Key, IV)); } public static string DecryptToString(byte[] EncryptedBytes, byte[] Key,byte[] IV=null) { return ByteArrToString(DecryptStringFromBytes(EncryptedBytes, Key, IV)); } public static byte[] DecryptToBytes(string EncryptedText, byte[] Key,byte[] IV=null) { return DecryptStringFromBytes(StrToByteArray(EncryptedText), Key, IV); } public static byte[] DecryptToBytes(byte[] EncryptedBytes, byte[] Key,byte[] IV=null) { return DecryptStringFromBytes(EncryptedBytes, Key, IV); } private static byte[] EncryptStringToBytes(byte[] TextToEncrypt, byte[] Key, byte[] IV=null) { Debug.WriteLine("Password: " + ByteArrToString(Key)); Debug.WriteLine("IV: " + ByteArrToString(IV)); 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(TextToEncrypt); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return encrypted; } private static byte[] DecryptStringFromBytes(byte[] EncryptedText, byte[] Key, byte[] IV) { Debug.WriteLine("Password: " + ByteArrToString(Key)); Debug.WriteLine("IV: " + ByteArrToString(IV)); byte[] fromEncrypt = new byte[EncryptedText.Length]; // Create a 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(EncryptedText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { //read stream into byte array csDecrypt.Read(fromEncrypt,0,fromEncrypt.Length); } } } return fromEncrypt; } public static byte[] StrToByteArray(string str) { if (str.Length == 0) throw new Exception("Invalid string value in StrToByteArray"); byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; } public static string ByteArrToString(byte[] bytes) { char[] chars = new char[bytes.Length / sizeof(char)]; System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); return new string(chars); } public static byte[] GetIV() { byte[] randomArray = new byte[16]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetBytes(randomArray); return randomArray; } } </code></pre> <p>I test it with the following:</p> <pre><code>byte[] iv = Encryption.GetIV(); byte[] password = Encryption.StrToByteArray("password"); string encrypted = Encryption.EncryptToString("Hello", password, iv); Debug.WriteLine("Result: " + Encryption.DecryptToString(encrypted, password, iv)); </code></pre> <p>This is the result I get in the debug window:</p> <blockquote> <p>Password: password </p> <p>IV: 䴞ㆫ튾꛽輔 </p> <p>Password: password </p> <p>IV: 䴞ㆫ튾꛽輔</p> <p>Result: 祓瑳浥䈮瑹孥]</p> </blockquote> <p>I don't get any errors; just a jibberish result. </p> <p>I don't know if it's a problem with the initialization vector, the stream, or something else that I'm missing. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload