Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Generating AES Key

843811Oct 10 2005 — edited Oct 10 2005
I am trying to test AES encryption and decryption. I am using JDK 1.5 and Bouncy Castle as my provider.

I am using - Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 5.0



I created a key using keytool cmd as below.



keytool -genkey -alias patient -storepass test -keypass test



In the code below, I am using IV for AES



byte iv[] = new byte[16]; //cipher.getIV();

IvParameterSpec dps = new IvParameterSpec(iv);



When I run the code I get this error



Exception in thread "main" java.security.InvalidKeyException: Key length not 128/192/256 bits.

Error is in the line

cipher.init(Cipher.ENCRYPT_MODE, skeySpec, dps);



I am not able to create the key of length 128/192/256 using keytool, how do I do that?

When I try to generate key for length 128/192/256 using



keytool -genkey -alias patient1 -keysize 256 -storepass test -keypass test



I get this error



keytool error: java.lang.IllegalArgumentException: Modulus size must range from 512 to 1024 and a multiple of 64



Is there anyother way I can create the key?

I guess I cannot use keytool to generate key, since it doesn't generate secret key. Can some one point me how do we generate and store the AES key?



---------------------------------------------------------------------------------------------------------------------------------



import java.security.*;

import javax.crypto.*;

import javax.crypto.spec.*;

import java.io.*;

import org.bouncycastle.jce.provider.BouncyCastleProvider ;



public class AES {

public static String asHex (byte buf[]) {

StringBuffer strbuf = new StringBuffer( buf.length * 2);

int i;

for (i = 0; i < buf.length ; i++) {

if (((int) buf[i] & 0xff) < 0x10) strbuf.append("0");

strbuf.append(Long.toString((int) buf[i] & 0xff, 16));

}



return strbuf.toString();

}



public static void main(String[] args) throws Exception {

Security.addProvider(new BouncyCastleProvider());

String message="This is just an example";



KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

char[] passwd = {'t','e','s','t'};

String fname = System.getProperty("user.home") + File.separator + ".keystore";

System.out.println("User home = " + System.getProperty("user.home"));

FileInputStream fis = new FileInputStream(fname);

ks.load(fis, passwd);



Key k = ks.getKey("patient", passwd);

byte[] raw = k.getEncoded();



SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

System.out.println("SecretKeySpec = " + skeySpec.toString());



// Instantiate the cipher

Cipher cipher = Cipher.getInstance ("AES/CBC/PKCS5Padding", "BC");



byte iv[] = new byte[16]; //cipher.getIV();



IvParameterSpec dps = new IvParameterSpec(iv);

cipher.init(Cipher.ENCRYPT_MODE , skeySpec, dps);



byte[] encrypted = cipher.doFinal ((args.length == 0 ? "This is just an example" : args[0]).getBytes());

System.out.println("encrypted string: " + asHex(encrypted));



cipher.init(Cipher.DECRYPT_MODE , skeySpec, dps);

byte[] original = cipher.doFinal (encrypted);

String originalString = new String(original);

System.out.println("Original string: " + originalString + " Hexa Val " + asHex(original));

}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 7 2005
Added on Oct 10 2005
1 comment
1,802 views