HarmonyOS SM2密钥问题

let keyGenAlg = "SM2_256";
let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
let keyPairTest = await generator.generateKeyPair();
console.log('公钥',keyPairTest.pubKey.getEncoded().data)
console.log('公钥原始',JSON.stringify(keyPairTest.pubKey))
console.log('私钥',keyPairTest.priKey.getEncoded().data)

我们根据以上代码生成了密钥进行了查看,按道理SM2 256意味着密钥长度为256也就是32个字节

那为什么以上方法生成的公钥是91,私钥是51呢。还是说我们的识别方法不对?

当我们传入32位的密钥以

let keyPair = generator.convertKeySync(null, privateKeyBlob);

的方式去获取密钥时又为什么会报错呢

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

参考sm2-256 私钥是64位的demo:

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

@Entry
@Component
export struct SM2256 {
  aboutToAppear(): void {

  }

  // 加密消息
  async encryptMessagePromise(publicKey: cryptoFramework.PubKey, plainText: cryptoFramework.DataBlob) {
    let cipher = cryptoFramework.createCipher('SM2_256|SM3');
    await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, publicKey, null);
    let encryptData = await cipher.doFinal(plainText);
    return encryptData;
  }

  // 解密消息
  async decryptMessagePromise(privateKey: cryptoFramework.PriKey, cipherText: cryptoFramework.DataBlob) {
    let decoder = cryptoFramework.createCipher('SM2_256|SM3');
    await decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, privateKey, null);
    let decryptData = await decoder.doFinal(cipherText);
    return decryptData;
  }

  async convertStrToPubKey(keyStr: string): Promise<cryptoFramework.PubKey> {
    const pubKeyStr = keyStr.startsWith('04') ? keyStr.slice(2) : keyStr
    const pkPart1 = pubKeyStr.slice(0, pubKeyStr.length / 2)
    const pkPart2 = pubKeyStr.slice(pubKeyStr.length / 2)
    const pk: cryptoFramework.Point = {
      x: BigInt('0x' + pkPart1),
      y: BigInt('0x' + pkPart2),
    }
    const pubKeySpec: cryptoFramework.ECCPubKeySpec = {
      params: cryptoFramework.ECCKeyUtil.genECCCommonParamsSpec('NID_sm2'),
      pk: pk,
      algName: 'SM2',
      specType: cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC
    }
    const keypairGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(pubKeySpec)
    return await keypairGenerator.generatePubKey()
  }

  async convertStrToPriKey(keyStr: string): Promise<cryptoFramework.PriKey> {
    const sk = BigInt('0x' + keyStr)
    const priKeySpec: cryptoFramework.ECCPriKeySpec = {
      params: cryptoFramework.ECCKeyUtil.genECCCommonParamsSpec('NID_sm2'),
      sk: sk,
      algName: 'SM2',
      specType: cryptoFramework.AsyKeySpecType.PRIVATE_KEY_SPEC
    }
    const keypairGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(priKeySpec)
    return await keypairGenerator.generatePriKey()
  }

  build() {
    Row() {
      Button("createAsyKeyGenerator").onClick(async () => {
        let generator = cryptoFramework.createAsyKeyGenerator("SM2_256");
        let keyPair = generator.generateKeyPairSync();
        let priKeyData = keyPair.priKey.getEncoded().data
        let pubKeyData = keyPair.pubKey.getEncoded().data

        let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("123", 'utf-8').buffer) };
        let encryptText = await this.encryptMessagePromise(keyPair.pubKey, plainText)
        let decryptText = await this.decryptMessagePromise(keyPair.priKey, encryptText)
      })

      Button("网站生成的测试").onClick(async () => {
        // 64位
        let priKey = "75085381c66ac32b452...6ba76a0c5ec495d050d2816eba7bd5e6ce9"
        // 128 位
        let pubKey =
          "04a1ed2cb2f678994badca91af895b29db610771...d971a93cd7a2179b56ffb9c264919be36ad603c223bedc16ceb16f0dfbcd869c5b15ecf0c4"

        let plainText: cryptoFramework.DataBlob = { data: new Uint8Array(buffer.from("123", 'utf-8').buffer) };
        const pk = await this.convertStrToPubKey(pubKey)
        const sk = await this.convertStrToPriKey(priKey)

        let encryptText = await this.encryptMessagePromise(pk, plainText)
        console.log('123加密后的信息'+JSON.stringify(encryptText))
        let decryptText = await this.decryptMessagePromise(sk, encryptText)
        console.log('123解密后的信息'+JSON.stringify(decryptText))
        let messageDecrypted = buffer.from(decryptText.data).toString('utf-8');
        console.log(messageDecrypted)
      })
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS SM2密钥对转换失败
679浏览 • 1回复 待解决
HarmonyOS 生成SM2密钥对报错
87浏览 • 1回复 待解决
HarmonyOS Sm2和DES加解密问题
517浏览 • 1回复 待解决
HarmonyOS SM2数据签名
20浏览 • 1回复 待解决
HarmonyOS SM2/SM4结合加解密
36浏览 • 1回复 待解决
HarmonyOS sm2验签失败
76浏览 • 1回复 待解决
HarmonyOS SM2加密算法
33浏览 • 1回复 待解决
huks sm2签名验签失败
259浏览 • 1回复 待解决
HarmonyOS SM2公钥PEM读取接口
25浏览 • 1回复 待解决
HarmonyOS SM2SM4国密加解密使用demo
312浏览 • 1回复 待解决
HarmonyOS SM2加签验签SM3摘要算法使用
525浏览 • 1回复 待解决
HarmonyOS 指定私钥生成SM2秘钥的方法
24浏览 • 1回复 待解决
HarmonyOS 国密SM2转换公钥失败
42浏览 • 1回复 待解决
如何使用国密SM2算法进行加解密
4478浏览 • 1回复 待解决