我正在尝试使用 Javascript 的CryptoJS加密客户端发送的密码, 并使用 Java 的Cipher类在服务器端对其进行解密。
在客户端:
<html>
<body>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/pad-nopadding.js"></script>
<script>
var iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
var encrypted = CryptoJS.AES.encrypt("A Sample Message", "SecretPassphrase", { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding, iv: iv });
console.log("iv: " + encrypted.iv.toString(CryptoJS.enc.Hex));
console.log("ct: " + encrypted.ciphertext.toString(CryptoJS.enc.Hex));
</script>
</body>
</html>
我在Firebug上得到的输出是
iv: a43e384b24e275c29a8a68bc031fd79e
ct: c86b6ca4ef30fadfea28821e04aa8dad
在服务器端:
import java.nio.charset.Charset;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.*;
public class AES {
public static String decrypt(String encryptedData) throws Exception {
byte[] keyBytes = "SecretPassphrase".getBytes();
Key key = new SecretKeySpec(keyBytes, "AES");
Cipher c = Cipher.getInstance(ALGO);
byte[] iv = (byte[]) new Hex().decode("a43e384b24e275c29a8a68bc031fd79e");
IvParameterSpec ivspec = new IvParameterSpec(iv);
c.init(Cipher.DECRYPT_MODE, key, ivspec);
byte[] decordedValue = (byte[]) new Hex().decode(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = Hex.encodeHexString(decValue);
return decryptedValue;
}
public static void main(String[] args) throws Exception {
String result = AES.decrypt("c86b6ca4ef30fadfea28821e04aa8dad");
System.out.println(hexToString(result));
}
}
我需要一些关于我做错了什么的帮助,以及iv
当我限制它使用传递的 iv 时,为什么我在客户端变得随机。