mirror of
https://gitee.com/dapppp/ruoyi-plus-vben5.git
synced 2026-05-10 06:42:08 +08:00
feat: API加密 前端已经实现RSA/SM2 AES/SM4
This commit is contained in:
28
packages/utils/src/encryption/impl/aes.ts
Normal file
28
packages/utils/src/encryption/impl/aes.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import CryptoJS from 'crypto-js';
|
||||
|
||||
import { BaseSymmetricEncryption } from '../base';
|
||||
|
||||
/**
|
||||
* AES 实现
|
||||
*/
|
||||
export class AesEncryption extends BaseSymmetricEncryption {
|
||||
override decrypt(data: string, key: string): string {
|
||||
// 必须格式化字符串才能正常使用
|
||||
const aesKey = CryptoJS.enc.Utf8.parse(key);
|
||||
const decrypted = CryptoJS.AES.decrypt(data, aesKey, {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7,
|
||||
});
|
||||
return decrypted.toString(CryptoJS.enc.Utf8);
|
||||
}
|
||||
|
||||
override encrypt(data: string, key: string): string {
|
||||
// 必须格式化字符串才能正常使用
|
||||
const aesKey = CryptoJS.enc.Utf8.parse(key);
|
||||
const encrypted = CryptoJS.AES.encrypt(data, aesKey, {
|
||||
mode: CryptoJS.mode.ECB,
|
||||
padding: CryptoJS.pad.Pkcs7,
|
||||
});
|
||||
return encrypted.toString();
|
||||
}
|
||||
}
|
||||
30
packages/utils/src/encryption/impl/rsa.ts
Normal file
30
packages/utils/src/encryption/impl/rsa.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import JSEncrypt from 'jsencrypt';
|
||||
|
||||
import { BaseAsymmetricEncryption } from '../base';
|
||||
|
||||
/**
|
||||
* RSA 实现
|
||||
*/
|
||||
export class RsaEncryption extends BaseAsymmetricEncryption {
|
||||
override decrypt(str: string): string {
|
||||
const instance = new JSEncrypt();
|
||||
instance.setPrivateKey(this.privateKey);
|
||||
const ret = instance.decrypt(str);
|
||||
|
||||
if (ret === false) {
|
||||
throw new Error('RsaEncryption decrypt error');
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
override encrypt(str: string): string {
|
||||
const instance = new JSEncrypt();
|
||||
instance.setPublicKey(this.publicKey);
|
||||
const ret = instance.encrypt(str);
|
||||
if (ret === false) {
|
||||
throw new Error('RsaEncryption encrypt error');
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
33
packages/utils/src/encryption/impl/sm2.ts
Normal file
33
packages/utils/src/encryption/impl/sm2.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/* eslint-disable no-console */
|
||||
import { sm2 } from 'sm-crypto';
|
||||
|
||||
import { BaseAsymmetricEncryption } from '../base';
|
||||
|
||||
/**
|
||||
* SM2 实现
|
||||
* 注意生成的公钥必须为04开头 或者使用下面的generateSm2KeyPair生成
|
||||
* @see https://tool.hiofd.com/sm2-key-gen/ 这里可以生成04开头的SM2密钥对
|
||||
*/
|
||||
export class Sm2Encryption extends BaseAsymmetricEncryption {
|
||||
override decrypt(str: string): string {
|
||||
return sm2.doDecrypt(str, this.privateKey);
|
||||
}
|
||||
|
||||
override encrypt(str: string): string {
|
||||
return sm2.doEncrypt(str, this.publicKey);
|
||||
}
|
||||
}
|
||||
|
||||
export function generateSm2KeyPair() {
|
||||
const { privateKey, publicKey } = sm2.generateKeyPairHex();
|
||||
return {
|
||||
privateKey,
|
||||
publicKey,
|
||||
};
|
||||
}
|
||||
|
||||
export function logSm2KeyPair() {
|
||||
const { privateKey, publicKey } = generateSm2KeyPair();
|
||||
console.log('privateKey', privateKey);
|
||||
console.log('publicKey', publicKey);
|
||||
}
|
||||
37
packages/utils/src/encryption/impl/sm4.ts
Normal file
37
packages/utils/src/encryption/impl/sm4.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import CryptoJS from 'crypto-js';
|
||||
import { sm4 } from 'sm-crypto';
|
||||
|
||||
import { BaseSymmetricEncryption } from '../base';
|
||||
|
||||
/**
|
||||
* SM4 实现
|
||||
*/
|
||||
export class Sm4Encryption extends BaseSymmetricEncryption {
|
||||
override decrypt(data: string, key: string): string {
|
||||
this.checkKey(key);
|
||||
const keyHex = CryptoJS.enc.Hex.stringify(CryptoJS.enc.Utf8.parse(key));
|
||||
return sm4.decrypt(data, keyHex);
|
||||
}
|
||||
|
||||
override encrypt(data: string, key: string): string {
|
||||
this.checkKey(key);
|
||||
/**
|
||||
* 转hex字符串
|
||||
* encrypt方法的key需要为`16进制字符串`而非`原始字符串`
|
||||
* 比如字符串ab a为0x61 b为0x62 转字符串为 6162
|
||||
*/
|
||||
const keyHex = CryptoJS.enc.Hex.stringify(CryptoJS.enc.Utf8.parse(key));
|
||||
|
||||
return sm4.encrypt(data, keyHex);
|
||||
}
|
||||
|
||||
/**
|
||||
* key长度只能为16位字符串
|
||||
* @param key key
|
||||
*/
|
||||
private checkKey(key: string) {
|
||||
if (key.length !== 16) {
|
||||
throw new Error('SM4 key must be 16 bytes');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user