#鸿蒙通关秘籍#怎么使用ECDH进行密钥协商?在next中如何实现一个同步的方法?

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FTP风行者

这玩意晦涩难懂,一般很少人用到的,贴个代码看看能不能帮到你

import { cryptoFramework } from '@kit.CryptoArchitectureKit';

function ecdhAwait() {
    let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]);
    let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]);
    let eccGen = `cryptoFramework`.createAsyKeyGenerator('ECC256');
    let keyPairA = eccGen.convertKeySync({ data: pubKeyArray }, { data: priKeyArray });
    let keyPairB = eccGen.generateKeyPairSync();
    let eccKeyAgreement = `cryptoFramework`.createKeyAgreement('ECC256');
    let secret1 = eccKeyAgreement.generateSecretSync(keyPairB.priKey, keyPairA.pubKey);
    let secret2 = eccKeyAgreement.generateSecretSync(keyPairA.priKey, keyPairB.pubKey);
    if (secret1.data.toString() == secret2.data.toString()) {
        console.info('ecdh success');
        console.info('ecdh output is ' + secret1.data);
    } else {
        console.error('ecdh result is not equal');
    }
}
已于2024-12-10 10:05:22修改
分享
微博
QQ
微信
回复
3天前
相关问题
一个页面怎么实现多个AbilitySlice?
14311浏览 • 5回复 待解决
如何实现一个监听网络变化方法
589浏览 • 1回复 待解决