RSA加密---jsencrypt
在线生成公钥私钥
前提:npm install jsencrypt
import { JSEncrypt } from 'jsencrypt'
var JSEncryptKey={
// 公钥
publicKey :"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArflOnHHKOXRYIsh/Lurq5lhRmsYKcNRjvib8y7xs0HdPXWp99sl2bQy1kRm3vbClKpOkB/TohhIEp3dk6GsS2c8HV3Uuulkdy3kYNzTnh4bTefUBMu3Y9ZXO5S07wr6MozHN+v7RsqHlDPLBP2tHv06EgYjbwBtKII92cjgFN/AuLj9O9P9bMoHNDFJqyZsmsplXf6FXP3b4gyOjZWtX0QAuLvWiddAACEuF74Ej4ckTI9ThgDTNjOHfM1DjNaO2T4zS6iA5DHn7dQKQ82Cree/hXvzvr2DsjvgkSOvV5L/Qivrs7FTE08KLTCYgfgxGBH22X87BMy9fDzWGzAICuwIDAQAB" ,
// 私钥
privateKey :"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCt+U6ccco5dFgiyH8u6urmWFGaxgpw1GO+JvzLvGzQd09dan32yXZtDLWRGbe9sKUqk6QH9OiGEgSnd2ToaxLZzwdXdS66WR3LeRg3NOeHhtN59QEy7dj1lc7lLTvCvoyjMc36/tGyoeUM8sE/a0e/ToSBiNvAG0ogj3ZyOAU38C4uP070/1sygc0MUmrJmyaymVd/oVc/dviDI6Nla1fRAC4u9aJ10AAIS4XvgSPhyRMj1OGANM2M4d8zUOM1o7ZPjNLqIDkMeft1ApDzYKt57+Fe/O+vYOyO+CRI69Xkv9CK+uzsVMTTwotMJiB+DEYEfbZfzsEzL18PNYbMAgK7AgMBAAECggEBAKAy/gxZS9n0JGSDM3pEn+FUWnCnBF45dU9YH1iMPbcI5nAi67MgeIiaN2dRwzxNOQonwdgB0UwDs31yF91dG5KtbRk/srOt9B21eHL9qGXH5VxcG6qGEk6ykD3AlVMSm/Z6mi4SD47qPTzbHNAFPaAP8TxGhfQ0LOImuO8cXtHON5aV/oti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(msg)
encrypt.setPublicKey(this.publicKey)
const encryptMsg = encrypt.encrypt(msg)
return encryptMsg
},
decrypt:function(msg){
const decrypt = new JSEncrypt()
decrypt.setPrivateKey(this.privateKey)
const decryptMsg = decrypt.decrypt(msg)
return decryptMsg
}
}
export {JSEncryptKey}
RSA加密(随机生成密钥版):
import { JSEncrypt } from 'jsencrypt'
var jsencrypt=new JSEncrypt()
//RSA加密
let RSAKey ={
getKeys:function(){
const keypair = jsencrypt.getKey();
const publicKey = keypair.getPublicKey();
const privateKey = keypair.getPrivateKey();
return {
publicKey:publicKey,
privateKey:privateKey
}
},
encrypt: function (publicKey,msg) {
jsencrypt.setPublicKey(publicKey)
return jsencrypt.encrypt(msg)
},
decrypt: function (privateKey,msg) {
jsencrypt.setPrivateKey(privateKey)
return jsencrypt.decrypt(msg)
},
}
以下加密方法使用前提:
npm i crypto-js
AES加密:
import {enc, mode, AES, pad,DES,MD5} from "crypto-js";
var CryptoAESKey={
key:'1234567812345678',
iv:'12345678912345678',
encrypt:function(msg){
var data = enc.Utf8.parse(msg);
var encrypted = AES.encrypt(data, this.key, {
iv: this.iv,
mode: mode.CBC,
padding:pad.ZeroPadding
});
return encrypted.toString();
},
decrypt:function(msg){
var decrypted = AES.decrypt(msg, this.key, {
iv: this.iv,
mode: mode.CBC,
padding:pad.ZeroPadding
});
return decrypted.toString(enc.Utf8);
}
}
DES加密:
import {enc, mode, AES, pad,DES,MD5} from "crypto-js";
var CryptoDESKey={
key:'1234567812345678',
// iv:'12345678912345678',
// .net DES加密的默认模式---密文分组链接方式
encryptCBC:function(msg){
var encrypted = DES.encrypt(msg, this.key, {
iv: this.key,
mode: mode.CBC,
padding:pad.Pkcs7
});
return encrypted.toString();
},
decryptCBC:function(msg){
var decrypted = DES.decrypt(msg, this.key, {
iv: this.key,
mode: mode.CBC,
padding:pad.Pkcs7
});
return decrypted.toString(enc.Utf8);
},
// java DES加密的默认模式---电子密本方式
encryptECB:function(msg){
var encrypted = DES.encrypt(msg, this.key, {
mode: mode.ECB,
padding:pad.Pkcs7
});
return encrypted.toString();
},
decryptECB:function(msg){
var decrypted = DES.decrypt(msg, this.key, {
mode: mode.ECB,
padding:pad.Pkcs7
});
return decrypted.toString(enc.Utf8);
}
}
MD5加密:
import {enc, mode, AES, pad,DES,MD5} from "crypto-js";
function MD5Key(msg){
let data=MD5(msg)
return data.toString()
}
base64加密
import {enc, mode, AES, pad,DES,MD5} from "crypto-js";
var Base64Key={
encrypt:function (msg){
var data=enc.Utf8.parse(msg)
return enc.Base64.stringify(data)
},
decrypt:function (msg){
var data=enc.Base64.parse(msg)
return data.toString(enc.Utf8)
}
}
以下加密方法使用前提:
npm i sm-crypto
SM2 加密:
在线生成密钥
var SM2Key={
// publicKey:SM2.generateKeyPairHex().publicKey , //公钥
// privateKey:SM2.generateKeyPairHex().privateKey , //私钥
publicKey:'04'+'2E66D3CA2FBE23D0C54D79BA738883FB979D4C66267E7D9AA4D4BDE59E26E90037B550FEDC3ED517CA0C109E7F82B7677CAE1BC641718F4E96B427CF37CA6D0B' , //公钥
privateKey:'8A4B3BAB9AB3131FE130C550D353104EB0B9A6B947F6D0F8B700E4D228078561' , //私钥
// compressedPublicKey :SM2.compressPublicKeyHex(publicKey) , //压缩公钥
// SM2.comparePublicKeyHex(publicKey, compressedPublicKey) 判断压缩前后公钥是否等价
// verifyResult :SM2.verifyPublicKey(this.publicKey), //验证公钥
cipherMode : 1, // 1 - C1C3C2,0 - C1C2C3,默认为1
encrypt:function (msg) {
// console.log(this.publicKey);
return SM2.doEncrypt(msg, this.publicKey, this.cipherMode)
},
decrypt:function(msg){
// console.log( this.privateKey);
return SM2.doDecrypt(msg, this.privateKey, this.cipherMode)
}
}
SM3加密
const SM3 = require('sm-crypto').sm3
var SM3Key={
encryptNormal:function (msg) {
let data=SM3(msg)
return data
},
encryptUser:function(msg){
let key='daac25c1512fe50f79b0e4526b93f5c0e1460cef40b6dd44af13caec62e8c60e0d885f3c6d6fb51e530889e6fd4ac743a6d332e68a0f2a3923f42585dceb93e9'
let data=SM3(msg,{key:key})
return data
}
}
SM4加密
const SM4 = require('sm-crypto').sm4
var SM4Key={
tips:'温馨提示-----', // 可以为 utf8 串或字节数组
key : '0123456789abcdeffedcba9876543210', //可以为 16 进制串或字节数组,要求为 128 比特
encrypt:function (msg) {
return SM4.encrypt(msg, this.key) 加密,默认输出 16 进制字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
return SM4.encrypt(msg, this.key,{padding: 'none'}) // 加密,不使用 padding
return SM4.encrypt(msg, this.key,{padding: 'none', output: 'array'}) // 加密,不使用 padding,输出为字节数组
return SM4.encrypt(msg, this.key,{mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 加密,cbc 模式
},
decrypt:function(msg){
return SM4.decrypt(msg, this.key) 解密,默认输出 utf8 字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
return SM4.decrypt(msg, this.key,{padding: 'none'}) 解密,不使用 padding
return SM4.decrypt(msg, this.key, {padding: 'none', output: 'array'}) // 解密,不使用 padding,输出为字节数组
return SM4.decrypt(msg, this.key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式
}
}
使用:
import {JSEncryptKey,CryptoAESKey,CryptoDESKey,MD5Key,Base64Key,SM2Key,SM3Key,SM4Key,RSAKey} from '../assets/js/util'
export default {
data() {
return {
msg:'123456'
}
},
mounted() {
// RSA加密:
console.log(JSEncryptKey.encrypt(this.msg));
console.log(JSEncryptKey.decrypt('DyGj56bzSaycZHoJ58WAhh1yCcOcDbgSMbygHodjgm035aual9pcyOkwLzNsHmU1gGHeysCo58UCGN67CaNu5FobMR9/J2uGNNSZXU34aZC09K/I6KYlYz3vXBRuVJAVX7t4nxAw6KUJ+TI2WdwLw3e3/jKwBky9oQvK/uzytfLN+ZxCjyqfrbxzu7NmWgTilhSc/FEhtm5Kmiwl3gY0CAzJxAaBHn9uVt5IsPoaeR1Qt/IjkCNvcUU215xTjHYIoXCwnHyOIFXRBa4YRreQFOvb6g7pzDrb9XXiree0SEXHLnSbM4iTsiMNjOPTkZ+ftBXmi0A3BjBFVpBoLeDZ/g=='));
console.log(`--------RSA加密---------`);
// AES加密
let str='my AES';
let strs=CryptoAESKey.encrypt(str)
console.log(strs);
console.log(CryptoAESKey.decrypt(strs));
console.log(`--------AES加密---------`);
// DES加密:
//.net
let str1=CryptoDESKey.encryptCBC(str)
console.log(str1);
console.log(CryptoDESKey.decryptCBC(str1));
console.log(`--------DES加密 .net---------`);
//java
let str2=CryptoDESKey.encryptECB(str)
console.log(str2);
console.log(CryptoDESKey.decryptECB(str2));
console.log(`--------DES加密 java---------`);
// MD5
console.log(MD5Key(str));
console.log(`--------MD5加密---------`);
// BASE64
let str3=Base64Key.encrypt(str)
console.log(str3);
console.log(Base64Key.decrypt(str3));
console.log(`--------BASE64加密---------`);
// SM2Key
let str4=SM2Key.encrypt('0000000000000')
console.log(str4);
console.log(SM2Key.decrypt(str4));
console.log(`--------SM2Key加密---------`);
// SM3Key
let str5=SM3Key.encryptNormal(str)
console.log(str5);
let str55=SM3Key.encryptUser(str)
console.log(str55);
console.log(`--------SM3Key加密---------`);
// SM2Key
let str6=SM4Key.encrypt(str)
console.log(str6);
console.log(SM4Key.decrypt(str6));
console.log(`--------SM4Key加密---------`);
//RSAkey
var key =RSAKey.getKeys()
var publicKey = key.publicKey
var privateKey = key.privateKey
let str7=RSAKey.encrypt(publicKey,str)
console.log(str7);
console.log(RSAKey.decrypt(privateKey,str7));
},
}