-2

I am doing AES 128 bit encyption and decryption using following java code which works fine. For same key and data it gives me same output each time.

link1: http://aesencryption.net/#Java-aes-encryption-example

(final String strPssword = "dAtAbAsE98765432";)

For AES 256 bit encryption and decryption I found another java code. It gives me different output each time.

link2: AES-256 Password Based Encryption/Decryption in Java

My question is that, whether i need to wirte different code for AES 128 bit and AES 256 bit? Or are they same? only key size is different?

For 256 bit encryption only i need to change in first java code link

public boolean setKey(String sKey){
    arbtKey = Arrays.copyOf(arbtKey, 32); // use only first 256 bits
}

public static void main(String[] aryCmdArgs)
{
    final String sKey = "dAtAbAsE98765432dAtAbAsE98765432"; //256 bits
}

or i need to use a code given in link2?

Community
  • 1
  • 1
RPrashant
  • 217
  • 3
  • 13

1 Answers1

1

The sample code for the 256-bit key encryption uses a random salt, which is why output will be different from one execution to the next. As for using the code from the first (128-bit) sample, you just supply a key with a supported length. You just supply the algorithm name ("AES") and it will figure the rest out from the key length. Supported lengths are 128, 192 and 256 bits.

G_H
  • 11,739
  • 3
  • 38
  • 82