Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.
HarmonyOS
This topic uses RSA and SM2 as an example to describe how to generate an asymmetric key pair (KeyPair) and obtain the binary data.
The asymmetric key pair may be used for subsequent operations such as encryption and decryption, and binary data may be used for storage or transmission.
For details about the algorithm specifications, see RSA.
Call cryptoFramework.createAsyKeyGenerator with the string parameter 'RSA1024|PRIMES_2' to create an asymmetric key generator (AsyKeyGenerator) object for a 1024-bit RSA key with two primes.
Call AsyKeyGenerator.generateKeyPair to randomly generate an asymmetric key pair (KeyPair).
The KeyPair object includes a public key (PubKey) and a private key (PriKey).
Call PubKey.getEncoded to obtain the binary data of the public key, and call PriKey.getEncoded to obtain the binary data of the private key.
Example: Randomly generate an RSA key pair (using promise-based APIs).
- import { cryptoFramework } from '@kit.CryptoArchitectureKit';
-
- function generateAsyKey() {
- // Create an AsyKeyGenerator instance.
- let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_2');
- // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
- let keyGenPromise = rsaGenerator.generateKeyPair();
- keyGenPromise.then(keyPair => {
- let pubKey = keyPair.pubKey;
- let priKey = keyPair.priKey;
- // Obtain the binary data of the asymmetric key pair.
- let pkBlob = pubKey.getEncoded();
- let skBlob = priKey.getEncoded();
- console.info('pk bin data: ' + pkBlob.data);
- console.info('sk bin data: ' + skBlob.data);
- });
- }
Example: Randomly generate an RSA key pair (using the synchronous API generateKeyPairSync).
- import { cryptoFramework } from '@kit.CryptoArchitectureKit';
-
- function generateAsyKeySync() {
- // Create an AsyKeyGenerator instance.
- let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_2');
- // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
- try {
- let keyPair = rsaGenerator.generateKeyPairSync();
- if (keyPair != null) {
- let pubKey = keyPair.pubKey;
- let priKey = keyPair.priKey;
- // Obtain the binary data of the asymmetric key pair.
- let pkBlob = pubKey.getEncoded();
- let skBlob = priKey.getEncoded();
- console.info('pk bin data: ' + pkBlob.data);
- console.info('sk bin data: ' + skBlob.data);
- } else {
- console.error('[Sync]: get key pair result: fail!');
- }
- } catch (e) {
- console.error(`get key pair failed: errCode: ${e.code}, message: ${e.message}`);
- }
- }
For details about the algorithm specifications, see SM2.
Call cryptoFramework.createAsyKeyGenerator with the string parameter 'SM2_256' to create an asymmetric key generator (AsyKeyGenerator) object for a 256-bit SM2 key pair.
Call AsyKeyGenerator.generateKeyPair to randomly generate an asymmetric key pair (KeyPair).
The KeyPair object includes a public key (PubKey) and a private key (PriKey).
Call PubKey.getEncoded to obtain the binary data of the public key, and call PriKey.getEncoded to obtain the binary data of the private key.
Example: Randomly generate an SM2 key pair (using promise-based APIs).
- import { cryptoFramework } from '@kit.CryptoArchitectureKit';
-
- function generateSM2Key() {
- // Create an AsyKeyGenerator instance.
- let sm2Generator = cryptoFramework.createAsyKeyGenerator('SM2_256');
- // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
- let keyGenPromise = sm2Generator.generateKeyPair();
- keyGenPromise.then(keyPair => {
- let pubKey = keyPair.pubKey;
- let priKey = keyPair.priKey;
- // Obtain the binary data of the asymmetric key pair.
- let pkBlob = pubKey.getEncoded();
- let skBlob = priKey.getEncoded();
- console.info('pk bin data: ' + pkBlob.data);
- console.info('sk bin data: ' + skBlob.data);
- });
- }
Example: Randomly generate an RSA key pair (using the synchronous API generateKeyPairSync).
- import { cryptoFramework } from '@kit.CryptoArchitectureKit';
-
- function generateSM2KeySync() {
- // Create an AsyKeyGenerator instance.
- let rsaGenerator = cryptoFramework.createAsyKeyGenerator('SM2_256');
- // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
- try {
- let keyPair = rsaGenerator.generateKeyPairSync();
- if (keyPair != null) {
- let pubKey = keyPair.pubKey;
- let priKey = keyPair.priKey;
- // Obtain the binary data of the asymmetric key pair.
- let pkBlob = pubKey.getEncoded();
- let skBlob = priKey.getEncoded();
- console.info('pk bin data: ' + pkBlob.data);
- console.info('sk bin data: ' + skBlob.data);
- } else {
- console.error('[Sync]: get key pair result: fail!');
- }
- } catch (e) {
- console.error(`get key pair failed: errCode: ${e.code}, message: ${e.message}`);
- }
- }
Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Quick start
Helps you find desired resources with ease.