rodrigodesp 28 de mar. de 2013
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" ));
}
rodrigodesp 4 de abr. de 2013
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" ));
}
WellingtonRamos 28 de mar. de 2013
O que tem no método at bbc.tvl.helper.EncryptDecryptHelper.decryptStringFromBytes_AES e qual que é a linha 38?