package aesprueba;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class AESprueba {
/**
* @param args the command line arguments
* @throws java.security.NoSuchAlgorithmException
* @throws java.security.NoSuchProviderException
* @throws javax.crypto.NoSuchPaddingException
* @throws java.security.InvalidKeyException
* @throws javax.crypto.ShortBufferException
* @throws javax.crypto.IllegalBlockSizeException
* @throws javax.crypto.BadPaddingException
*/
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException {
// TODO code application logic here
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte [] mensaje = "Hola amigos mios".getBytes();
byte [] clave = "asdfghjkasdfghjk".getBytes();
SecretKeySpec key = new SecretKeySpec(clave, "AES");
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte [] ciphertext = new byte [cipher.getOutputSize(mensaje.length)];
int ciphertextLength = cipher.update(mensaje, 0, mensaje.length, ciphertext, 0);
ciphertextLength += cipher.doFinal(ciphertext, ciphertextLength);
StringBuffer hexCipher = new StringBuffer();
for(int i=0; i<ciphertext.length; i++)
hexCipher.append(Integer.toString((ciphertext[i]&0xff)+0x100,16).substring(1));
System.out.println("Cipher: " + hexCipher);
}
}
Me aparece este error:
Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1026) at javax.crypto.Cipher.implInit(Cipher.java:801) at javax.crypto.Cipher.chooseProvider(Cipher.java:864) at javax.crypto.Cipher.init(Cipher.java:1249) at javax.crypto.Cipher.init(Cipher.java:1186) at aesprueba.AESprueba.main(AESprueba.java:36)