HUKS的RSA加解密,关于RSA加解密的代码示例

关于RSA加解密的代码示例

HarmonyOS
2024-05-28 21:19:08
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
怎么不算呢

使用的核心API

@ohos.security.huks (通用密钥库系统)

核心代码解释

plainText(要加密的数据)、keyPair(密钥的别名,用于存放密钥,密钥不可见) 
//生成RSA密钥属性信息(以下是必须要有的,也可根据使用场景,参考密钥属性信息添加相关内容) 
function GetRsaGenerateProperties() { 
  let properties: Array<huks.HuksParam> = new Array(); 
  let index = 0; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 
    value: huks.HuksKeyAlg.HUKS_ALG_RSA 
  }; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 
    value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048 
  }; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_PURPOSE, 
    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | 
    huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT 
  } 
  return properties; 
} 
//RSA加密密钥属性信息 
function GetRsaEncryptProperties() { 
  let properties: Array<huks.HuksParam> = new Array(); 
  let index = 0; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 
    value: huks.HuksKeyAlg.HUKS_ALG_RSA 
  }; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 
    value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048 
  }; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_PURPOSE, 
    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT 
  } 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_PADDING, 
    value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5 
  } 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_DIGEST, 
    value: huks.HuksKeyDigest.HUKS_DIGEST_NONE 
  } 
  return properties; 
} 
//RSA解密密钥属性信息 
function GetRsaDecryptProperties() { 
  let properties: Array<huks.HuksParam> = new Array(); 
  let index = 0; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 
    value: huks.HuksKeyAlg.HUKS_ALG_RSA 
  }; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, 
    value: huks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048 
  }; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_PURPOSE, 
    value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT 
  } 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_PADDING, 
    value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5 
  } 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_DIGEST, 
    value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 
  } 
  return properties; 
} 
//检验密钥是否存在的属性信息 
function GetCheckProperties(){ 
  let properties: Array<huks.HuksParam> = new Array(); 
  let index = 0; 
  properties[index++] = { 
    tag: huks.HuksTag.HUKS_TAG_ALGORITHM, 
    value: huks.HuksKeyAlg.HUKS_ALG_RSA 
  }; 
  return properties; 
} 
//生成密钥 
async function GenerateRsaKey() { 
  let genProperties = GetRsaGenerateProperties(); 
  let options: huks.HuksOptions = { 
    properties: genProperties 
  } 
  await huks.generateKeyItem(KeyAlias, options) 
    .then((data) => { 
      console.info(`callback: generate RSA Key success, data = ${JSON.stringify(data)}`); 
    }).catch((error: BusinessError) => { 
      console.error(`callback: generate RSA Key failed`); 
    }) 
} 
//检验密钥是否存在 
async function checkKeyPair():Promise<boolean>{ 
  let isKeyExist = false;//检验密钥 
  let decryptOptions = GetCheckProperties() 
  let options: huks.HuksOptions = { 
    properties: decryptOptions, 
    inData: cipherData 
  } 
  try { 
    huks.isKeyItemExist(KeyAlias, options, (error, data) => { 
      if (error) { 
        console.error(`callback: isKeyItemExist failed`); 
        AlertDialog.show({message:"密钥存在状态=>"+`${isKeyExist}`}) 
      } else { 
        if (data !== null && data.valueOf() !== null) { 
          isKeyExist = data.valueOf(); 
          console.info(`callback: isKeyItemExist success, isKeyExist = ${isKeyExist}`); 
          AlertDialog.show({message:"密钥存在状态=>"+`${isKeyExist}`}) 
        } 
      } 
    }); 
  } catch (error) { 
    console.error(`callback: isKeyItemExist input arg invalid`); 
  } 
  return isKeyExist; 
} 
//使用密钥别名加密 
async function EncryptData() { 
  let encryptProperties = GetRsaEncryptProperties(); 
  let options: huks.HuksOptions = { 
    properties: encryptProperties, 
    inData: StringToUint8Array(plainText) 
  } 
  await huks.initSession(KeyAlias, options) 
    .then((data) => { 
      handle = data.handle; 
    }).catch((error: BusinessError) => { 
      console.error(`callback: init encryptdata failed`); 
    }) 
  await huks.finishSession(handle, options) 
    .then((data) => { 
      console.info(`callback: encrypt data success, data is ` + Uint8ArrayToString(data.outData as Uint8Array)); 
      cipherData = data.outData as Uint8Array; 
    }).catch((error: BusinessError) => { 
      console.error(`callback: encrypt data failed`); 
    }) 
} 
//使用密钥别名解密 
async function DeleteKey() { 
  let emptyOptions: huks.HuksOptions = { 
    properties: [] 
  } 
  await huks.deleteKeyItem(KeyAlias, emptyOptions) 
    .then((data) => { 
      console.info(`callback: delete data success`); 
    }).catch((error: BusinessError) => { 
      console.error(`callback: delete data failed`); 
    }) 
} 
//删除密钥 
async function DeleteKey() { 
  let emptyOptions: huks.HuksOptions = { 
    properties: [] 
  } 
  await huks.deleteKeyItem(KeyAlias, emptyOptions) 
    .then((data) => { 
      console.info(`callback: delete data success`); 
    }).catch((error: BusinessError) => { 
      console.error(`callback: delete data failed`); 
    }) 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.

适配的版本信息

  • IDE:DevEco Studio 4.1.3.220
  • SDK:HarmoneyOS 4.1.2.1
分享
微博
QQ
微信
回复
2024-05-29 22:25:27
相关问题
HarmonyOS 关于RSA公钥加解密问题
678浏览 • 1回复 待解决
RSA导入外部密钥实现加解密
1214浏览 • 1回复 待解决
HarmonyOS 是否有rsa加解密例子?
290浏览 • 1回复 待解决
HarmonyOS 有RSA加解密相关吗
854浏览 • 0回复 待解决
HarmonyOS中有RSA加解密相关吗?
303浏览 • 0回复 待解决
HarmonyOS 有没有相关rsa加解密内容
301浏览 • 1回复 待解决
HarmonyOS Native RSA 加解密实现咨询
286浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
1595浏览 • 1回复 待解决
HarmonyOS sm4、rsa加解密库及参考文档
1131浏览 • 1回复 待解决
HarmonyOS 3DES加解密示例
487浏览 • 1回复 待解决
HarmonyOS 关于DES加解密疑问
527浏览 • 1回复 待解决
HarmonyOS RSA解密问题?
271浏览 • 0回复 待解决
HarmonyOS RSA解密问题
804浏览 • 1回复 待解决
HarmonyOS 加解密问题
513浏览 • 1回复 待解决
HarmonyOS RSA解密数据
245浏览 • 1回复 待解决
HarmonyOS 加解密咨询
506浏览 • 1回复 待解决
HarmonyOS 加解密 demo
957浏览 • 1回复 待解决
HarmonyOS DEC加解密支持
413浏览 • 1回复 待解决
加解密问题定位指导
837浏览 • 1回复 待解决