Problems using JCE AES
I am trying to use AES encryption with a 256 bit key but using the following
code I keep getting this error message
Error Illegal key size or default parameters
from the line
AesCipher.init(Cipher.ENCRYPT_MODE, KeySpec);
This is the code
=========================================================================
package testaes;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
public class Main
{
public Main(String[] args)
{
try
{
// get key generator using the default provider
Cipher AesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// 256 length key
byte[] keyBytes = {
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb,
(byte)0xbb
};
SecretKeySpec KeySpec = new SecretKeySpec(keyBytes, "AES");
AesCipher.init(Cipher.ENCRYPT_MODE, KeySpec);
byte[] CipherText;
String PlainText = args[0];
CipherText = AesCipher.doFinal(PlainText.getBytes());
System.out.println("The encrypted text is [" + CipherText + "]");
}
catch (Throwable e)
// show what went wrong and tie it up with a usage display if we have one
{
System.out.println("Error " + e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
System.out.println("Encrypting [" + args[0] + "]");
Main testAES = new Main(args);
}
catch (Throwable e)
{
System.out.println("Error " + e.getMessage());
e.printStackTrace();
}
}
}
=========================================================================
So how can I specify the key length?
The java version I am using is
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Server VM (build 1.5.0_04-b05, mixed mode)
TIA,
Pep.