2013-03-11 15 views
5

Ho un metodo ColdFusion per la decrittografia di una stringa: PFN123. Usa l'algoritmo AES, la codifica HEX e ha una lunghezza di 128 bit. L'output è:ColdFusion Java Uscita di codifica diversa per la stessa stringa con algoritmo AES

32952063062A232355AABB63E129EA9F 

Ho scritto codice java equivalente per crittografia e decrittografia AES. Tuttavia produce un risultato diverso:

07e342ad4b59b276cbb6418248aaf886. 

Non capisco perché i risultati siano diversi per lo stesso algoritmo e schema di codifica. Qualcuno può spiegare perché? Grazie in anticipo.

del codice Java:

import java.security.Key; 

import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 

import org.apache.commons.codec.binary.Hex; 

public class AESEncryptionDecryptionTest { 

    private static final String ALGORITHM  = "AES"; 
    private static final String myEncryptionKey = "OIXQUULC7khaJzzOOHRqgw=="; 
    private static final String UNICODE_FORMAT = "UTF8"; 

    public static String encrypt(String valueToEnc) throws Exception { 
     Key key = generateKey(); 
     Cipher c = Cipher.getInstance(ALGORITHM); 
     c.init(Cipher.ENCRYPT_MODE, key); 
     byte[] encValue = c.doFinal(valueToEnc.getBytes(UNICODE_FORMAT)); 
     String encryptedValue = new Hex().encodeHexString(encValue); 
     return encryptedValue; 
    } 

    public static String decrypt(String encryptedValue) throws Exception { 
     Key key = generateKey(); 
     Cipher c = Cipher.getInstance(ALGORITHM); 
     c.init(Cipher.DECRYPT_MODE, key); 
     byte[] decordedValue = new Hex().decode(encryptedValue.getBytes()); 
     byte[] decValue = c.doFinal(decordedValue);//////////LINE 50 
     String decryptedValue = new String(decValue); 
     return decryptedValue; 
    } 

    private static Key generateKey() throws Exception { 
     byte[] keyAsBytes; 
     keyAsBytes = myEncryptionKey.getBytes(); 
     Key key = new SecretKeySpec(keyAsBytes, ALGORITHM); 
     return key; 
    } 

    public static void main(String[] args) throws Exception { 

     String value = "PFN123"; 
     String valueEnc = AESEncryptionDecryptionTest.encrypt(value); 
     String valueDec = AESEncryptionDecryptionTest.decrypt(valueEnc); 

     System.out.println("Plain Text : " + value); 
     System.out.println("Encrypted : " + valueEnc); 
     System.out.println("Decrypted : " + valueDec); 
    } 

} 
+0

Che cosa fa la Fusione fredda nel suo metodo implicito di decrittografia che mi manca in Java ?? –

+0

Potrebbe usare un'altra codifica di UTF8, usare una diversa modalità di concatenamento dei blocchi, usare un padding differente (e ovviamente un'altra chiave di input, ma suppongo che l'abbiate verificata). –

+0

Sì, ho provato tutte e tre le opzioni in base a tutte le combinazioni ... Il mio problema è che devo crittografare una stringa in coldFusion e quindi decodificarla in Java. –

risposta

2

Grazie per il supporto. Ho trovato la mia risposta. ColdFusion sta memorizzando la chiave nei suoi byte decodificati Base64. Quindi in Java dobbiamo generare la chiave decodificando tramite BASE64Decoder:

private static Key generateKey() throws Exception 
{ 
    byte[] keyValue; 
    keyValue = new BASE64Decoder().decodeBuffer(passKey); 
    Key key = new SecretKeySpec(keyValue, ALGORITHM); 

    return key; 
} 
Problemi correlati