HarmonyOS 鸿蒙Next使用国密SM2_256,密钥转成Uint8Array后,convertKey报错
HarmonyOS 鸿蒙Next使用国密SM2_256,密钥转成Uint8Array后,convertKey报错
【关键字】
国密 / SM2_256 / 加解密 / Uint8Array / convertKey
【问题描述】
按照官方的SM2_256加解密代码进行配置,从Android项目拷贝密钥,并将密钥转成Uint8Array后,convertKey报错,错误信息如下:
ConvertEcPubKey: d2iECPUBKEY fail.
com.examp…lication E HcfPrintOpensslError: [Openssl]: engine fail, error code = 109052059, error string = error:0680009B:asn1 encoding routines::too long
com.examp…lication E ConvertKeyAsyncWorkProcess: convert key fail.
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
问题代码如下:
let generator = cryptoFramework.createAsyKeyGenerator("");
generator.convertKey(pubKeyBlob, null, (error, data) => {
})
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
【解决方案】
请参考如下代码进行配置:
function HexStrTouint8Array(data: string): Uint8Array {
return new Uint8Array(buffer.from(data, ‘hex’).buffer);;
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
async init(): Promise<cryptoFramework.KeyPair | null> {
let puk = “04D03640597C482DC4684A753BEB6CD18669AF5B77EF90C2CA6EDD25D68A8F54916E445307B2EDD05789373CD1B96DE9F79367F4E9A1005B3845B971EA18007090”;
let prk = “83C74105FC41DD00A48AF1F15A6C82EFC69D9E05E4002713B1D2F2C44DBF4C39”;
//转16进制放入对应的位置 04+x+y
let pk: cryptoFramework.Point = {
x: BigInt(“0xD03640597C482DC4684A753BEB6CD18669AF5B77EF90C2CA6EDD25D68A8F5491”),
y: BigInt(“0x6E445307B2EDD05789373CD1B96DE9F79367F4E9A1005B3845B971EA18007090”)
}
let sk: bigint = BigInt(“0x83C74105FC41DD00A48AF1F15A6C82EFC69D9E05E4002713B1D2F2C44DBF4C39”);
// 密钥对对象数据
let keyPair: cryptoFramework.ECCKeyPairSpec = {
params: this.genSM2CommonSpec(),
sk: sk,
pk: pk,
algName: ‘SM2’,
specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC
}
let KeyPair: cryptoFramework.KeyPair | null = null;
try {
let KeyPairGenerator: cryptoFramework.AsyKeyGeneratorBySpec;
KeyPairGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(keyPair);
KeyPair = await KeyPairGenerator.generateKeyPair()
.then((keyPair) => {
return keyPair;
})
.catch((e: Error): null => {
console.log(JSON.stringify(e))
return null;
})
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(sync error, ${e.code}, ${e.message}
);
}
return KeyPair;
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
genSM2CommonSpec(): cryptoFramework.ECCCommonParamsSpec {
let fieldFp: cryptoFramework.ECFieldFp = {
fieldType: “Fp”,
p: BigInt(“0xFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF”)
}
let G: cryptoFramework.Point = {
x: BigInt(“0x32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7”),
y: BigInt(“0xBC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0”)
}
let SM2CommonSpec: cryptoFramework.ECCCommonParamsSpec = {
algName: “ECC”,
specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC,
field: fieldFp,
a: BigInt(“0xFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC”),
b: BigInt(“0x28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93”),
g: G,
n: BigInt(“0xFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123”),
h: 1
}
return SM2CommonSpec;
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
关于HarmonyOS 鸿蒙Next使用国密SM2_256,密钥转成Uint8Array后,convertKey报错的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17
找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17
有解决方案吗,我这边也遇到了同样的问题?