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.

Only Essential Cookies
Accept All
GuidesSystemSecurityCrypto Architecture KitKey Generation and ConversionKey Generation and Conversion DevelopmentRandomly Generating an Asymmetric Key Pair (ArkTS)

Randomly Generating an Asymmetric Key Pair (ArkTS)

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.

Randomly Generating an RSA Key Pair

For details about the algorithm specifications, see RSA.

  1. 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.

  2. Call AsyKeyGenerator.generateKeyPair to randomly generate an asymmetric key pair (KeyPair).

    The KeyPair object includes a public key (PubKey) and a private key (PriKey).

  3. 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).

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    2. function generateAsyKey() {
    3. // Create an AsyKeyGenerator instance.
    4. let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_2');
    5. // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
    6. let keyGenPromise = rsaGenerator.generateKeyPair();
    7. keyGenPromise.then(keyPair => {
    8. let pubKey = keyPair.pubKey;
    9. let priKey = keyPair.priKey;
    10. // Obtain the binary data of the asymmetric key pair.
    11. let pkBlob = pubKey.getEncoded();
    12. let skBlob = priKey.getEncoded();
    13. console.info('pk bin data: ' + pkBlob.data);
    14. console.info('sk bin data: ' + skBlob.data);
    15. });
    16. }
  • Example: Randomly generate an RSA key pair (using the synchronous API generateKeyPairSync).

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    2. function generateAsyKeySync() {
    3. // Create an AsyKeyGenerator instance.
    4. let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_2');
    5. // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
    6. try {
    7. let keyPair = rsaGenerator.generateKeyPairSync();
    8. if (keyPair != null) {
    9. let pubKey = keyPair.pubKey;
    10. let priKey = keyPair.priKey;
    11. // Obtain the binary data of the asymmetric key pair.
    12. let pkBlob = pubKey.getEncoded();
    13. let skBlob = priKey.getEncoded();
    14. console.info('pk bin data: ' + pkBlob.data);
    15. console.info('sk bin data: ' + skBlob.data);
    16. } else {
    17. console.error('[Sync]: get key pair result: fail!');
    18. }
    19. } catch (e) {
    20. console.error(`get key pair failed: errCode: ${e.code}, message: ${e.message}`);
    21. }
    22. }

Randomly Generating an SM2 Key Pair

For details about the algorithm specifications, see SM2.

  1. Call cryptoFramework.createAsyKeyGenerator with the string parameter 'SM2_256' to create an asymmetric key generator (AsyKeyGenerator) object for a 256-bit SM2 key pair.

  2. Call AsyKeyGenerator.generateKeyPair to randomly generate an asymmetric key pair (KeyPair).

    The KeyPair object includes a public key (PubKey) and a private key (PriKey).

  3. 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).

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    2. function generateSM2Key() {
    3. // Create an AsyKeyGenerator instance.
    4. let sm2Generator = cryptoFramework.createAsyKeyGenerator('SM2_256');
    5. // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
    6. let keyGenPromise = sm2Generator.generateKeyPair();
    7. keyGenPromise.then(keyPair => {
    8. let pubKey = keyPair.pubKey;
    9. let priKey = keyPair.priKey;
    10. // Obtain the binary data of the asymmetric key pair.
    11. let pkBlob = pubKey.getEncoded();
    12. let skBlob = priKey.getEncoded();
    13. console.info('pk bin data: ' + pkBlob.data);
    14. console.info('sk bin data: ' + skBlob.data);
    15. });
    16. }
  • Example: Randomly generate an RSA key pair (using the synchronous API generateKeyPairSync).

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    2. function generateSM2KeySync() {
    3. // Create an AsyKeyGenerator instance.
    4. let rsaGenerator = cryptoFramework.createAsyKeyGenerator('SM2_256');
    5. // Use AsyKeyGenerator to randomly generate an asymmetric key pair.
    6. try {
    7. let keyPair = rsaGenerator.generateKeyPairSync();
    8. if (keyPair != null) {
    9. let pubKey = keyPair.pubKey;
    10. let priKey = keyPair.priKey;
    11. // Obtain the binary data of the asymmetric key pair.
    12. let pkBlob = pubKey.getEncoded();
    13. let skBlob = priKey.getEncoded();
    14. console.info('pk bin data: ' + pkBlob.data);
    15. console.info('sk bin data: ' + skBlob.data);
    16. } else {
    17. console.error('[Sync]: get key pair result: fail!');
    18. }
    19. } catch (e) {
    20. console.error(`get key pair failed: errCode: ${e.code}, message: ${e.message}`);
    21. }
    22. }
Search in Guides
Enter a keyword.