Decryption Exception - decryption data length invalid

I am working in a C # application. We have common methods for storing data in a file. These methods encrypt data and store them in the file system. when we need data, the ReadData method decrypts the data and returns me plain text.

This code works fine in normal cases, if the text size is small. but for the example below, the decryption code will throw an exception - the length of the data to decrypt is invalid.

The exception occurs in the line

// close the CryptoStream x_cryptostream.Close(); 

I tried different ways, but no luck. Can help any PLS.

Why do I encrypt already encrypted data - I'm just trying to be stored in a file, using a common method of huge applications. General methods storedata(key,data) nad readdata(key) make the encryption / decryption, which I can not avoid.

  public static byte[] Decrypt(byte[] ciphertext, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to decrypt data ICryptoTransform x_decryptor = x_alg.CreateDecryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and the // ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_decryptor, CryptoStreamMode.Write); // write the ciphertext out to the cryptostream x_cryptostream.Write(ciphertext, 0, ciphertext.Length); // close the CryptoStream x_cryptostream.Close(); // get the plaintext from the MemoryStream byte[] x_plaintext = x_memory_stream.ToArray(); );  public static byte[] Decrypt(byte[] ciphertext, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to decrypt data ICryptoTransform x_decryptor = x_alg.CreateDecryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and the // ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_decryptor, CryptoStreamMode.Write); // write the ciphertext out to the cryptostream x_cryptostream.Write(ciphertext, 0, ciphertext.Length); // close the CryptoStream x_cryptostream.Close(); // get the plaintext from the MemoryStream byte[] x_plaintext = x_memory_stream.ToArray(); );  public static byte[] Decrypt(byte[] ciphertext, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to decrypt data ICryptoTransform x_decryptor = x_alg.CreateDecryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and the // ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_decryptor, CryptoStreamMode.Write); // write the ciphertext out to the cryptostream x_cryptostream.Write(ciphertext, 0, ciphertext.Length); // close the CryptoStream x_cryptostream.Close(); // get the plaintext from the MemoryStream byte[] x_plaintext = x_memory_stream.ToArray(); to decrypt data  public static byte[] Decrypt(byte[] ciphertext, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to decrypt data ICryptoTransform x_decryptor = x_alg.CreateDecryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and the // ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_decryptor, CryptoStreamMode.Write); // write the ciphertext out to the cryptostream x_cryptostream.Write(ciphertext, 0, ciphertext.Length); // close the CryptoStream x_cryptostream.Close(); // get the plaintext from the MemoryStream byte[] x_plaintext = x_memory_stream.ToArray(); 

The following is the encryption code.

  public static byte[] Encrypt(string strplain, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); byte[] plaintext = Encoding.Default.GetBytes(strplain); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to encrypt data ICryptoTransform x_encryptor = x_alg.CreateEncryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and // the ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_encryptor, CryptoStreamMode.Write); // write the plaintext out to the cryptostream x_cryptostream.Write(plaintext, 0, plaintext.Length); // close the CryptoStream x_cryptostream.Close(); // get the ciphertext from the MemoryStream byte[] x_ciphertext = x_memory_stream.ToArray(); // close memory stream x_memory_stream.Close(); // convert from array to string string cipher_Tx = Encoding.Default.GetString(x_ciphertext, 0, x_ciphertext.Length); x_encryptor.Dispose(); x_alg.Clear(); byte[] cipher = Encoding.Default.GetBytes(cipher_Tx); return cipher; } );  public static byte[] Encrypt(string strplain, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); byte[] plaintext = Encoding.Default.GetBytes(strplain); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to encrypt data ICryptoTransform x_encryptor = x_alg.CreateEncryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and // the ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_encryptor, CryptoStreamMode.Write); // write the plaintext out to the cryptostream x_cryptostream.Write(plaintext, 0, plaintext.Length); // close the CryptoStream x_cryptostream.Close(); // get the ciphertext from the MemoryStream byte[] x_ciphertext = x_memory_stream.ToArray(); // close memory stream x_memory_stream.Close(); // convert from array to string string cipher_Tx = Encoding.Default.GetString(x_ciphertext, 0, x_ciphertext.Length); x_encryptor.Dispose(); x_alg.Clear(); byte[] cipher = Encoding.Default.GetBytes(cipher_Tx); return cipher; } );  public static byte[] Encrypt(string strplain, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); byte[] plaintext = Encoding.Default.GetBytes(strplain); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to encrypt data ICryptoTransform x_encryptor = x_alg.CreateEncryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and // the ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_encryptor, CryptoStreamMode.Write); // write the plaintext out to the cryptostream x_cryptostream.Write(plaintext, 0, plaintext.Length); // close the CryptoStream x_cryptostream.Close(); // get the ciphertext from the MemoryStream byte[] x_ciphertext = x_memory_stream.ToArray(); // close memory stream x_memory_stream.Close(); // convert from array to string string cipher_Tx = Encoding.Default.GetString(x_ciphertext, 0, x_ciphertext.Length); x_encryptor.Dispose(); x_alg.Clear(); byte[] cipher = Encoding.Default.GetBytes(cipher_Tx); return cipher; } );  public static byte[] Encrypt(string strplain, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); byte[] plaintext = Encoding.Default.GetBytes(strplain); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to encrypt data ICryptoTransform x_encryptor = x_alg.CreateEncryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and // the ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_encryptor, CryptoStreamMode.Write); // write the plaintext out to the cryptostream x_cryptostream.Write(plaintext, 0, plaintext.Length); // close the CryptoStream x_cryptostream.Close(); // get the ciphertext from the MemoryStream byte[] x_ciphertext = x_memory_stream.ToArray(); // close memory stream x_memory_stream.Close(); // convert from array to string string cipher_Tx = Encoding.Default.GetString(x_ciphertext, 0, x_ciphertext.Length); x_encryptor.Dispose(); x_alg.Clear(); byte[] cipher = Encoding.Default.GetBytes(cipher_Tx); return cipher; } MemoryStream and  public static byte[] Encrypt(string strplain, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); byte[] plaintext = Encoding.Default.GetBytes(strplain); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to encrypt data ICryptoTransform x_encryptor = x_alg.CreateEncryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and // the ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_encryptor, CryptoStreamMode.Write); // write the plaintext out to the cryptostream x_cryptostream.Write(plaintext, 0, plaintext.Length); // close the CryptoStream x_cryptostream.Close(); // get the ciphertext from the MemoryStream byte[] x_ciphertext = x_memory_stream.ToArray(); // close memory stream x_memory_stream.Close(); // convert from array to string string cipher_Tx = Encoding.Default.GetString(x_ciphertext, 0, x_ciphertext.Length); x_encryptor.Dispose(); x_alg.Clear(); byte[] cipher = Encoding.Default.GetBytes(cipher_Tx); return cipher; } ;  public static byte[] Encrypt(string strplain, string Key, string IV) { byte[] k = Encoding.Default.GetBytes(Key); byte[] iv = Encoding.Default.GetBytes(IV); byte[] plaintext = Encoding.Default.GetBytes(strplain); // create the encryption algorithm SymmetricAlgorithm x_alg = SymmetricAlgorithm.Create("Rijndael"); x_alg.Padding = PaddingMode.PKCS7; // create an ICryptoTransform that can be used to encrypt data ICryptoTransform x_encryptor = x_alg.CreateEncryptor(k, iv); // create the memory stream MemoryStream x_memory_stream = new MemoryStream(); // create the CryptoStream that ties together the MemoryStream and // the ICryptostream CryptoStream x_cryptostream = new CryptoStream(x_memory_stream, x_encryptor, CryptoStreamMode.Write); // write the plaintext out to the cryptostream x_cryptostream.Write(plaintext, 0, plaintext.Length); // close the CryptoStream x_cryptostream.Close(); // get the ciphertext from the MemoryStream byte[] x_ciphertext = x_memory_stream.ToArray(); // close memory stream x_memory_stream.Close(); // convert from array to string string cipher_Tx = Encoding.Default.GetString(x_ciphertext, 0, x_ciphertext.Length); x_encryptor.Dispose(); x_alg.Clear(); byte[] cipher = Encoding.Default.GetBytes(cipher_Tx); return cipher; } 
+6
source share
2 answers

Your problem string cipher_Tx = Encoding.Default.GetString(x_ciphertext, 0, x_ciphertext.Length); .

x_ciphertext is not a valid byte representation of text, it contains a lot of representable characters, and when you perform the conversion byte[] in string , you lose information. The correct way to do it - use a string format that is used to represent binary data using something like Convert.ToBase64String(byte[]) and Convert.FromBase64String(string) .

 string cipher_Tx = Convert.ToBase64String(x_ciphertext) x_encryptor.Dispose(); x_alg.Clear(); byte[] cipher = Convert.FromBase64String(cipher_Tx) 

As they say, in your code, there are many other "strange" things, for example, you are not using the instructions using , and you really need. In addition, a complete transformation in a row and did not need to return, simply return x_ciphertext . There may be other problems with the code (for example, where did the strings for the Key and IV ), and many other best practices (for example, you need to generate a random IV and burn it to a conclusion, and the key is to be generated using the derivation of the key features that are not related to text user), but I stopped checking after it found the string conversion problem.

+4
source

Your code above works as long as the key and iv, used to decrypt the corresponding key and iv, which are used for the encryption. Try the following:

 byte[] test = new byte[1000000]; for (int i = 0; i < 256; i++) { test[i] = (byte)i; } var ciphertext = Encrypt(Encoding.Default.GetString(test), "0000000000000000", "0000000000000000"); byte[] check = Decrypt(ciphertext, "0000000000000000", "0000000000000000"); for (int i = 0; i < 256; i++) { Debug.Assert(check[i] == (byte)i, "round trip"); } , " byte[] test = new byte[1000000]; for (int i = 0; i < 256; i++) { test[i] = (byte)i; } var ciphertext = Encrypt(Encoding.Default.GetString(test), "0000000000000000", "0000000000000000"); byte[] check = Decrypt(ciphertext, "0000000000000000", "0000000000000000"); for (int i = 0; i < 256; i++) { Debug.Assert(check[i] == (byte)i, "round trip"); } byte) i, "round trip"); byte[] test = new byte[1000000]; for (int i = 0; i < 256; i++) { test[i] = (byte)i; } var ciphertext = Encrypt(Encoding.Default.GetString(test), "0000000000000000", "0000000000000000"); byte[] check = Decrypt(ciphertext, "0000000000000000", "0000000000000000"); for (int i = 0; i < 256; i++) { Debug.Assert(check[i] == (byte)i, "round trip"); } 

As you can see, one million bytes are encrypted and decrypted only with your code, so I don’t think it has anything to do with the size of the data.

However, to change the value IV as follows:

 byte[] check = Decrypt(ciphertext, "0000000000000000", "000000000000000X"); // note X 

and Debug.Assert will light up - decryption will not match. However, x_cryptostream.Close () succeeds.

Then try changing the key as follows:

 byte[] check = Decrypt(ciphertext, "000000000000000X", "0000000000000000"); // note X 

Now x_cryptostream.Close () fails CryptographicException, perhaps, "Invalid filling and can not be removed."

key corruption will lead to a decryption failure, and x_cryptostream.Close error () will fail.

I think the problem in the preservation and subsequent reduction of the key bytes.

BTW: We hope you use the full binary range of the key and do not base it only on ASCII characters, otherwise you really do not have a strong key.

0
source

All Articles