5
我有一個用於解密字符串的ColdFusion方法:PFN123
。它使用AES算法,HEX編碼,長度爲128位。輸出是:ColdFusion Java與AES算法相同的字符串的不同編碼輸出
32952063062A232355AABB63E129EA9F
我寫了用於AES加密和解密的等效java代碼。然而它產生了不同的結果:
07e342ad4b59b276cbb6418248aaf886.
我不明白爲什麼結果對於相同的算法和編碼方案是不同的。 任何人都可以解釋爲什麼?提前致謝。
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);
}
}
什麼額外的ColdFusion是我在Java中很想念它的解密隱式方法做? – 2013-03-11 11:59:25
它可以使用另一種編碼比UTF8,使用不同的塊鏈模式,使用不同的填充(當然還有另一個輸入鍵,但我想你檢查了)。 – 2013-03-11 12:25:28
是通過所有組合嘗試了所有三個選項....我的問題是我必須在coldFusion中加密一個字符串,然後用Java解密它。 – 2013-03-11 12:33:52