HarmonyOS 鸿蒙Next接口参数的【RSA签名+验签】

发布于 1周前 作者 itying888 来自 鸿蒙OS

HarmonyOS 鸿蒙Next接口参数的【RSA签名+验签】 关于RSA签名+验签的问题,可以提供相应的鸿蒙代码吗,传参和返回参数最好都是string类型

2 回复

请参考以下代码demo实例

import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer, util } from '@kit.ArkTS';

@Entry
@Component
struct RSASignVerifyCrypto {
  @State message: string = '点击开始';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            SignVerifyCrypto()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

// 完整的明文被拆分为input1和input2
let input1: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan1", 'utf-8').buffer) };
let input2: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("This is Sign test plan2", 'utf-8').buffer) };

function signMessage(priKey: cryptoFramework.PriKey) {
  let signAlg = "RSA1024|PKCS1|SHA256";
  let signer = cryptoFramework.createSign(signAlg);
  signer.initSync(priKey);
  signer.updateSync(input1); // 如果明文较短,可以直接调用sign接口一次性传入
  let signData = signer.signSync(input2);
  return signData;
}

function verifyMessage(signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) {
  let verifyAlg = "RSA1024|PKCS1|SHA256";
  let verifier = cryptoFramework.createVerify(verifyAlg);
  verifier.initSync(pubKey);
  verifier.updateSync(input1); // 如果明文较短,可以直接调用verify接口一次性传入
  let res = verifier.verifySync(input2, signMessageBlob);
  console.error("verify result is " + res);
  return res;
}

// 生成RSA密钥对
function genKeyPairByData(pubKeyData: Uint8Array, priKeyData: Uint8Array) {
  let pubKeyBlob: cryptoFramework.DataBlob = { data: pubKeyData };
  let priKeyBlob: cryptoFramework.DataBlob = { data: priKeyData };
  let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
  let keyPair = rsaGenerator.convertKeySync(pubKeyBlob, priKeyBlob);
  console.error('convertKey success');
  return keyPair;
}

function SignVerifyCrypto() {
  let keyGenAlg = "RSA1024";

  let pubKeyVal = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWsxfGt4uUCK1KOKAP+PSm0fqOStg6ddey9/4ntONVyTuF0d0aCXQfrJf8uXsUGQdcgQXE79Z+/pq875Chq0EqH9Zdc/dFXo82MxkxkszNpRR4I7i+QWoM1rA5feszWIdMSW1wk4rG/AUU9TMHIGxZfcwyvVj+/5L09JVPNtgtWQIDAQAB";
  let priKeyVal = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANazF8a3i5QIrUo4oA/49KbR+o5K2Dp117L3/ie041XJO4XR3RoJdB+sl/y5exQZB1yBBcTv1n7+mrzvkKGrQSof1l1z90VejzYzGTGSzM2lFHgjuL5BagzWsDl96zNYh0xJbXCTisb8BRT1MwcgbFl9zDK9WP7/kvT0lU822C1ZAgMBAAECgYEAmG+RywpYdKNwfgkURCLreWIOtmaXfXJb0nrXHdQFsMvukgW+KRVbOH3vb4U1yMA4hMoqkXgD4CjfLpQdKVwRKAxIpUXA047pUcqx65wbszASVZplwS3aWxiPxPgQU7HGiE1vhjzbX/YXBS0OUx2J+J8chI7NY+LVVOg5gpxRv+0CQQD/ntQNK4T0h5Sh6NsUUcRmZyxuR2Q+ScgginLRq5azXMYFvtpP4+MlIDmf/GvTi8bK+ImPus1qUVXPhpRuzPMbAkEA1wS1eTngqqi3n5gISulQ9JJRMJ/CxyS7BrW233OFl6tO21qh+EUGz60DUaECPO7MsQyKEdyzR3HI+J+Z/Ja0mwJBAL7KudOqq+4oVFQVDZA5B7K3R354YuXrBCjlrZW50R3HHTakoSYIHj5Tsy8qpQCcz6AnqeVRtIiqdLYU6S1aZAkCQQCY/y/GD8nuhVkLhZm4/CXvsUF2UOe+3kL6dkim3UOc9XeKHAaOa0d6dMicx+2Yv+8EuEByj1E+MBfp2V8v3WirAkAe2wHm8Ub283muQ0ILY8oRNOpOHQM5M3uVVkDAScdsZTfoKXKZ7f00zZQtVrrxtrcqTfzDHZ6tA7bP/j1HuKe4";

  let base64 = new util.Base64Helper();
  let priKey = base64.decodeSync(priKeyVal)
  let pubKey = base64.decodeSync(pubKeyVal);

  let keyPair = genKeyPairByData(pubKey, priKey)

  let signData = signMessage(keyPair.priKey);
  console.error('signData : ' + signData);
}

更多关于HarmonyOS 鸿蒙Next接口参数的【RSA签名+验签】的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对HarmonyOS(鸿蒙)Next接口参数的【RSA签名+验签】问题,以下是专业且简洁的回答:

在HarmonyOS中,对于Next接口参数的RSA签名与验签操作,通常涉及以下几个关键步骤:

  1. 生成密钥对:首先,需要生成一对RSA密钥,包括公钥和私钥。这对密钥将用于后续的签名和验签过程。

  2. 签名:在发送数据前,使用私钥对数据进行签名。签名过程是将数据通过哈希函数生成摘要,然后使用私钥对摘要进行加密,生成签名。

  3. 发送签名数据:将签名后的数据与原始数据一起发送给接收方。

  4. 验签:接收方收到数据后,使用公钥对签名进行验签。验签过程是将接收到的数据通过相同的哈希函数生成摘要,然后使用公钥对签名进行解密,得到原始摘要。如果两个摘要相同,则验签成功,表示数据未被篡改。

在HarmonyOS的具体实现中,这些操作可能依赖于系统提供的加密库或API。开发者需要确保正确配置和使用这些库或API,以实现安全的RSA签名与验签。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部