赞
踩
npm install crypto-js
const CryptoJS = require('crypto-js');
function md5_test() {
// 字符串入参
console.log(CryptoJS.MD5('qwwqww').toString());
// WordArray 入参
const wordArray = CryptoJS.enc.Utf8.parse('qwwqww');
CryptoJS.MD5(wordArray).toString();
// wordArray 转换回 utf8 字符
console.log(CryptoJS.enc.Utf8.stringify(wordArray));
// 大文件 ,md5.update()
const md5_hasher = CryptoJS.algo.MD5.create();
md5_hasher.update('qww');
md5_hasher.update('qww');
const hash = md5_hasher.finalize();
console.log(hash.toString());
// 清除除之前的 update,调用 md5_hasher.reset()
// md5_hasher.reset();
// 输出:
// 261c63f27f816ed12125f275350d512e
// qwwqww
// 261c63f27f816ed12125f275350d512e
}
md5_test();
(function base64_test() {
// 3个字节转换为4个字节, 入参要为 wordArray, 就像后端入参为 ByteArray, 而不是字符串
console.log(CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('qww')));
let md5_wordArr = CryptoJS.MD5('qwwqww');
console.log(md5_wordArr.toString())
// md5 输出的字符串采用16进制表示, 两个字符为一个实际的字节
// 也就是6个字符代表3个实际的字节,是base 64后 会缩短为4个;也就是总长度会变为原来的 2/3
const base64 = CryptoJS.enc.Base64.stringify(md5_wordArr);
console.log(base64);
}())
// 输出:
// cXd3
// 261c63f27f816ed12125f275350d512e
// Jhxj8n+BbtEhJfJ1NQ1RLg==
CryptoJS.enc.Utf8.parse()
转成 WordArray 对象再传入,这样做得到结果与不转换直接传入是不一样的。toString()
的解析方式就是写默认的 CryptoJS.enc.Utf8
。const CryptoJS = require('crypto-js');
function md5_test() {
// 字符串入参
console.log(CryptoJS.MD5('qwwqww').toString());
// WordArray 入参
const wordArray = CryptoJS.enc.Utf8.parse('qwwqww');
CryptoJS.MD5(wordArray).toString();
// wordArray 转换回 utf8 字符
console.log(CryptoJS.enc.Utf8.stringify(wordArray));
// 大文件 ,md5.update()
const md5_hasher = CryptoJS.algo.MD5.create();
md5_hasher.update('qww');
md5_hasher.update('qww');
const hash = md5_hasher.finalize();
console.log(hash.toString());
// 清除除之前的 update,调用 md5_hasher.reset()
// md5_hasher.reset();
}
function aes_test_wordArray() {
// =============================使用 wordArray============================
// 加密
const txt = 'qwwqww';
// 注意 key, iv 如果不是 128、192 或 256 bit, 可以加密出密文来,但无法解密
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));
// 输出:
// ==>>: encrypted= 7rPjCUFm6h6LkY5TkjwmGQ==
// decrypted=[717777717777]
// decrypted.toString()=[717777717777]
// decrypted.toString(CryptoJS.enc.Utf8)=[qwwqww]
// qwwqww
}
function aes_test_str() {
// =============================使用 字符串============================
// 加密
const txt = 'qwwqww';
// 注意 key, iv 如果不是 128、192 或 256 bit, 可以加密出密文来,但无法解密
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_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));
// 输出:
// ==>>: encrypted= U2FsdGVkX199Zx4YIUHjkJfrKrB3TUA/E+Aqywe6GoU=
// decrypted=[717777717777]
// decrypted.toString()=[717777717777]
// decrypted.toString(CryptoJS.enc.Utf8)=[qwwqww]
// qwwqww
}
aes_test_wordArray();
aes_test_str();
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-----
---
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); // crypt.setPublicKey()
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();
// rsa_key.setPublic('publi_key', 'exp_值')
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。