Tuesday, May 6, 2008

AES : JAVA coding

Hello..it's been a long time since my last post. Actually i am a bit busy with my project going to ITEX exhibition this friday.

Now i am going to share with you all an example of AES encryption. This AES encryption used password from user as a key. Firstly, let me tell you more about AES.

Strictly speaking, AES is not precisely Rijndael (although in practice they are used interchangeably) as Rijndael supports a larger range of block and key sizes; AES has a fixed block size of 128 bits and a key size of 128, 192, or 256 bits, whereas Rijndael can be specified with key and block sizes in any multiple of 32 bits, with a minimum of 128 bits and a maximum of 256 bits.

Due to the fixed block size of 128 bits, AES operates on a 4×4 array of bytes, termed the state (versions of Rijndael with a larger block size have additional columns in the state). Most AES calculations are done in a special finite field. [source: wikipedia]
PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator();

byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99,
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};

int count = 16;

IvParameterSpec iv = new IvParameterSpec(salt);

generator.init(pBytes, salt, count);

ParametersWithIV params = (ParametersWithIV)
generator.generateDerivedParameters(128, 128);

KeyParameter keyParam = (KeyParameter) params.getParameters();

SecretKeySpec key = new SecretKeySpec(keyParam.getKey(), "AES");

String password = "secretkey";
String PT = "Attack at dawn";

byte[] pBytes = password.getBytes();

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding","BC");
cipher.init(Cipher.ENCRYPT_MODE,key,iv);

byte[] cipherText = cipher.doFinal(PT.getBytes("ASCII"));
Base64 b64 = new Base64();

String b64ctxt = new String(Base64.encode(cipherText), "ASCII");
String b64key = new String(Base64.encode(key.getEncoded()), "ASCII");

System.out.println("Ciphertext = " + b64ctxt);
System.out.println("B64 key = " + b64key);

byte[] newCtxt = Base64.decode(b64ctxt);
cipher.init(Cipher.DECRYPT_MODE, key,iv);
byte[] plainBytes = cipher.doFinal(newCtxt);
String newPlainText = new String(plainBytes, "ASCII");
System.out.println("Plaintext = [" + newPlainText + "]");

Thats all folks..Hope this would help. Just drop a comment if something is not right about the coding.

5 comments:

  1. Thanks very much for this posting. I have to encrypt some data for a project using AES 128 CBC. We are rewriting a java-based system with Ruby on Rails and I need to encrypt some data using java and store it in a mysql table so that the new application can use it. We'll use a common key and salt on both sides. I'm new to cryptography but fairly proficient in java. Your example seems to be really close to what I need to do. Does it use the Bounty Castle API. I downloaded it and frankly its a bit imposing. Is there a way to do this with the standard java crypto classes? Speed is not an issue. Simplicity and accuracy are much more important as this is a one-time conversion. Thanks again. I look forward to hearing from you at your convenience.

    ReplyDelete
  2. You are the most welcome. Yup, it use BC as it library. Actually you can encrypt and then decrypt it back by simple change a single line. You are the first one who really appreciate my post..LOL..have a nice programming day.

    ReplyDelete
  3. Thank you very much for the example code. I was surprised when I found that AES+CBC wasn't supported in the default Sun provider. I'm glad I found this, worked perfectly (-:

    ReplyDelete
  4. my very welcome. have a nice encryption day=p

    ReplyDelete
  5. That was real helpful man! Keep up the good fight!

    ReplyDelete