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`); 
    }) 
}

适配的版本信息

  • IDE:DevEco Studio 4.1.3.220
  • SDK:HarmoneyOS 4.1.2.1
分享
微博
QQ
微信
回复
2024-05-29 22:25:27
相关问题
RSA导入外部密钥实现加解密
704浏览 • 1回复 待解决
HarmonyOS 有RSA加解密相关吗
339浏览 • 0回复 待解决
实现一次非对称RSA非对称加解密
973浏览 • 1回复 待解决
HarmonyOS RSA解密问题
343浏览 • 1回复 待解决
HarmonyOS 加解密 demo
392浏览 • 1回复 待解决
加解密问题定位指导
281浏览 • 1回复 待解决
HarmonyOS RSA如何用公钥解密
465浏览 • 0回复 待解决
HarmonyOS rsa解密速度过于缓慢
434浏览 • 1回复 待解决
加解密算法库框架使用
802浏览 • 1回复 待解决
多种加密方式实现加解密
930浏览 • 1回复 待解决
HarmonyOS 请提供RAS加解密文档
310浏览 • 1回复 待解决
基于加解密算法框架规格问题
638浏览 • 1回复 待解决
关于3des加解密问题,有人知道吗?
65浏览 • 1回复 待解决
RSA加密,使用自有私钥解密报错
120浏览 • 1回复 待解决
rsa 公钥解密初始化报错
96浏览 • 1回复 待解决
HarmonyOS 支持rsa使用公钥解密
381浏览 • 1回复 待解决
如何进行不同规格AES加解密
690浏览 • 1回复 待解决