Error in Decryption --> Invalid padding and Data size is not block size multiple RRS feed

  • Question

  • Hi All,

    Could you please help me on this error anybody its very urgnet issue in production.

    i have an issue while doing the decription with ingrainkey value got below 2 errors.
    1. Error in Decryption --> Invalid padding.
    2. Error in Decryption --> Data size is not block size multiple.
    why these two above errors got while doing decription with current key but same files encripted without
    any errors and we are not able to download the files form UI side and got above two errors those file manualy able to opened.
    Please help me what is the issue here from files or decription/encription/ code side while download the files.

    FileStream fSource;
    FileStream fDestination;
    ICryptoTransform dec;
    Byte[] inputChar = null;
    Byte[] decryptedChar = null;
    //Int64 bfrSize = 9999999;
    int count;
    int decryptedCount;
    int dataSize = 150000;

    string sourcePath = ConfigurationSettings.AppSettings["EncryptedBackUpFilesPath"] + fileName;
    string destinationPath = ConfigurationSettings.AppSettings["DecryptedFiles"] + fileName;
    fSource = File.OpenRead(sourcePath);
    fDestination = File.Create(destinationPath);
    inputChar = new Byte[dataSize + 16];
    decryptedChar = new Byte[dataSize + 16];

    dec = nKey.CreateDecryptor();

    count = fSource.Read(inputChar, 0, dataSize);
    while (count > 0)
    decryptedCount = dec.TransformBlock(inputChar, 0, count, decryptedChar, 0);
    fDestination.Write(decryptedChar, 0, decryptedCount);
    count = fSource.Read(inputChar, 0, dataSize);
    fDestination.Write(decryptedChar, 0, decryptedCount);
    AesManaged aes = new AesManaged();
    // aes.Padding = PaddingMode.None;
    decryptedChar = dec.TransformFinalBlock(inputChar, 0, count); ---here getting the those two errors.

    fDestination.Write(decryptedChar, 0, decryptedChar.Length);
    Logger.LogError(destinationPath + " is decrypted. POMID is --> " + pomId);

    //CryptoStream crStream = new CryptoStream(fDestination, nKey.CreateEncryptor(), CryptoStreamMode.Write);

    catch (NAEException ex)
    Logger.LogError("Error in Decryption --> " + ex.Message);
    inputChar = null;
    decryptedChar = null;



    Thanks & Regards, Narasimha

    Tuesday, November 18, 2014 11:21 AM


  • Hello Narasimha,

    Could you please provide a demo file? According to your current description, what we can do is to provide some information related with the error occurs in TransformFinalBlock:

    This discussion discusses the “Invalid padding” error message and provides some workarounds, you could check it.

    For this error “Data size is not block size multiple.”, it shows the data you are trying to decrypt which size is not multiple of block size, you could check the example in the link below

    It would firstly verify that multiple blocks cannot be transformed and use the example provides workaround to decrypt the data.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 19, 2014 8:17 AM