名称 | 接口 |
|---|---|
指定DH算法公共参数。 | |
指定DH算法私钥参数。 | |
指定DH算法公钥参数。 | |
指定DH算法全量参数。 | |
通过指定密钥参数,创建非对称密钥生成器。 | |
通过指定算法名称的字符串,创建非对称密钥生成器。 |
import { cryptoFramework } from '@kit.CryptoArchitectureKit'; // 使用字符串参数生成DH密钥对 export function getDHKeyFromStringParams() { try { // 1、生成密钥对 let spec = cryptoFramework.createAsyKeyGenerator('DH_modp2048'); let pubKey = spec.generateKeyPairSync().pubKey.getEncoded().data; let priKey = spec.generateKeyPairSync().priKey.getEncoded().data; console.info(`getDHKeyFromStringparams success, DH public key = ${pubKey.toString()}`); console.info(`getDHKeyFromStringparams success, DH priKey key = ${priKey.toString()}`); } catch (error) { console.error(`failed`); } }
// 使用密钥参数生成DH密钥对 export function getDHKeyFromParams() { try { // 1、生成公共参数 let dHCommonParamsSpec: cryptoFramework.DHCommonParamsSpec = { // 参数仅用于示例,按照实际填写 p: BigInt('0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21'), g: BigInt('0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34'), l: 192, algName: 'DH', specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC }; // 2、生成密钥对参数 let dHKeyPairSpec: cryptoFramework.DHKeyPairSpec = { // 参数仅用于示例,按照实际填写 sk: BigInt('0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe'), pk: BigInt('0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4'), params: dHCommonParamsSpec, algName: 'DH', specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC }; // 3、生成密钥对 let spec = cryptoFramework.createAsyKeyGeneratorBySpec(dHKeyPairSpec); let pubKey = spec.generateKeyPairSync().pubKey.getEncoded().data; let priKey = spec.generateKeyPairSync().priKey.getEncoded().data; console.info(`getDHKeyFromparams success, DH public key = ${pubKey.toString()}`); console.info(`getDHKeyFromparams success, DH priKey key = ${priKey.toString()}`); } catch (error) { console.error(`getDHKeyFromparams failed`); } }
完整示例参考如下:
import { cryptoFramework } from '@kit.CryptoArchitectureKit'; // 使用字符串参数生成DH密钥对 export function getDHKeyFromStringParams() { try { // 1、生成密钥对 let spec = cryptoFramework.createAsyKeyGenerator('DH_modp2048'); let pubKey = spec.generateKeyPairSync().pubKey.getEncoded().data; let priKey = spec.generateKeyPairSync().priKey.getEncoded().data; console.info(`getDHKeyFromStringparams success, DH public key = ${pubKey.toString()}`); console.info(`getDHKeyFromStringparams success, DH priKey key = ${priKey.toString()}`); } catch (error) { console.error(`failed`); } } // 使用密钥参数生成DH密钥对 export function getDHKeyFromParams() { try { // 1、生成公共参数 let dHCommonParamsSpec: cryptoFramework.DHCommonParamsSpec = { // 参数仅用于示例,按照实际填写 p: BigInt('0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21'), g: BigInt('0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34'), l: 192, algName: 'DH', specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC }; // 2、生成密钥对参数 let dHKeyPairSpec: cryptoFramework.DHKeyPairSpec = { // 参数仅用于示例,按照实际填写 sk: BigInt('0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe'), pk: BigInt('0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4'), params: dHCommonParamsSpec, algName: 'DH', specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC }; // 3、生成密钥对 let spec = cryptoFramework.createAsyKeyGeneratorBySpec(dHKeyPairSpec); let pubKey = spec.generateKeyPairSync().pubKey.getEncoded().data; let priKey = spec.generateKeyPairSync().priKey.getEncoded().data; console.info(`getDHKeyFromparams success, DH public key = ${pubKey.toString()}`); console.info(`getDHKeyFromparams success, DH priKey key = ${priKey.toString()}`); } catch (error) { console.error(`getDHKeyFromparams failed`); } } @Entry @Component struct Index { build() { RelativeContainer() { Text('Hello World') .id('HelloWorld') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .onClick(() => { getDHKeyFromStringParams(); getDHKeyFromParams(); }); } .height('100%') .width('100%'); } }