HarmonyOS 鸿蒙Next RSA私钥加密公钥解密
HarmonyOS 鸿蒙Next RSA私钥加密公钥解密
rsaDecrypt(message: string, callback) {
message = “aTc2-LTeM7Dsxo-2fuHeoC-k0pGU89jKm6xghw-gGllwyRz-q5ybwBc6wjIazfj14_3aWW1GzuG3mW6QUi0dFJEwX1HlGys7f7–RoZKRSRvNDAxrhMmslVLrLTONhZVhIm_9CHMqw9yjtYxAOwh-C_IVtt9euYfBzFqJTbgJgSWjHzFSUXG9za5ZyW8qt4YPTtXzCSu-uTUfpoxDoMGqS23rQTkiRACDGBVOC66jhTKMOgRfOcCXWqP6FuC25eRohHVMBc3r3AU_e5d-ObvbI_IaZGU4M6RV_Ptx0aQi3nA9rf94HlbouaAxy9xofHyIzXWt8vtAGt6PsEgAugikA”;
const PUBLICKEY_DEBUG = “MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiOpWXDGVxiq2ON0dDYMwdk86Rtwmzv1R9Date2Wi_1-QBfJL0_MbyrKNxhD840oMThOmFdLUsK8Aq8_akTzNQFg3bxiaf4vQxnDwyVWjeZD-7rFD4-NeX7pUPgxKpyHDUe8lmHceuHB_YktGN4Ewwk6-ofV0dOF8-BgrMtjZJ1dP_BgtR6haO013R61awGH1hJCz76477ykJzmbILbAcZ2Fr9Ki_UrJWOCFw1IQzadAcrPJk09N0p26KsM2pp2F-7TPVTKSsaXhQF5esN6aec5SwlNeIHu5rgCW93atHEvwdh6bBa3bKYzc2EMCJlgJF3UTgkPx3hyxL8Uut5KVGTwIDAQAB”;
const RSA512_PRIMES_2: string = ‘RSA1024|PRIMES_2’;
const RSA512_PKCS1: string = ‘RSA1024|PKCS1’;
let rsaGenerator = cryptoFramework.createAsyKeyGenerator(RSA512_PRIMES_2);
let cipher = cryptoFramework.createCipher(RSA512_PKCS1);
let that = new util.Base64Helper();
// let priKey = that.decodeSync(RSA_DECRYPT_KEY);
let pubKey = new Uint8Array(buffer.from(PUBLICKEY_DEBUG, ‘base64url’).buffer);
let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKey };
rsaGenerator.convertKey(pubKeyBlob, null, (err, keyPair) => {
if (err) {
Logger.error(TAG, “convertKey: error.” + (err as BusinessError).code);
return;
}
cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.pubKey, null, (err, data) => {
try {
// let newMessage = that.decodeSync(message);
let newMessage = new Uint8Array(buffer.from(message, ‘base64url’).buffer);
let input: cryptoFramework.DataBlob = { data: newMessage };
cipher.doFinal(input, (err, data) => {
if (err) {
Logger.error(TAG, “cipher doFinal.” + (err as BusinessError).code);
return;
}
Logger.info(TAG, "DecryptOutPut is " + data.data);
let result = this.uint8ArrayToString(data.data);
Logger.info(TAG, "result is " + result);
callback(result)
})
} catch (err) {
Logger.info(TAG, "cipher init error: " + (err as BusinessError).code);
return err;
}
})
})
}
更多关于HarmonyOS 鸿蒙Next RSA私钥加密公钥解密的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
rsa签名验签参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V13/crypto-rsa-sign-sig-verify-pkcs1-V13
rsa加密解密参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V13/crypto-rsa-asym-encrypt-decrypt-pkcs1-V13
私钥加密,公钥解密,公钥解密的部分,需要按照如下的代码进行编写:
//私钥加密
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: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 " + uint8ArrayToString(rawSignData?.data))
return rawSignData;
}
使用rsa签名,对要加密的数据长度有限制,小于等于密钥长度-11个字节
更多关于HarmonyOS 鸿蒙Next RSA私钥加密公钥解密的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统中,RSA私钥加密、公钥解密的过程并不符合非对称加密算法的常规用法,但技术上是可以实现的,关键在于正确理解和应用RSA算法的数学原理。
通常,RSA加密使用的是接收方的公钥,而解密则使用接收方的私钥。但如果你有特殊需求,想要用私钥加密、公钥解密,需要注意以下几点:
- 密钥生成:确保生成的RSA密钥对(包括公钥和私钥)是有效的,且私钥保密性良好。
- 加密过程:使用私钥对数据进行加密时,需要利用RSA算法的数学特性,这通常涉及到对数据的特定格式化和填充,以确保加密过程的安全性和有效性。
- 解密过程:使用公钥进行解密时,同样需要遵循RSA算法的规定,正确解析密文并恢复原始数据。
需要注意的是,这种非常规的加密方式可能会引入额外的安全风险,因此在实际应用中需要谨慎考虑。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。