0

I have implemented AES 256 ECB Encryption and Decryption using Bouncy Castle library. I am able to encrypt the data , but unable to decrypt the cipher text. It throws an error saying "Org.BouncyCastle.Crypto.DataLengthException: last block incomplete in decryption". Following is my code for Encryption and Decryption;

public byte[] encrypt(byte[] skey, byte[] data)
{
   PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
   new AesEngine(), new Pkcs7Padding());
   cipher.Init(true, new KeyParameter(skey));
   int outputSize = cipher.GetOutputSize(data.Length);
   byte[] tempOP = new byte[outputSize];
   int processLen = cipher.ProcessBytes(data, 0, data.Length, tempOP, 0);
   int outputLen = cipher.DoFinal(tempOP, processLen);
   byte[] result = new byte[processLen + outputLen];
   Array.Copy(tempOP, 0, result, 0, result.Length);
   return result;
}
public byte[] decrypt(byte[] skey, byte[] data)
{
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
    new AesEngine(), new Pkcs7Padding());
    cipher.Init(false, new KeyParameter(skey));
    int outputSize = cipher.GetOutputSize(data.Length);

    byte[] tempOP = new byte[outputSize];
    int processLen = cipher.ProcessBytes(data, 0, data.Length, tempOP, 0);
    int outputLen = cipher.DoFinal(tempOP, processLen);

    byte[] result = new byte[processLen + outputLen];
    Array.Copy(tempOP, 0, result, 0, result.Length);
    return result;    
}
Topaco
  • 40,594
  • 4
  • 35
  • 62
  • Not reproducible, post the call of `encrypt()` and `decrypt()` along with test data (key, plaintext, ciphertext). – Topaco Jun 11 '22 at 10:05

1 Answers1

0

I have same issue.

Type : Org.BouncyCastle.Crypto.DataLengthException StackTrace : at Org.BouncyCastle.Crypto.Paddings.PaddedBufferedBlockCipher.DoFinal(Byte[] output, Int32 outOff) in /_/crypto/src/crypto/paddings/PaddedBufferedBlockCipher.cs:line 281

-- FoFinal method

        if (bufOff == blockSize)
        {
            num = cipher.ProcessBlock(buf, 0, buf, 0);
            bufOff = 0;
            try
            {
                num -= padding.PadCount(buf);
                Array.Copy(buf, 0, output, outOff, num);
                return num;
            }
            finally
            {
                Reset();
            }
        }

        Reset();
        **throw new DataLengthException("last block incomplete in decryption");**
                               
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32913921) – Abhishek Dutt Oct 16 '22 at 14:04