[RESOLVIDO] Descriptografia com AES

3 respostas
R

Galera,

Estou com um problema na hora de descriptografar os dados:

Segue erro que estou tendo.

Exception in thread main java.security.InvalidKeyException: Invalid AES key length: 64 bytes

at com.sun.crypto.provider.AESCipher.engineGetKeySize(DashoA13*)

at javax.crypto.Cipher.b(DashoA13*)

at javax.crypto.Cipher.a(DashoA13*)

at javax.crypto.Cipher.a(DashoA13*)

at javax.crypto.Cipher.a(DashoA13*)

at javax.crypto.Cipher.init(DashoA13*)

at javax.crypto.Cipher.init(DashoA13*)

at EncryptDecryptHelper.decryptStringFromBytes_AES(EncryptDecryptHelper.java:38)

at EncryptDecryptHelper.decrypt(EncryptDecryptHelper.java:25)

at EncryptDecryptHelper.main(EncryptDecryptHelper.java:76)

O que especificamente significa isso, quando eu tento com 16bit, ele funciona perfeitamente :S

Podem me ajudar com isso?

3 Respostas

R

Estou recebendo uma String criptografada de um sistema legado, que seria esse código fGwXfERan92FG9cN2s5tXeLAAixaASBtrSu56t1X73g.

E me enviaram duas key

KEY: 3EE4582DD1573591EEB960CA1F832F9989572F244E477A9514AADCD656D01234

IV: 593B2A8D4587DAB1949EB326752E4DDE

Quando estou tentando descriptografar esse código: fGwXfERan92FG9cN2s5tXeLAAixaASBtrSu56t1X73g, com essas keys, está me retornando erro invalid AES key. tentei pesquisar na internet, mas não encontrei muita coisa.

public static String decryptStringFromBytes_AES(final byte[] cipherText, final String key, final String iv) throws Exception {

		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		byte[] keyBytes= key.getBytes();

		SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
		IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());
		cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);

		BASE64Decoder decoder = new BASE64Decoder();
		byte [] results = cipher.doFinal(decoder.decodeBuffer(cipherText.toString()));
		return new String(results,"UTF-8");
	}


[code]public static void main(final String[] args) throws Exception {
		System.out.println(
				EncryptDecryptHelper.decryptStringFromBytes_AES("fGwXfERan92FG9cN2s5tXeLAAixaASBtrSu56t1X73g".getBytes(), "3EE4582DD1573591EEB960CA1F832F9989572F244E477A9514AADCD656D01234", "593B2A8D4587DAB1949EB326752E4DDE"));
	}
R

Consegui contornar o problema

Criei um método que transforma a key em hexadecimal e altera para 32 bytes.

public static byte[] stringToByteArray(final String hex) throws Exception {

		int NumberChars = hex.length();
		byte[] bytes = new byte[NumberChars / 2];
		for (int i = 0; i < NumberChars; i += 2) {
			int y = Integer.parseInt(hex.substring(i,  i + 2), 16);
			bytes[i/2] = (byte)(y & 0xFF);
		}
		return bytes;
	}

Tive um erro também “given final block not properly padded”.

Esse problema estava acontecendo por que o texto que eu estava tentando descriptografar, estava vindo com - e _, tive que fazer replace, metodo final ficou assim:

public String decrypt(String cipherText, final boolean oldDecrypt) throws Exception {
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

		SecretKeySpec keySpec = UnsubscribeBO.generateKey();
		IvParameterSpec ivSpec = UnsubscribeBO.getIv();
		cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);

		cipherText = cipherText.replaceAll("-", "+");
		cipherText = cipherText.replaceAll("_", "/");

		byte[] decordedValue = new BASE64Decoder().decodeBuffer(cipherText);
		byte[] decValue = cipher.doFinal(decordedValue);

		return new String(decValue, "iso-8859-1");
	}

        private static SecretKeySpec generateKey() throws Exception {	
		return new SecretKeySpec(UnsubscribeBO.stringToByteArray("3EE4582DD1573591EEB960CA1F832F9989572F244E477A9514AADCD656D01234"), "AES");		
	}

	private static IvParameterSpec getIv() throws Exception {
		return new IvParameterSpec(UnsubscribeBO.stringToByteArray("593B2A8D4587DAB1949EB326752E4DDE"));	
	}
W

O que tem no método at bbc.tvl.helper.EncryptDecryptHelper.decryptStringFromBytes_AES e qual que é a linha 38?

Criado 27 de março de 2013
Ultima resposta 28 de mar. de 2013
Respostas 3
Participantes 2