Criptografia [Resolvido]

Pessoal,

Estou usando essa classe para criptografar e descriptografar alguns dados:

public class Encrypter {
 
    private static final String METODO_ENCRIPTACAO = "AES";
    public static final byte[] CHAVE = {71, -75, -63, 101, -21, 64, -12, 50, -27, -44, 13, -87, 49, -58, -44, -4, -4, -69, -7, 113, -68, 28, 74, -114, 89, -104, 103, -106, -67, 21, -103, -107};
 
    public static String encriptar(byte[] key, String value)
            throws SegurancaException {
 
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, METODO_ENCRIPTACAO);
 
            Cipher cipher = Cipher.getInstance(METODO_ENCRIPTACAO);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(value.getBytes());
 
            return new BASE64Encoder().encode(encrypted);
        }
        catch (Exception e) {
            throw new SegurancaException("Erro ao criptografar informações " + e.getMessage());
        }
    }
 
    public static String decriptar(byte[] key, String encrypted)
            throws SegurancaException {
 
        byte[] decrypted = null;
 
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, METODO_ENCRIPTACAO);
 
            byte[] decoded = new BASE64Decoder().decodeBuffer(encrypted);
 
            Cipher cipher = Cipher.getInstance(METODO_ENCRIPTACAO);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            decrypted = cipher.doFinal(decoded);
        }
        catch (Exception e) {
            throw new SegurancaException("Erro ao descriptografar informações " + e.getMessage());
        }
 
        return new String(decrypted);
    }
}

E usando essa para o teste:

public class MainClass {
	public static void main(String[] args) {
		try {
			String value = Encrypter.encriptar(Encrypter.CHAVE, "robson");
			System.out.println(value);
			value = Encrypter.decriptar(Encrypter.CHAVE, value);
			System.out.println(value);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Só que está dando o seguinte erro:

[color=red] Erro ao criptografar informações Illegal key size or default parameters[/color]

Alguém pode me ajudar?

Usar o algoritmo AES com a chave de tamanho incorreto vai provocar esse erro. Ela deve ter 16 bytes, não contei os bytes da sua chave.

entanglement,

Como faço para que aceite uma chave com tamanho diferente de 16 bytes?

Os tamanhos das chaves admitidos pelo AES são: 128, 192, ou 256 bits. (Respectivamente, 16, 24 e 32 bytes).

Para usar chaves maiores que 128 bits, você precisa instalar na JVM um pacotinho chamado “Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 7” ou “Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6” (veja no fim da página de download, http://www.oracle.com/technetwork/java/javase/downloads/index.html ).

Todo algortimo de criptografia tem chave de tamanho fixo. Pelo seu código, a chave é fixa. Por que você não pode trabalhar com chave de 16 bytes (128 bits)? Existem outros algoritmos com tamanhos de chave diferente.
[edit] O entanglement respondeu antes. A resposta dele é mais completa[/edit]

Veleu pessoal, deu tudo certo aqui!