Java AES: Nenhum provedor instalado suporta esta chave: javax.crypto.spec.SecretKeySpec
Estou tentando configurar a criptografia AES de 128 bits e estou recebendo uma exceção no meu Cipher.init:
No installed provider supports this key: javax.crypto.spec.SecretKeySpec
Estou gerando a chave no lado do cliente usando o seguinte código:
private KeyGenerator kgen;
try {
kgen = KeyGenerator.getInstance("AES");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
kgen.init(128);
}
SecretKey skey = kgen.generateKey();
Esta chave é então passada para o servidor como um cabeçalho. é codificado em Base64 usando esta função:
public String secretKeyToString(SecretKey s) {
Base64 b64 = new Base64();
byte[] bytes = b64.encodeBase64(s.getEncoded());
return new String(bytes);
}
O servidor puxa o cabeçalho e faz
protected static byte[] encrypt(byte[] data, String base64encodedKey) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher;
try {
cipher = Cipher.getInstance("AES");
} catch (NoSuchAlgorithmException ex) {
//log error
} catch (NoSuchPaddingException ex) {
//log error
}
SecretKey key = b64EncodedStringToSecretKey(base64encodedKey);
cipher.init(Cipher.ENCRYPT_MODE, key); //THIS IS WHERE IT FAILS
data = cipher.doFinal(data);
return data;
}
private static SecretKey b64EncodedStringToSecretKey(String base64encodedKey) {
SecretKey key = null;
try {
byte[] temp = Base64.decodeBase64(base64encodedKey.getBytes());
key = new SecretKeySpec(temp, SYMMETRIC_ALGORITHM);
} catch (Exception e) {
// Do nothing
}
return key;
}
Para depurar isso, eu coloquei pontos de interrupção após a geração de chaves no lado do cliente e antes do cipher.init no lado do servidor. De acordo com o Netbeans, os bytes que compõem as SecretKeys são idênticos e têm 16 bytes de comprimento (na verdade, até onde eu sei, os objetos são idênticos
Estou ciente do material JCE de força ilimitada, mas não tenho a impressão de que precisava para o AES de 128 bit
Client Side: versão java "1.6.0_26"
Server Side: versão java "1.6.0_20"
Alguma ideia