HarmonyOS 鸿蒙Next DSA签名将字符串转化为私钥对象失败
HarmonyOS 鸿蒙Next DSA签名将字符串转化为私钥对象失败 我使用字符串私钥,实现dsa签名的功能,在生成KeyPair的过程中一直失败,报错信息是
code = 17630001
message = "convert key fail."
stack = "Cannot get SourceMap info, dump raw stack:
=====================Backtrace========================
#00 pc 00000000006e7d4c /system/lib64/platformsdk/libark_jsruntime.so
#01 pc 00000000006e82c0 /system/lib64/platformsdk/libark_jsruntime.so
#02 pc 0000000000323f90 /system/lib64/platformsdk/libark_jsruntime.so
#03 pc 00000000001bbd3c /system/lib64/platformsdk/libark_jsruntime.so
#04 pc 00000000001bb7cc /system/lib64/platformsdk/libark_jsruntime.so
#05 pc 000000000024e828 /system/lib64/platformsdk/libark_jsruntime.so
#06 pc 00000000005c6f64 /system/lib64/platformsdk/libark_jsruntime.so
#07 pc 000000000059f6e4 /system/lib64/platformsdk/libark_jsruntime.so
#08 pc 00000000000576a4 /system/lib64/platformsdk/libace_napi.z.so
#09 pc 0000000000033ae0 /system/lib64/module/security/libcryptoframework_napi.z.so
#10 pc 0000000000019d9c /system/lib64/module/security/libcryptoframework_napi.z.so"
以下是具体代码
static async generateDSAKeyPair(): Promise<cryptoFramework.KeyPair | undefined> {
try {
let keyGenAlg = "DSA1024";
let base64Helper = new util.Base64Helper();
let key = base64Helper.decodeSync(DSAUtil.DSAKEY)
let keyUint8Array = new Uint8Array(key); // 调试信息
let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
let priKeyBlob: cryptoFramework.DataBlob = { data: keyUint8Array };
return await generator.convertKey(null, priKeyBlob)
} catch (error) {
console.error("生成 DSA 私钥失败:", error);
return undefined
}
}
更多关于HarmonyOS 鸿蒙Next DSA签名将字符串转化为私钥对象失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
当调用convertKey方法将外来二进制数据转换为算法库非对称密钥对象时,公钥应满足ASN.1语法、X.509规范、DER编码格式,私钥应满足ASN.1语法、PKCS#8规范、DER编码格式。需要确认使用的key信息是否符合,使用如下demo可以正常生成转换,此处keyUint8Array 中的key由asyKeyGeneratorBySpec.generateKeyPair生成,参考https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V14/js-apis-cryptoframework-V14#cryptoframeworkcreateasykeygeneratorbyspec10
private async generateDSAKeyPair() {
try {
let keyGenAlg = "DSA1024";
let keyUint8Array = new Uint8Array([48,130,1,204,2,1,0,2,129,129,0,237,21,1,85,27,138,179,84,127,99,85,255,220,41,19,133,109,222,202,25,136,51,219,208,79,2,14,95,37,228,124,80,224,179,137,79,118,144,160,210,234,94,211,167,190,37,197,66,146,166,152,225,240,134,235,58,151,222,180,219,240,79,202,210,218,253,148,169,243,92,58,227,56,171,53,71,126,22,152,29,237,106,91,19,213,255,32,191,85,241,178,98,48,58,211,168,10,247,26,166,170,35,84,210,14,156,130,100,118,100,189,182,179,51,183,190,160,165,244,157,85,202,64,188,49,42,23,41,2,29,0,210,51,4,4,64,25,213,211,130,207,234,191,53,22,54,199,171,33,150,148,172,132,80,81,246,11,4,123,2,129,128,44,194,102,216,189,51,195,0,155,214,127,40,90,37,123,167,79,12,58,126,18,183,34,134,70,50,160,172,63,44,23,201,28,47,63,103,235,45,87,7,30,244,122,170,143,142,23,162,26,210,193,7,46,225,206,40,19,98,170,208,29,203,205,56,118,69,92,209,126,29,213,93,78,211,111,160,17,219,64,240,187,184,203,160,29,6,111,57,43,94,170,148,4,191,203,119,95,33,150,166,188,32,238,236,61,179,45,84,233,77,135,236,219,122,3,16,165,160,23,197,205,184,172,120,89,119,120,189,2,129,129,0,174,107,93,80,66,231,88,243,252,154,2,208,9,216,150,223,17,88,17,167,91,95,123,56,45,133,38,39,13,187,60,2,148,3,250,251,133,115,186,78,240,49,78,168,111,9,208,30,130,161,77,30,187,103,176,195,49,244,16,73,189,107,24,66,101,139,5,146,231,6,165,228,210,12,20,182,121,119,225,125,247,189,212,100,204,225,75,95,19,186,230,96,119,96,252,223,57,78,11,115,172,112,170,241,65,250,77,175,215,54,189,3,100,177,214,230,192,215,104,58,93,230,185,34,30,127,45,107,2,29,0,162,221,42,219,45,17,57,44,37,65,147,15,97,241,22,92,55,10,171,210,215,141,0,52,46,10,47,217]);
let generator = cryptoFramework.createAsyKeyGenerator(keyGenAlg);
let priKeyBlob: cryptoFramework.DataBlob = { data: keyUint8Array };
console.log("开始生成 DSA 私钥");
return await generator.convertKey(null, priKeyBlob)
} catch (error) {
console.error("生成 DSA 私钥失败:", error);
return undefined
}
}
更多关于HarmonyOS 鸿蒙Next DSA签名将字符串转化为私钥对象失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对帖子标题“HarmonyOS 鸿蒙Next DSA签名将字符串转化为私钥对象失败”的问题,以下是专业且直接的回答:
在HarmonyOS鸿蒙Next系统中,若遇到DSA签名过程中将字符串转化为私钥对象失败的情况,通常可能是由于以下原因:
-
字符串格式错误:确保提供的私钥字符串符合正确的格式,包括PEM或DER等格式,且未包含非法字符或多余的空格。
-
编码问题:私钥字符串可能需要进行Base64解码或其他适当的编码转换,以匹配系统期望的输入格式。
-
密钥类型不匹配:检查私钥字符串是否确实为DSA类型的私钥,而非RSA或其他类型。
-
API使用不当:确保在调用相关API时,正确传递了所有必要的参数,并遵循了API的文档说明。
-
权限问题:确保应用程序具有访问和解析私钥的适当权限。
若上述检查均无误,但问题依旧存在,可能是由于系统或库的内部实现问题。此时,建议直接联系鸿蒙系统的官方技术支持团队,以获取更深入的帮助。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html,