php js java AES-256-CBC 对称加密 php加密 js java解密
php
<?php
function encode() {
$auth_key = "qweasdzxc123456QWEASDZXC|}[)8675";
$content = "qweqweqwe asd";
$options = 0;
$iv = "test1test1qweasd";
$str = openssl_encrypt($content, 'AES-256-CBC', $auth_key, $options, $iv); // options 默认值 0 表示 base64输出
return $str;
}
function decode($str) {
$auth_key = "qweasdzxc123456QWEASDZXC|}[)8675";
$options = 0;
$iv = "test1test1qweasd";
$str = openssl_decrypt($str, 'AES-256-CBC', $auth_key, $options, $iv); // options 默认值 0 表示 base64输出
return $str;
}
$str = encode();
var_dump($str);
//var_dump(base64_encode($str));
$str = decode($str);
var_dump($str);
js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test1</title>
<!--<link rel="stylesheet" href="/crypto-js.min.js">-->
<script src="crypto-js.js"></script>
</head>
<body>
<script>
var keystr = "qweasdzxc123456QWEASDZXC|}[)8675";//秘钥。长度32的16进制字符串。
var ivstr="test1test1qweasd";
var enstr = encrypt2("qweqweqwe asd", keystr, ivstr);
console.log(enstr.toString(CryptoJS.enc.Utf8))
function encrypt2(word, keyStr, ivStr) {
const key = CryptoJS.enc.Latin1.parse(keyStr);
const iv = CryptoJS.enc.Latin1.parse(ivStr);
const encoded = CryptoJS.AES.encrypt(word, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
adding: CryptoJS.pad.ZeroPadding
}).toString()
return encoded;
}
// 解密
var destr = decrypt2("IcrUE7dxuM5B0/IV5qXWwQ==", keystr, ivstr);
console.log(destr)
function decrypt2(word, keyStr, ivStr) {
keyStr = keyStr;
ivStr = ivStr;
var key = CryptoJS.enc.Utf8.parse(keyStr);
let iv = CryptoJS.enc.Utf8.parse(ivStr);
var decrypt = CryptoJS.AES.decrypt(word, key, {
iv,
mode: CryptoJS.mode.CBC,
//padding: CryptoJS.pad.ZeroPadding
});
return decrypt.toString(CryptoJS.enc.Utf8);
}
</script>
</body>
</html>
java
package test2;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import test1.test4;
public class test3 {
private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
private static final String KEY = "00000000000000000000000000000000";
private static final String IV = "test1test1qweasd";
public static void main(String[] args) throws Exception {
//test1();
test2();
}
public static void test2() throws Exception {
String KEY="qweasdzxc123456QWEASDZXC|}[)8675";
String IV="test1test1qweasd";
String s2 = AES_cbc_decrypt("IcrUE7dxuM5B0/IV5qXWwQ==".getBytes(),KEY.getBytes(), IV.getBytes());
System.out.println(s2);
}
public static void test1() throws Exception {
// 加密
String s = AES_cbc_encrypt("aaa".getBytes(), KEY.getBytes(), IV.getBytes());
System.out.println(s);
//解密
String s2 = AES_cbc_decrypt("db/3myf1YyIZFNLzk9OAJw==".getBytes(), KEY.getBytes(), IV.getBytes());
System.out.println(s2);
}
//加密
public static String AES_cbc_encrypt(byte[] srcData, byte[] key, byte[] iv) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); //AES-256-CBC
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
return Base64.getEncoder().encodeToString(srcData);
}
//解密
public static String AES_cbc_decrypt(byte[] encData, byte[] key, byte[] iv) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
return new String(cipher.doFinal(Base64.getDecoder().decode(encData)));
}
}
|