You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.7 KiB

1 year ago
import JSEncrypt from 'jsencrypt';
// 密钥对生成 http://web.chacuo.net/netrsakeypair
import { useGlobSetting } from '@/hooks/setting';
import {sm2} from 'sm-crypto'
1 year ago
const globSetting = useGlobSetting();
const publicKey = globSetting.rsaPublicKey;
// 前端不建议存放私钥 不建议解密数据 因为都是透明的意义不大
/** ??? 意义何在 */
const privateKey = globSetting.rsaPrivateKey;
// /**
// * RSA加密
// * @param txt 需要加密的数据
// * @returns
// */
// export function encrypt(txt: string) {
// const instance = new JSEncrypt();
// instance.setPublicKey(publicKey);
// return instance.encrypt(txt);
// }
// /**
// * RSA解密
// * @param txt 需要解密的数据
// * @returns
// */
// export function decrypt(txt: string) {
// const instance = new JSEncrypt();
// instance.setPrivateKey(privateKey);
// return instance.decrypt(txt);
// }
1 year ago
/**
* SM2公钥加密
* @param message
* @param publicKey (04)
* @returns
1 year ago
*/
export function encrypt(message: string) {
try {
const cipherData = sm2.doEncrypt(message, publicKey,1);
return cipherData;
} catch (error) {
throw new Error('SM2加密失败: ' + error.message);
}
1 year ago
}
/**
* SM2私钥解密
* @param ciphertext
* @param privateKey
* @returns
1 year ago
*/
export function decrypt(ciphertext: string) {
try {
//去掉密文的开头的04
const ciphertextHex = ciphertext.slice(2);
const decrypted = sm2.doDecrypt(ciphertextHex, privateKey,1);
return decrypted;
} catch (error) {
throw new Error('SM2解密失败: ' + error.message);
}
}