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

关于RSA加解密的代码示例

HarmonyOS
2024-05-28 21:19:08
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
kaijunx

使用的核心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`); 
    }) 
}

适配的版本信息

  • IDE:DevEco Studio 4.1.3.220
  • SDK:HarmoneyOS 4.1.2.1
分享
微博
QQ
微信
回复
2024-05-29 22:25:27
相关问题
RSA导入外部密钥实现加解密
351浏览 • 1回复 待解决
实现一次非对称RSA非对称加解密
436浏览 • 1回复 待解决
加解密算法库框架使用
452浏览 • 1回复 待解决
多种加密方式实现加解密
415浏览 • 1回复 待解决
基于加解密算法框架规格问题
194浏览 • 1回复 待解决
如何进行不同规格AES加解密
196浏览 • 1回复 待解决
如何使用SM4CBC模式加解密
174浏览 • 1回复 待解决
SM4采用OFB模式进行加解密
310浏览 • 1回复 待解决
求大佬告知如何进行des加解密
422浏览 • 1回复 待解决
SM4 CBC模式加解密,有好方案吗?
448浏览 • 1回复 待解决
AES加解密长字符串是否需要分段
150浏览 • 1回复 待解决
使用32字节秘钥加解密后报错
454浏览 • 1回复 待解决
如何使用国密SM2算法进行加解密
859浏览 • 1回复 待解决
加解密HmacSha1 、HmacSha256、aes参考Demo
543浏览 • 1回复 待解决