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!

InvalidKeyException: Illegal key size

843811Sep 2 2006 — edited May 6 2010
Hello

I try to encrypt a string
public String doEncreption(String inStr)
{
   String retStr="";
   byte[] input =inStr.getBytes();
 byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 
                                                      0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 
                                                     0x13, 0x14, 0x15, 0x16, 0x17};
	        
                Security.addProvider(new BouncyCastleProvider());  
		try
		{
			     Key key = new SecretKeySpec( keyBytes, "AES" );
			    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding","BC");
			    cipher.init( Cipher.ENCRYPT_MODE, key );
			    byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
			    int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
			    ctLength += cipher.doFinal(cipherText, ctLength);
			    retStr = new String(cipherText);
		}catch(Exception  ex){
                                ex.printStackTrace();
                }
		return retStr;
	}

 // I becom an Exception 

java.security.InvalidKeyException: Illegal key size or default parameters
	at javax.crypto.Cipher.a(DashoA13*..)
	at javax.crypto.Cipher.init(DashoA13*..)
	at javax.crypto.Cipher.init(DashoA13*..)
I use Jdk 1.6

what is wrong ????

thanks in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 3 2010
Added on Sep 2 2006
27 comments
1,072 views