How to Encrypt or Decrypt with Rijndael and a Block-Size of 256 Bits

How to encrypt or decrypt with Rijndael and a block-size of 256 bits?

There is no support in any of the Sun JCE providers for anything other than Rijndael with the 128-bit blocksize: this is the AES algorithm. To get rijndael with the 256-bit blocksize you will have to go somewhere else. I suggest the Bouncycastle java library. The RijndaelEngine class has a constructor that accepts a block size in bits. Most people find the PaddedBufferedBlockCipher class to be more convenient when used with suitable padding, e.g.

PaddedBufferedBlockCipher c = new PaddedBufferedBlockCipher(new RijndaelEngine(256), new PKCS7Padding());

Decrypt Rijndael 256 Block Size with BouncyCastle

I believe your problem is in the line

cipher.Init(true, keyParamWithIV);

the first parameter initializes the cipher for encryption if true and for decryption if false. If you set it to false it should work.

See
http://people.eecs.berkeley.edu/~jonah/bc/org/bouncycastle/crypto/paddings/PaddedBufferedBlockCipher.html#init(boolean,%20org.bouncycastle.crypto.CipherParameters)

How to use Rijndael algorithm with 256 long block size in dotnet core 2.1

The best documentation is a source code. According to their source code only 128 is supported:

public override int BlockSize
{
get { return _impl.BlockSize; }
set
{
Debug.Assert(BlockSizeValue == 128);

//Values which were legal in desktop RijndaelManaged but not here in this wrapper type
if (value == 192 || value == 256)
throw new PlatformNotSupportedException(SR.Cryptography_Rijndael_BlockSize);

// Any other invalid block size will get the normal "invalid block size" exception.
if (value != 128)
throw new CryptographicException(SR.Cryptography_Rijndael_BlockSize);
}
}

Use BouncyCastle.NetCore. There is a code snippet available at the following link:

var keyBytes = password.GetBytes(Keysize / 8);
var engine = new RijndaelEngine(256);
var blockCipher = new CbcBlockCipher(engine);
var cipher = new PaddedBufferedBlockCipher(blockCipher, new Pkcs7Padding());
var keyParam = new KeyParameter(keyBytes);
var keyParamWithIV = new ParametersWithIV(keyParam, ivStringBytes, 0, 32);

cipher.Init(true, keyParamWithIV);
var comparisonBytes = new byte[cipher.GetOutputSize(cipherTextBytes.Length)];
var length = cipher.ProcessBytes(cipherTextBytes, comparisonBytes, 0);
cipher.DoFinal(comparisonBytes, length);

256-bit Rijndael blocksize?

Looking at the implementation, the current iterations of crypto++ only support Rijndael with a block size of 16 bytes. As the IV has to be precisely a single block for CBC mode, Rijndael with a 256 bit block size does not seem to be possible.

Erdelsky's Rijndael 128bit blocks 256bit keys Implementation in java

Some extra background.
I tried to create a java implementation of a packing algorithm for some game engine. I suppose that ECB encryption was selected to reduce entropy between different versions of packed assets (because patching system is trying to install chunks of already encrypted content into existing data). What about JDK 1.8.112 - I used Excelsior Jet to AOT-compile my program (it will be open-source, I'll append the link to this post a bit later), but unfortunately, my previous version of the AOT compiler only supported 1.8.112, so I used same JDK version to ensure compatibility.

Verbose answer for non-crypto-guys (like me) in facts:

  1. The block size is always 16 bytes-wide, this was completely okay.
  2. The key length is 256 bit-wide (or 32 bytes), you could not use that long keys in older JDK patches, but then later, the restriction has been removed, so update your JDK to Java 9, Java 8u161, Java 7u171 or 6u181, or download and install this Unlimited Strength Jurisdiction Policy.
  3. You have to use ECB encryption to match Erdelsky's Rijndael, this is less secure than CBC, but it can still be used for compatibility reasons.

Thanks everyone for the answer and a discussion. Cheers!

Decryption results in gibberish with Rijndael and 256 block size

AES uses a 128 bit block size. The VB code uses Rijndael with 256 bit block size.



Related Topics



Leave a reply



Submit