- 引入的三方库
@yunkss/eftool
import { Base64Util, RSA,} from '@yunkss/eftool'
- 鸿蒙系统加解密库
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
具体代码实现
export class RSAUtil {
static readonly clientPublicKey: string = '公钥串'
static readonly clientPrivateKey: string = '私钥串'
1. 通过Uinit8Array类型生成密钥对
static async genKeyPairByUinit8Array() {
let publicKeyDataBlob: cryptoFramework.DataBlob =
{ data: new Uint8Array(buffer.from(RSAUtil.clientPublicKey, 'utf-8').buffer) };
let privateKeyDataBlob: cryptoFramework.DataBlob =
{ data: new Uint8Array(buffer.from(RSAUtil.clientPrivateKey, 'utf-8').buffer) };
let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
let keyPair = await rsaGenerator.convertKey(publicKeyDataBlob, privateKeyDataBlob);
console.info('convertKey success');
return keyPair;
}
2. 通过字符串直接生成密钥对
static async genKeyPairByString() {
let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
let keyPair = rsaGenerator.convertPemKeySync(RSAUtil.clientPublicKey,RSAUtil.clientPrivateKey)
console.info('convertKey success');
return keyPair;
}
加密操作
static async encryptData(text: string) {
let cipher = cryptoFramework.createCipher('RSA1024|PKCS1');
let pubKey: cryptoFramework.PubKey = (await RSAUtil.genKeyPairByString()).pubKey
await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null);
let encode = new util.TextEncoder();
let textDataBlob: cryptoFramework.DataBlob = { data: encode.encodeInto(text) };
let encryptDataBlob: cryptoFramework.DataBlob = await cipher.doFinal(textDataBlob);
let encryptString = await Base64Util.encodeToStr(encryptDataBlob.data)
return encryptString
}
解密操作
static async decryptData(text: string) {
let decoder = cryptoFramework.createCipher('RSA1024|PKCS1');
let priKey: cryptoFramework.PriKey = (await RSAUtil.genKeyPairByString()).priKey
await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, priKey, null);
let decoderBuffer = await Base64Util.decode(text)
let decryptData = await decoder.doFinal({ data: decoderBuffer});
let decryptString = buffer.from(decryptData.data).toString('utf-8');
return decryptString;
}
// RSA三方库加密
static async rsaEncryptData(text: string){
let pubKey = RSA.pemToStrKey(RSAUtil.clientPublicKey);
let encode = await RSA.encodePKCS1(text, pubKey);
let encryptText = encode.getDataRow()
return encryptText
}
// RSA三方库解密
static async rsaDecryptData(text: string){
let priKey = RSA.pemToStrKey(RSAUtil.clientPrivateKey);
let decode = await RSA.decodePKCS1(text, priKey);
let decryptText = decode.getDataRow();
return decryptText
}
}
测试代码
static async testRSA(){
let uuidData: string = '0c6e3ba-bd8e-4bed-b905-2ecb55f5dd29'
let uuid: string = await RSAUtil.encryptData(uuidData)
LogUtil.print('uuid加密数据',uuid)
let deuuid = await RSAUtil.decryptData(uuid)
LogUtil.print('uuid解密数据',deuuid)
LogUtil.print('---------------------')
let uuid_rsa = await RSAUtil.rsaEncryptData(uuidData)
LogUtil.print('uuid加密数据',uuid_rsa)
let deuuid_rsa = await RSAUtil.rsaDecryptData(uuid_rsa)
LogUtil.print('uuid解密数据',deuuid_rsa)
return
}