gpt4 book ai didi

java - cryptojs aes 256加密和java解密

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:02 25 4
gpt4 key购买 nike

我在客户端使用 cryptojs aes 256 加密消息。但无法在java端解密它。首先,我将 key 以十六进制形式直接传递到服务器端,然后将其从十六进制转换为 java 字节数组。它不起作用。然后我将短语 salt, iv 以十六进制形式传递到服务器。然后生成 key 。还是没用。老是提示 key 长度不对。

客户端:

var salt = CryptoJS.lib.WordArray.random(16);
var salt_hex = CryptoJS.enc.Hex.stringify(salt);

var iv = CryptoJS.lib.WordArray.random(256/32);
var iv_hex = CryptoJS.enc.Hex.stringify(iv);


var key = CryptoJS.PBKDF2(secret, salt, { keySize: 256/32, iterations: 10 });
var key_hex=CryptoJS.enc.Hex.stringify(key);

var encrypted = CryptoJS.AES.encrypt(plaintext, key, { iv: iv });

var encryptedtxt = secret+":"+salt_hex+":"+iv_hex+":"+encrypted.ciphertext.toString(CryptoJS.enc.Base64)+":"+key_hex;

服务器端:

    if (encrypted != null)
{
//Get the passphras, salt, IV and msg
String data[] = encrypted.split(":");
String passphrase = data[0];
String salt_hex = data[1];
String iv_hex = data[2];
String msg64 = data[3];
String jskey_hex = data[4];
byte[] jskey = hexStringToByteArray(jskey_hex);
byte[] iv = hexStringToByteArray(iv_hex);
byte[] salt = hexStringToByteArray(salt_hex);
BASE64Decoder decoder = new BASE64Decoder();
byte[] msg = decoder.decodeBuffer(msg64);

try {
//theClear = AES.decrypt(encrypted);
/* Decrypt the message, given derived key and initialization vector. */
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), salt, 10, 256/32);
SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
String plaintext = new String(cipher.doFinal(msg), "UTF-8");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

终于明白了。默认情况下,JRE 7 不支持 256 位 key 。我必须从 http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html 下载新的 jar 并将其覆盖在 jre/lib/security 文件夹中。对于Websphere 6或7,您必须执行类似的操作。如果您不执行此操作,则会提示“非法 key 大小”。我还看到过“非法 key 大小 x”,其中 x 是一个数字。这意味着 key 大小不正确。下面是代码。

服务器端:

//@Override
public String getClearText() throws IOException {
// Get the body
String encrypted = super.getParameter("aes"); //base64

if (encrypted != null)
{
//Get the passphras, salt, IV and msg
String data[] = encrypted.split(":");
String passphrase = data[0];
String salt_hex = data[1];
String iv_hex = data[2];
String msg64 = data[3];
String jskey_hex = data[4];
byte[] jskey = hexStringToByteArray(jskey_hex);
byte[] iv = hexStringToByteArray(iv_hex);
byte[] salt = hexStringToByteArray(salt_hex);
BASE64Decoder decoder = new BASE64Decoder();
byte[] msg = decoder.decodeBuffer(msg64);
String plaintext = "";
try {
SecretKey key = new SecretKeySpec(jskey, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
plaintext = new String(cipher.doFinal(msg), "UTF-8");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("in filter, decrypted: " +plaintext);
}
return plaintext;
}

客户:

$(function() {

$('#test').on('submit', function() {

var plaintext = $('#text').val();
var secret = '0123456789abcdef';

var salt = CryptoJS.lib.WordArray.random(16);
var salt_hex = CryptoJS.enc.Hex.stringify(salt);

var iv = CryptoJS.lib.WordArray.random(16);
var iv_hex = CryptoJS.enc.Hex.stringify(iv);


var key = CryptoJS.PBKDF2(secret, salt, { keySize: 256/32, iterations: 1 });
//var key_hex=CryptoJS.enc.Hex.stringify(key);
var key_hex= key;

var encrypted = CryptoJS.AES.encrypt(plaintext, key, { iv: iv });


//decrypt
var decrypted = CryptoJS.AES.decrypt(
encrypted,
CryptoJS.enc.Hex.parse(key_hex),
{ iv: CryptoJS.enc.Hex.parse(iv_hex) });

var text = decrypted.toString( CryptoJS.enc.Utf8 );
//console.log(encrypted);

// ----- base64 encoding ----------
var encryptedtxt = secret+":"+salt_hex+":"+iv_hex+":"+encrypted.ciphertext.toString(CryptoJS.enc.Base64)+":"+key_hex;
console.log('html - ciphere txt : ' +encryptedtxt);

// ---- testing ----
//var decrypted = CryptoJS.AES.decrypt(encrypted, key,{iv: CryptoJS.enc.Utf8.parse(iv)});
//console.log(decrypted.toString(CryptoJS.enc.Utf8));

post ('/E2Efilter/TheServlet', encryptedtxt);
return false;
});

关于java - cryptojs aes 256加密和java解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24414258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com