Exception when using Cipher

I have this code

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

keyGen.initialize(1024);

KeyPair keyPair = keyGen.genKeyPair();

PrivateKey priv = keyPair.getPrivate();

PublicKey pub = keyPair.getPublic();

SecretKeySpec skeySpecc = new SecretKeySpec(pub.getEncoded(), "AES");

Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, skeySpecc);

byte[] encrypted = cipher.doFinal(plain.getBytes());

When i run it i have this exception

java.security.InvalidKeyException: Invalid AES key length: 1296

Any one have any idea?

Best Regards

[653 byte] By [eng.aymana] at [2007-11-27 4:52:32]
# 1
You create an RSA key and then try to use it with AES!
sabre150a at 2007-7-12 10:06:34 > top of Java-index,Security,Cryptography...
# 2
How i can do it correctly?
eng.aymana at 2007-7-12 10:06:34 > top of Java-index,Security,Cryptography...
# 3

> How i can do it correctly?

AES is a symmetric cipher, which means that you only have one secret key to encrypt and decrypt data...

RSA is an asymmetric cipher... that means you have two keys... one private and one public...

these two are not interchangeable... both symmetric and asymmetric ciphers have their own pros and cons... you first need to figure out what you want to do before choosing which one you want to implement... you can read about difference between the two in any good crypto book or even on wikipedia...

DarumAa at 2007-7-12 10:06:34 > top of Java-index,Security,Cryptography...