Click here to Skip to main content
14,488,136 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi guys, i was wrote a code to encrypt and decrypt data, but i keep getting this frustrating error
Length of the data to decrypt is invalid.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;

namespace inChargeAES
{
    /// <summary>
    /// Summary description for inChargeCrypto
    /// </summary>
    public class inChargeCrypto : IinChargeAES
    {
        public inChargeCrypto()
        {
        }

        public String inChargeEncrypt(String plaintext, byte[] encryptionKey, byte[] initializationVector)
        {
            if (plaintext == null || plaintext.Length <= 0)
            {
                throw new ArgumentNullException("plaintext");
            }
            if(encryptionKey == null || encryptionKey.Length <= 0){
                throw new ArgumentNullException("encryptionKey");
            }
            if(initializationVector == null || initializationVector.Length <= 0){
                throw new ArgumentNullException("initializationVector");
            }
            byte[] encryptedText;
            using(RijndaelManaged rjManage = new RijndaelManaged())
            {
                rjManage.Key = encryptionKey;
                rjManage.IV = initializationVector;
                rjManage.Mode = CipherMode.CBC;
                //rjManage.Padding = PaddingMode.None;

                ICryptoTransform iTransformer = rjManage.CreateEncryptor(rjManage.Key, rjManage.IV);
                using(MemoryStream memStream = new MemoryStream())
                {
                    using(CryptoStream cEncryptStream = new CryptoStream(memStream, iTransformer, CryptoStreamMode.Write))
                    {
                        using(StreamWriter encryptStreamWriter = new StreamWriter(cEncryptStream))
                        {
                            encryptStreamWriter.Write(plaintext);
                        }
                        encryptedText = memStream.ToArray();
                    }
                }
            }
            return Convert.ToBase64String(encryptedText);
        }

        public String inChargeDecrypt(byte[] cipher, byte[] encryptionKey, byte[] initializationVector)
        {
            if (cipher == null || cipher.Length <= 0){
                throw new ArgumentNullException("cipher");
            }
            if (encryptionKey == null || encryptionKey.Length <= 0){
                throw new ArgumentNullException("encryptionKey");
            }
            if (initializationVector == null || initializationVector.Length <= 0){
                throw new ArgumentNullException("initializationVector");
            }

            String decryptedText = null;
            using (RijndaelManaged rijManage = new RijndaelManaged())
            {
                rijManage.Key = encryptionKey;
                rijManage.IV = initializationVector;
                rijManage.Mode = CipherMode.CBC;
                rijManage.Padding = PaddingMode.None;

                ICryptoTransform iTranformation = rijManage.CreateDecryptor(rijManage.Key, rijManage.IV);
                using(MemoryStream memStream = new MemoryStream(cipher))
                {
                    using(CryptoStream cDecryptorStream = new CryptoStream(memStream, iTranformation, CryptoStreamMode.Read))
                    {
                        using (StreamReader decryptReader = new StreamReader(cDecryptorStream))
                        {
                            decryptedText = decryptReader.ReadToEnd(); //Exception Is Thrown
                        }
                        //memStream.Read(cipher, 0, cipher.Length);
                    }
                }

            }
            return decryptedText;
        }

    }
}
Posted
Updated 31-Oct-13 4:37am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Just a hunch, but move this line:

encryptedText = memStream.ToArray();

below the brace following it. Streams often need flushing and it might be that you are reading from the memory stream before its fully populated (by the end of the using block disposing, where it force a flush).

Does that make any sense? Doesn't mean much to me reading it back.
   
Comments
ahmedfaruk88 31-Oct-13 18:01pm
   
Ok. Let me try it out?
ahmedfaruk88 31-Oct-13 18:11pm
   
Thanks alot. but after trying that i was able to encrypt and decrypt to the decrypted text was not the initial plain text
Rob Philpott 1-Nov-13 5:22am
   
ah well, good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100