Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jsp中的加密和jQuery中的解密_Jquery_Jsp_Encryption_Aes_Cryptojs - Fatal编程技术网

jsp中的加密和jQuery中的解密

jsp中的加密和jQuery中的解密,jquery,jsp,encryption,aes,cryptojs,Jquery,Jsp,Encryption,Aes,Cryptojs,我想在jsp中加密,在jquery中解密,我在jsp中编写了下面的代码 String myKey = "dfslkskfs"; MessageDigest sha = null; key = myKey.getBytes("UTF-8"); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); // use only first 128 bit secretKe

我想在jsp中加密,在jquery中解密,我在jsp中编写了下面的代码

String myKey = "dfslkskfs";
MessageDigest sha = null;
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[]   bytesEncoded = Base64.encodeBase64(cipher.doFinal(json
        .getBytes("UTF-8")));
jsontext =  new String(bytesEncoded );
jsp文件中的html标记:-

<input type="hidden"  id="jsonid" value=<%=jsontext%> />
<input type="hidden" name=secretKey id="secretKey" value=<%=new String(secretKey.getEncoded())%> />

如果我运行上面的代码,我会得到异常未捕获错误:格式错误的UTF-8数据,所以请告诉我哪里出错了,或者你可以告诉我任何其他gud方法。

问题是双重的

调用secretKey.getEncoded时,密钥实际上没有编码。在将其放入页面之前,应将其编码为Base64。以下是一些解决方案:


在客户端,解析密钥,但不解析实际编码为Base64的密文。您应该从Base64解析这两种格式。

请花点时间正确格式化代码。escapeStr是否也将Base64解码为CryptoJS的本机格式?这看起来像是混淆。如果您通过SSL/TLS提供页面,则不需要加密,如果不需要加密,则可以恢复明文,因为您正在发送密钥。此外,欧洲央行是个坏主意,因为它在语义上不安全。您应该使用诸如GCM之类的身份验证模式,或者将CBC与HMAC一起使用。
jsonString      = $("#jsonid").val();
secretKey       = $("#secretKey").val();
jsonString = escapeStr(jsonString);


var key = CryptoJS.enc.Base64.parse(secretKey);

var decryptedData = CryptoJS.AES.decrypt(jsonString, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});
var decryptedText = decryptedData.toString(CryptoJS.enc.Utf8);
alert(decryptedText);