crypto-js
npm install crypto-js
- 主要用来处理: md5, AES, Base64
md5 摘要
const CryptoJS = require('crypto-js');
function md5_test() {
console.log(CryptoJS.MD5('qwwqww').toString());
const wordArray = CryptoJS.enc.Utf8.parse('qwwqww');
CryptoJS.MD5(wordArray).toString();
console.log(CryptoJS.enc.Utf8.stringify(wordArray));
const md5_hasher = CryptoJS.algo.MD5.create();
md5_hasher.update('qww');
md5_hasher.update('qww');
const hash = md5_hasher.finalize();
console.log(hash.toString());
}
md5_test();
Base64 编码
(function base64_test() {
console.log(CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('qww')));
let md5_wordArr = CryptoJS.MD5('qwwqww');
console.log(md5_wordArr.toString())
const base64 = CryptoJS.enc.Base64.stringify(md5_wordArr);
console.log(base64);
}())
AES
- 明文、秘钥和偏移向量一般先用诸如
CryptoJS.enc.Utf8.parse()
转成 WordArray 对象再传入,这样做得到结果与不转换直接传入是不一样的。 - 在加密时如果没有将明文进行 parse 而是直接传入的,那么在解密时,传入
toString()
的解析方式就是写默认的 CryptoJS.enc.Utf8
。
const CryptoJS = require('crypto-js');
function md5_test() {
console.log(CryptoJS.MD5('qwwqww').toString());
const wordArray = CryptoJS.enc.Utf8.parse('qwwqww');
CryptoJS.MD5(wordArray).toString();
console.log(CryptoJS.enc.Utf8.stringify(wordArray));
const md5_hasher = CryptoJS.algo.MD5.create();
md5_hasher.update('qww');
md5_hasher.update('qww');
const hash = md5_hasher.finalize();
console.log(hash.toString());
}
function aes_test_wordArray() {
const txt = 'qwwqww';
const key = '0123456789123456';
const iv_str = '0123456789123456';
const message = CryptoJS.enc.Utf8.parse(txt);
const secretPassphrase = CryptoJS.enc.Utf8.parse(key);
const iv = CryptoJS.enc.Utf8.parse(iv_str);
const encrypted = CryptoJS.AES.encrypt(message, secretPassphrase, {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv
}).toString();
console.log(`==>>: encrypted= ${encrypted}`);
const decrypted = CryptoJS.AES.decrypt(
encrypted,
secretPassphrase,
{
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv: iv
}
);
console.log(`decrypted=[${decrypted}]`);
console.log(`decrypted.toString()=[${decrypted.toString()}]`);
console.log(`decrypted.toString(CryptoJS.enc.Utf8)=[${decrypted.toString(CryptoJS.enc.Utf8)}]`);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
}
function aes_test_str() {
const txt = 'qwwqww';
const key = '0123456789123456';
const iv_str = '0123456789123456';
const encrypted_str = CryptoJS.AES.encrypt(txt, key, {
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv_str
}).toString();
console.log(`==>>: encrypted= ${encrypted_str}`);
const decrypted = CryptoJS.AES.decrypt( encrypted_str, key,
{
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
iv: iv_str
}
);
console.log(`decrypted=[${decrypted}]`);
console.log(`decrypted.toString()=[${decrypted.toString()}]`);
console.log(`decrypted.toString(CryptoJS.enc.Utf8)=[${decrypted.toString(CryptoJS.enc.Utf8)}]`);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
}
aes_test_wordArray();
aes_test_str();
jsencrypt
npm install jsencrypt
- 生成密钥对
openssl genrsa -out rsa_1024_priv.pem 1024
cat rsa_1024_priv.pem
---
$ cat rsa_1024_priv.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDbBEOrnpqVKvi16DGMAw2dRDUS+T05+4XY7J7DxAv2/qj4qAgk
PoUyDl9olkS2iHxmr2rRqLDNgFd2YkHnH//kWRN38opXJBzPU1friwSSP/7oW+kz
2ZpeCFy1TdJi6KYI6awZ2NTU24U8C2snzbfmjniw5dwX53I5vfaU6CBYfQIDAQAB
AoGBAM20uaJNez8zdxnTlgW9MGJPl9HUSo9VECfoRk30qpA4K5gwqkTwe8XB/haL
XMpccws3Iwr8d+Lr8jn8m41rXrjrP+irEnxzTYVTvjSaoNQxfICLGuD/mxali+e+
N1Y90NKUjOn3Xng+rDEp/mi4/cydoCe9FbFF89yIr4i/C4UBAkEA91caCUsrWa+D
M/vtv/rtu3edwKVUDMA5dGNB6SJZV+dJtENAztxcaO5Ssw56Wj64nWAkc5r7KqwH
XwHtA/5hYQJBAOKvTL0oNzQqCUQrAX25oz5luoRksBp1L/7wuExcktsaSsTalams
JaWAZUf1/Xaj6yBhRC79Zy8fvKdauiWCoJ0CQQDTsMXE7IQWx4P4ywcqR76Os4rd
5eJw41iJR6aSvWZQONE5KlJeefXRjSBUxfBsKsBhWXb5RszvMbtBUmmkAGUhAkBz
TwqxGdji7xMhmuK6Sojwf5zeJb2y57OPcnG9XkXRsFEiS7JfydKommRqs4XIKN+8
691cLcikHve/BZa05/q9AkEAlvTDF2W3JdqMPmTP87PpQYZRJyXSdYALquaq+T+h
Rruxjq1DZw+7LdF1q9SuGn3NYU5FsKMQGZ5ejWZP2IXQGw==
-----END RSA PRIVATE KEY-----
---
openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem
---
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDbBEOrnpqVKvi16DGMAw2dRDUS
+T05+4XY7J7DxAv2/qj4qAgkPoUyDl9olkS2iHxmr2rRqLDNgFd2YkHnH//kWRN3
8opXJBzPU1friwSSP/7oW+kz2ZpeCFy1TdJi6KYI6awZ2NTU24U8C2snzbfmjniw
5dwX53I5vfaU6CBYfQIDAQAB
-----END PUBLIC KEY-----
---
RSA
window = this;
const JSEncrypt = require('jsencrypt');
const public_key = '-----BEGIN PUBLIC KEY-----\n' +
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDbBEOrnpqVKvi16DGMAw2dRDUS\n' +
'+T05+4XY7J7DxAv2/qj4qAgkPoUyDl9olkS2iHxmr2rRqLDNgFd2YkHnH//kWRN3\n' +
'8opXJBzPU1friwSSP/7oW+kz2ZpeCFy1TdJi6KYI6awZ2NTU24U8C2snzbfmjniw\n' +
'5dwX53I5vfaU6CBYfQIDAQAB\n' +
'-----END PUBLIC KEY-----'
const crypt = new JSEncrypt();
crypt.setKey(public_key);
const text = 'qwwqww';
const enc_str = crypt.encrypt(text);
console.log(`enc_str=[${enc_str}]`);
const private_key = '-----BEGIN RSA PRIVATE KEY-----\n' +
'MIICXgIBAAKBgQDbBEOrnpqVKvi16DGMAw2dRDUS+T05+4XY7J7DxAv2/qj4qAgk\n' +
'PoUyDl9olkS2iHxmr2rRqLDNgFd2YkHnH//kWRN38opXJBzPU1friwSSP/7oW+kz\n' +
'2ZpeCFy1TdJi6KYI6awZ2NTU24U8C2snzbfmjniw5dwX53I5vfaU6CBYfQIDAQAB\n' +
'AoGBAM20uaJNez8zdxnTlgW9MGJPl9HUSo9VECfoRk30qpA4K5gwqkTwe8XB/haL\n' +
'XMpccws3Iwr8d+Lr8jn8m41rXrjrP+irEnxzTYVTvjSaoNQxfICLGuD/mxali+e+\n' +
'N1Y90NKUjOn3Xng+rDEp/mi4/cydoCe9FbFF89yIr4i/C4UBAkEA91caCUsrWa+D\n' +
'M/vtv/rtu3edwKVUDMA5dGNB6SJZV+dJtENAztxcaO5Ssw56Wj64nWAkc5r7KqwH\n' +
'XwHtA/5hYQJBAOKvTL0oNzQqCUQrAX25oz5luoRksBp1L/7wuExcktsaSsTalams\n' +
'JaWAZUf1/Xaj6yBhRC79Zy8fvKdauiWCoJ0CQQDTsMXE7IQWx4P4ywcqR76Os4rd\n' +
'5eJw41iJR6aSvWZQONE5KlJeefXRjSBUxfBsKsBhWXb5RszvMbtBUmmkAGUhAkBz\n' +
'TwqxGdji7xMhmuK6Sojwf5zeJb2y57OPcnG9XkXRsFEiS7JfydKommRqs4XIKN+8\n' +
'691cLcikHve/BZa05/q9AkEAlvTDF2W3JdqMPmTP87PpQYZRJyXSdYALquaq+T+h\n' +
'Rruxjq1DZw+7LdF1q9SuGn3NYU5FsKMQGZ5ejWZP2IXQGw==\n' +
'-----END RSA PRIVATE KEY-----';
const crypt_d = new JSEncrypt();
crypt_d.setKey(private_key);
const dec = crypt_d.decrypt(enc_str);
console.log(`dec_str=[${dec}]`);
const jscrypt = new JSEncrypt();
const rsa_key = jscrypt.getKey();
const public_key_ = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDbBEOrnpqVKvi16DGMAw2dRDUS+T05+4XY7J7DxAv2/qj4qAgkPoUyDl9olkS2iHxmr2rRqLDNgFd2YkHnH//kWRN38opXJBzPU1friwSSP/7oW+kz2ZpeCFy1TdJi6KYI6awZ2NTU24U8C2snzbfmjniw5dwX53I5vfaU6CBYfQIDAQAB';
rsa_key.setPublic(public_key_)
console.log(rsa_key.encrypt('qwwqww'));
enc_str=[go3BXf2TJ5Xsa5zXuGjr/enlpdLM5Y/f2Q7+BQETo4lA6iBlU+zd4aKzEHMF8WHZeZM7hjgY7jEeTzN4QyU6dAW92WZs23IA0ja7G1Ol0zzaQbJdbusPptYMvuQlncUsi2TpdJkXR/uxU9GTmA1/1p1AucLLzt3kFn832k2xDhI=]
dec_str=[qwwqww]
Invalid RSA public key
0e9810cabb7423cd4bbc5ef96ae7ab771f9d2f7d3b503df2d1fc799dff7a098923270a98edecfee365988baa119ec8703c11883246f8313f6028e0439794c8c9f6e39f63eb04a23f543c9f4625fb1255f19502a3e2c633d2599c7037da8a246a3720a669f66ac6c3083b9234e951df8083f66924b45b564fd42e282f8ccc56fe