HarmonyOS 接口参数的RSA签名+验签

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

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

您咨询的RSA 加签、验签,请参考以下代码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 = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDWsxfxxxxxxxxxxxxxxxxxxdey9/4ntONVyTuF0d0aCXQfrJf8uXsUGQdcgQXE79Z+/pq875Chq0EqH9Zdc/dFXo82MxkxkszNpRR4I7i+QWoM1rA5feszWIdMSW1wk4rG/AUU9TMHIGxZfcwyvVj+/5L09JVPNtgtWQIDAQAB";
  let priKeyVal = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBANazF8a3i5QIrUo4oA/49KbR+o5K2Dp117L3/ie041XJO4XR3RoJdB+sl/y5exQZB1yBBcTv1n7+mrzvkKGrQSof1l1xxxxxxxxxxxxxxxxxxxxxxxxxxxWsDl96zNYh0xJbXCTisb8BRT1MwcgbFl9zDK9WP7/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 keyPair = generator.generateKeyPairSync();
  let signData = signMessage(keyPair.priKey);
  console.error('signData : ' + signData);
  /*let verifyResult = verifyMessage(signData, keyPair.pubKey);
  if (verifyResult == true) {
    console.error('verify success');
  } else {
    console.error('verify failed');
  }*/
}
分享
微博
QQ
微信
回复
22h前
相关问题
HarmonyOS RSA签名相关
32浏览 • 1回复 待解决
RSA签名后,后端失败
212浏览 • 1回复 待解决
HarmonyOS 华为支付签名
238浏览 • 1回复 待解决
HarmonyOS HarmonyOS签名问题
31浏览 • 1回复 待解决
签名算法不支持RSA|PKCS8|SHA1
275浏览 • 1回复 待解决
huks sm2签名失败
252浏览 • 1回复 待解决
HarmonyOS SM3
30浏览 • 1回复 待解决
HarmonyOS sm2失败
70浏览 • 1回复 待解决
4.1.0(11) fluterr 无法通过
957浏览 • 1回复 待解决
HarmonyOS SM2加SM3摘要算法使用
519浏览 • 1回复 待解决
huks ECC指纹认证签名签报错
1460浏览 • 1回复 待解决