中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
function rsaKeyBase64(puk: string, prk: string) { // 创建一个AsyKeyGenerator实例 let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA2048'); let base64 = new util.Base64Helper(); if (puk != '' && prk != '') { let keyPair = rsaGenerator.convertKeySync({ data: base64.decodeSync(puk) }, { data: base64.decodeSync(prk) }); return keyPair; } else if (prk) { let keyPair = rsaGenerator.convertKeySync(null, { data: base64.decodeSync(prk) }); return keyPair; } else if (prk) { let keyPair = rsaGenerator.convertKeySync({ data: base64.decodeSync(puk) }, null); return keyPair; } else { let keyPair = rsaGenerator.generateKeyPairSync(); return keyPair; } } //私钥加密 async function signMessagePromise(priKey: cryptoFramework.PriKey) { let signAlg = "RSA1024|PKCS1|NoHash|OnlySign"; let signer = cryptoFramework.createSign(signAlg); await signer.init(priKey); let signData = await signer.sign({ data: RSAUtil.stringToUint8Array("私钥加密") }); return signData; } // 公钥解密 async function verifyMessagePromise(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) { let verifyAlg = "RSA1024|PKCS1|NoHash|Recover"; let verifier = cryptoFramework.createVerify(verifyAlg); await verifier.init(pubKey); let rawSignData = await verifier.recover(signMessageBlob); console.log("rsa puk data " + RSAUtil.uint8ArrayToString(rawSignData?.data)) return rawSignData; }