cryp-to-js
v1.1.1
Published
google CryptoJS v3.1.2
Downloads
12
Readme
CryptoJS
This repo is straight unmodified-in-any-way copy of Google Code hosted CryptoJS v3.1.2 project at https://code.google.com/p/crypto-js/,
use
var CryptoJS = require("cryp-to-js").default
example
HmacSHA1
var CryptoJS = require("cryp-to-js").default
var str = 'message'
var openKey = 'b3dcd87a8152864f05e7b52232a73656&'
var hash = CryptoJS.HmacSHA1(str, openKey)
var sign = hash.toString(CryptoJS.enc.Base64)
MD5
var CryptoJS = require("cryp-to-js").default
var sign = CryptoJS.MD5('Message').toString()
AES
var CryptoJS = require("cryp-to-js").default
/**
* 加密
*
* @param {String} data 明文
* @param {String} key 秘钥 十六位十六进制数
* @param {String} iv 秘钥偏移量 十六位十六进制数
*
* @return {String} If the cookie doesn't exist, return undefined
*/
function encryptedAES(data, key, iv) {
var key = CryptoJS.enc.Utf8.parse(key);
var iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt(data, key,
{
iv:iv,
mode:CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
}
)
return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}
var a = encryptedAES('1','1234567890123456', '1234567890123456')
// m4R0rUAW4REnW0XPhHfDCw==
/**
* 解密
*
* @param {String} data 明文
* @param {String} key 秘钥
* @param {String} iv 秘钥偏移量 十六位十六进制数
*
* @return {String} If the cookie doesn't exist, return undefined
*/
function decryptedAES(encrypted, key, iv){
var key = CryptoJS.enc.Utf8.parse(key)
var iv = CryptoJS.enc.Utf8.parse(iv)
var decrypted = CryptoJS.AES.decrypt(encrypted, key,
{
iv:iv,
mode:CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
}
)
return decrypted.toString(CryptoJS.enc.Utf8)
}
decryptedAES(a,'1234567890123456', '1234567890123456')
//1
Base64 encode decode
var CryptoJS = require("cryp-to-js").default
// encode
var enwords = CryptoJS.enc.Utf8.parse('we'); // WordArray object
console.log(enwords)
var enbase64 = CryptoJS.enc.Base64.stringify(enwords); // string: 'SGVsbG8gd29ybGQ='
console.log(enbase64)
// decode
var dewords = CryptoJS.enc.Base64.parse(enbase64);
console.log(dewords)
var destr = CryptoJS.enc.Utf8.stringify(dewords);
console.log(destr)