Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i am decrypting using AES algorithm i am getting the The input data is not a complete block.
error.
Following is the code:
C#
base64StringToDecrypt="dHQ4eeglPQ";
public static string DecryptString(string base64StringToDecrypt, string passphrase)
{
    //Set up the encryption objects
    using (AesCryptoServiceProvider acsp = GetProvider(Encoding.Default.GetBytes(passphrase)))
    {
        acsp.Mode = CipherMode.CFB;
        base64StringToDecrypt = base64StringToDecrypt.Trim().Replace(" ", "+");
        if (base64StringToDecrypt.Length % 4 > 0)
            base64StringToDecrypt = base64StringToDecrypt.PadRight(base64StringToDecrypt.Length + 4 - base64StringToDecrypt.Length % 4, '=');
        byte[] RawBytes = (Encoding.Default.GetBytes(base64StringToDecrypt));

     
     //   byte[] RawBytes =  Convert.FromBase64String(base64StringToDecrypt);
        ICryptoTransform ictD = acsp.CreateDecryptor();
      

        // Return decrypted bytes as a string  

        //RawBytes now contains original byte array, still in Encrypted state

        //using (MemoryStream ms = new MemoryStream())
        //{
        //    using (var cs = new CryptoStream(ms, ictD, CryptoStreamMode.Write))
        //    {
        //        cs.Write(RawBytes,0,RawBytes.Length);
        //        //using (var swEncrypt = new StreamWriter(csEncrypt))
        //        //{

        //        //    //Write all data to the stream.
        //        //    swEncrypt.Write(plainText);
        //        //}
        //    }
        //}


       // Encoding encoding = Encoding.UTF8;
      //  MemoryStream msD = new MemoryStream(RawBytes, 0, RawBytes.Length);
       
        //Decrypt into stream
        MemoryStream msD = new MemoryStream();
        CryptoStream csD = new CryptoStream(msD, ictD, CryptoStreamMode.Write);
        csD.Write(RawBytes, 0, RawBytes.Length);
        csD.Close();
        Encoding encoding = Encoding.UTF8; 
        return encoding.GetString(msD.ToArray());
        //csD now contains original byte array, fully decrypted

        //return the content of msD as a regular string
       // return (new StreamReader(csD)).ReadToEnd();
    }
}

please help me out...what i am doing wrong
Posted
v2

Check the number of bytes found in the array RawBytes: it must me be a multiple of the number of bytes of your key (your variable passphrase). And the key ought to be 128 or 256 bits (16 or 32 bytes).
 
Share this answer
 
C#

INPUT DATA IS NOT A COMPLETE BLOCK........... using AES

CSS
protected void PerformDecryption(ICryptoTransform decryptor, string inputFilePath, string outputFilePath)
			{

				//Open both the input file for reading, and
				//  the output file for writing.
				FileStream reader = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read);
				FileStream writer = new FileStream(outputFilePath, FileMode.OpenOrCreate, FileAccess.Write);

				//Create an Encrypted Stream using the "encryptor" we created above
				System.Security.Cryptography.CryptoStream decryptedStream = new System.Security.Cryptography.CryptoStream(writer, decryptor, CryptoStreamMode.Write);

				byte[] bites = new byte[1024]; //Stores 1 KB
				Int32 bitesRead = 0; //Number of Bytes read
				Int64 totalBites = 0; //Total Number of Bytes read

				//Loop through the entire file reading 1 kb at a time
				while (!(totalBites >= reader.Length))
				{

					bitesRead = reader.Read(bites, 0, 1024); //Read the bytes from the input file.
					decryptedStream.Write(bites, 0, bitesRead); //Write the encrypted bytes to the output file.

					totalBites += bitesRead;

				}

				//Close and release streams:
				decryptedStream.Close();
				decryptedStream.Dispose();

				reader.Close();
				reader.Dispose();

				writer.Close();
				writer.Dispose();

			}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900