JavaScript 使用CryptoJS实现AES-256-CBC加密/解密函数

AES-256-CBC的加解密函数使用的是crypto-js.js来实现,crypto-js.js下载地址是https://github.com/brix/crypto-js/releases crypto-js.js。

实现AES-256-CBC加解密代码

/**
* Encryption class for encrypt/decrypt that works between programming languages.
*
* @author Vee Winch.
* @link https://stackoverflow.com/questions/41222162/encrypt-in-php-openssl-and-decrypt-in-javascript-cryptojs Reference.
* @link https://github.com/brix/crypto-js/releases crypto-js.js can be download from here.
*/
class Encryption {
/**
* @var integer Return encrypt method or Cipher method number. (128, 192, 256)
*/
get encryptMethodLength() {
var encryptMethod = this.encryptMethod;
// get only number from string.
// @link https://stackoverflow.com/a/10003709/128761 Reference.
var aesNumber = encryptMethod.match(/\d+/)[0];
return parseInt(aesNumber);
}// encryptMethodLength

/**
* @var integer Return cipher method divide by 8. example: AES number 256 will be 256/8 = 32.
*/
get encryptKeySize() {
var aesNumber = this.encryptMethodLength;
return parseInt(aesNumber / 8);
}// encryptKeySize

/**
* @link http://php.net/manual/en/function.openssl-get-cipher-methods.php Refer to available methods in PHP if we are working between JS & PHP encryption.
* @var string Cipher method.
* Recommended AES-128-CBC, AES-192-CBC, AES-256-CBC
* due to there is no `openssl_cipher_iv_length()` function in JavaScript
* and all of these methods are known as 16 in iv_length.
*/
get encryptMethod() {
return 'AES-256-CBC';
}// encryptMethod

/**
* Decrypt string.
*
* @link https://stackoverflow.com/questions/41222162/encrypt-in-php-openssl-and-decrypt-in-javascript-cryptojs Reference.
* @link https://stackoverflow.com/questions/25492179/decode-a-base64-string-using-cryptojs Crypto JS base64 encode/decode reference.
* @param string encryptedString The encrypted string to be decrypt.
* @param string key The key.
* @return string Return decrypted string.
*/
decrypt(encryptedString, key) {
var json = JSON.parse(CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(encryptedString)));
var salt = CryptoJS.enc.Hex.parse(json.salt);
var iv = CryptoJS.enc.Hex.parse(json.iv);
var encrypted = json.ciphertext;// no need to base64 decode.
var iterations = parseInt(json.iterations);
if (iterations <= 0) {
iterations = 999;
}
var encryptMethodLength = (this.encryptMethodLength/4);// example: AES number is 256 / 4 = 64
var hashKey = CryptoJS.PBKDF2(key, salt, {'hasher': CryptoJS.algo.SHA512, 'keySize': (encryptMethodLength/8), 'iterations': iterations});
var decrypted = CryptoJS.AES.decrypt(encrypted, hashKey, {'mode': CryptoJS.mode.CBC, 'iv': iv});
return decrypted.toString(CryptoJS.enc.Utf8);
}// decrypt

/**
* Encrypt string.
*
* @link https://stackoverflow.com/questions/41222162/encrypt-in-php-openssl-and-decrypt-in-javascript-cryptojs Reference.
* @link https://stackoverflow.com/questions/25492179/decode-a-base64-string-using-cryptojs Crypto JS base64 encode/decode reference.
* @param string string The original string to be encrypt.
* @param string key The key.
* @return string Return encrypted string.
*/
encrypt(string, key) {
var iv = CryptoJS.lib.WordArray.random(16);// the reason to be 16, please read on `encryptMethod` property.
var salt = CryptoJS.lib.WordArray.random(256);
var iterations = 999;
var encryptMethodLength = (this.encryptMethodLength/4);// example: AES number is 256 / 4 = 64
var hashKey = CryptoJS.PBKDF2(key, salt, {'hasher': CryptoJS.algo.SHA512, 'keySize': (encryptMethodLength/8), 'iterations': iterations});
var encrypted = CryptoJS.AES.encrypt(string, hashKey, {'mode': CryptoJS.mode.CBC, 'iv': iv});
var encryptedString = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
var output = {
'ciphertext': encryptedString,
'iv': CryptoJS.enc.Hex.stringify(iv),
'salt': CryptoJS.enc.Hex.stringify(salt),
'iterations': iterations
};
return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(JSON.stringify(output)));
}// encrypt
}

版权声明:著作权归作者所有。

相关推荐

箭头函数

廖雪峰网站https://www.liaoxuefeng.com/wiki/1022910821149312/1031549578462080ES6标准新增了一种新的函数:Arrow Function(箭头函数)。为什么叫Arrow Function?因为它的定义用的就是一个箭头:x => x * x上面的箭头函数相当于:function (x) { return x * x;}箭头函

Spring Boot使用Jasypt加密配置文件

Jasypt Spring Boot为Spring Boot项目的属性提供了加密支持。有三种方式集成jasypt-spring-boot到Spring boot应用中。jasypt-spring-boot-starter如果Spring Boot项目中使用了@SpringBootApplication或者@EnableAutoConfiguration,在项目里添加jasypt-spring-bo

Java版RSA的加密和解密

代码演示如何使用java来做RSA的加密和解码。import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.Pu

理解JavaScript普通函数以及箭头函数里使用的this

this普通函数的this普通函数的this是由动态作用域决定,它总指向于它的直接调用者。具体可以分为以下四项:this总是指向它的直接调用者, 例如 obj.func() ,那么func()里的this指的是obj。在默认情况(非严格模式,未使用 'use strict'),如果函数没有直接调用者,this为window在严格模式下,如果函数没有

JavaScript Array map()函数的用法及误用

Array的map()函数作用是一个数组映射为另一个数组,映射方式是以原数组的元素作为输入,使用提供的回调函数对输入元素处理,处理后返回的结果作为新数组的元素。基本语法var new_array = arr.map(function callback(currentValue, index, array) {  &nb