1e41f4b71Sopenharmony_ci# Converting a PEM String into an Asymmetric Key Pair (ArkTS)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThis topic walks you through on how to convert a string in PEM format into an RSA asymmetric key pair (**KeyPair**).
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **NOTE**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> The **convertPemKey** operation must comply with the following requirements: 
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci> - The public key must comply with X.509 specifications, PKCS\#1 specifications, and PEM encoding format.
12e41f4b71Sopenharmony_ci>
13e41f4b71Sopenharmony_ci> - The private key must comply with the PKCS\#8 or PKCS\#1 specifications and the PEM encoding format.
14e41f4b71Sopenharmony_ci>
15e41f4b71Sopenharmony_ci> - Currently, only RSA asymmetric keys can be converted.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## Converting a String in PEM Format into an RSA Key Pair
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [RSA](crypto-asym-key-generation-conversion-spec.md#rsa).
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci1. Use [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator) with the string parameter **'RSA1024'** to create an asymmetric key generator (**AsyKeyGenerator**) object for a 1024-bit RSA key with two primes.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci   The default number of primes for creating an RSA asymmetric key is **2**. The **PRIMES_2** parameter is omitted in the string parameter here.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci2. Use [AsyKeyGenerator.convertPemKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertpemkey12) to convert the binary data into an asymmetric key pair (**KeyPair**).
27e41f4b71Sopenharmony_ci3. Use [AsyKeyGenerator.getEncodedPem](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencodedpem12) to convert the public key in the asymmetric key object into PKCS #1 or X.509 format and convert the private key into PKCS #1 or PKCS #8 format.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci- Example: Convert a string in PEM format into an RSA key pair (using promise-based APIs).
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci  ```ts
32e41f4b71Sopenharmony_ci  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci  let priKeyPkcs1Str1024: string  =
35e41f4b71Sopenharmony_ci    "-----BEGIN RSA PRIVATE KEY-----\n"
36e41f4b71Sopenharmony_ci    + "MIICXQIBAAKBgQCwIN3mr21+N96ToxnVnaS+xyK9cNRAHiHGgrbjHw6RAj3V+l+W\n"
37e41f4b71Sopenharmony_ci    + "Y68IhIe3DudVlzE9oMjeOQwkMkq//HCxNlIlFR6O6pa0mrXSwPRE7YKG97CeKk2g\n"
38e41f4b71Sopenharmony_ci    + "YOS8YEh8toAvm7xKbiLkXuuMlxrjP2j/mb5iI/UASFSPZiQ/IyxDr0AQaQIDAQAB\n"
39e41f4b71Sopenharmony_ci    + "AoGAEvBFzBNa+7J4PXnRQlYEK/tvsd0bBZX33ceacMubHl6WVZbphltLq+fMTBPP\n"
40e41f4b71Sopenharmony_ci    + "LjXmtpC+aJ7Lvmyl+wTi/TsxE9vxW5JnbuRT48rnZ/Xwq0eozDeEeIBRrpsr7Rvr\n"
41e41f4b71Sopenharmony_ci    + "7ctrgzr4m4yMHq9aDgpxj8IR7oHkfwnmWr0wM3FuiVlj650CQQDineeNZ1hUTkj4\n"
42e41f4b71Sopenharmony_ci    + "D3O+iCi3mxEVEeJrpqrmSFolRMb+iozrIRKuJlgcOs+Gqi2fHfOTTL7LkpYe8SVg\n"
43e41f4b71Sopenharmony_ci    + "e3JxUdVLAkEAxvcZXk+byMFoetrnlcMR13VHUpoVeoV9qkv6CAWLlbMdgf7uKmgp\n"
44e41f4b71Sopenharmony_ci    + "a1Yp3QPDNQQqkPvrqtfR19JWZ4uy1qREmwJALTU3BjyBoH/liqb6fh4HkWk75Som\n"
45e41f4b71Sopenharmony_ci    + "MzeSjFIOubSYxhq5tgZpBZjcpvUMhV7Zrw54kwASZ+YcUJvmyvKViAm9NQJBAKF7\n"
46e41f4b71Sopenharmony_ci    + "DyXSKrem8Ws0m1ybM7HQx5As6l3EVhePDmDQT1eyRbKp+xaD74nkJpnwYdB3jyyY\n"
47e41f4b71Sopenharmony_ci    + "qc7A1tj5J5NmeEFolR0CQQCn76Xp8HCjGgLHw9vg7YyIL28y/XyfFyaZAzzK+Yia\n"
48e41f4b71Sopenharmony_ci    + "akNwQ6NeGtXSsuGCcyyfpacHp9xy8qXQNKSkw03/5vDO\n"
49e41f4b71Sopenharmony_ci    + "-----END RSA PRIVATE KEY-----\n";
50e41f4b71Sopenharmony_ci  let publicPkcs1Str1024: string  =
51e41f4b71Sopenharmony_ci    "-----BEGIN RSA PUBLIC KEY-----\n"
52e41f4b71Sopenharmony_ci    + "MIGJAoGBALAg3eavbX433pOjGdWdpL7HIr1w1EAeIcaCtuMfDpECPdX6X5ZjrwiE\n"
53e41f4b71Sopenharmony_ci    + "h7cO51WXMT2gyN45DCQySr/8cLE2UiUVHo7qlrSatdLA9ETtgob3sJ4qTaBg5Lxg\n"
54e41f4b71Sopenharmony_ci    + "SHy2gC+bvEpuIuRe64yXGuM/aP+ZvmIj9QBIVI9mJD8jLEOvQBBpAgMBAAE=\n"
55e41f4b71Sopenharmony_ci    + "-----END RSA PUBLIC KEY-----\n";
56e41f4b71Sopenharmony_ci  async function TestPkcs1ToPkcs8ByPromise() {
57e41f4b71Sopenharmony_ci    let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
58e41f4b71Sopenharmony_ci    let keyPair = await asyKeyGenerator.convertPemKey(publicPkcs1Str1024, priKeyPkcs1Str1024);
59e41f4b71Sopenharmony_ci    let priPemKey = keyPair.priKey;
60e41f4b71Sopenharmony_ci    let pubPemKey = keyPair.pubKey;
61e41f4b71Sopenharmony_ci    let priString = priPemKey.getEncodedPem('PKCS8');
62e41f4b71Sopenharmony_ci    let pubString = pubPemKey.getEncodedPem('X509');
63e41f4b71Sopenharmony_ci    console.info("[promise]TestPkcs1ToPkcs8ByPromise priString output is " + priString);
64e41f4b71Sopenharmony_ci    console.info("[promise]TestPkcs1ToPkcs8ByPromise pubString output is " + pubString);
65e41f4b71Sopenharmony_ci  }
66e41f4b71Sopenharmony_ci  ```
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci- Example: Convert a string in PEM format into an RSA key pair (using the synchronous API [convertPemKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertpemkeysync12)).
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci  ```ts
71e41f4b71Sopenharmony_ci  import { cryptoFramework } from '@kit.CryptoArchitectureKit';
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci  let priKeyPkcs1Str1024: string  =
74e41f4b71Sopenharmony_ci    "-----BEGIN RSA PRIVATE KEY-----\n"
75e41f4b71Sopenharmony_ci    + "MIICXQIBAAKBgQCwIN3mr21+N96ToxnVnaS+xyK9cNRAHiHGgrbjHw6RAj3V+l+W\n"
76e41f4b71Sopenharmony_ci    + "Y68IhIe3DudVlzE9oMjeOQwkMkq//HCxNlIlFR6O6pa0mrXSwPRE7YKG97CeKk2g\n"
77e41f4b71Sopenharmony_ci    + "YOS8YEh8toAvm7xKbiLkXuuMlxrjP2j/mb5iI/UASFSPZiQ/IyxDr0AQaQIDAQAB\n"
78e41f4b71Sopenharmony_ci    + "AoGAEvBFzBNa+7J4PXnRQlYEK/tvsd0bBZX33ceacMubHl6WVZbphltLq+fMTBPP\n"
79e41f4b71Sopenharmony_ci    + "LjXmtpC+aJ7Lvmyl+wTi/TsxE9vxW5JnbuRT48rnZ/Xwq0eozDeEeIBRrpsr7Rvr\n"
80e41f4b71Sopenharmony_ci    + "7ctrgzr4m4yMHq9aDgpxj8IR7oHkfwnmWr0wM3FuiVlj650CQQDineeNZ1hUTkj4\n"
81e41f4b71Sopenharmony_ci    + "D3O+iCi3mxEVEeJrpqrmSFolRMb+iozrIRKuJlgcOs+Gqi2fHfOTTL7LkpYe8SVg\n"
82e41f4b71Sopenharmony_ci    + "e3JxUdVLAkEAxvcZXk+byMFoetrnlcMR13VHUpoVeoV9qkv6CAWLlbMdgf7uKmgp\n"
83e41f4b71Sopenharmony_ci    + "a1Yp3QPDNQQqkPvrqtfR19JWZ4uy1qREmwJALTU3BjyBoH/liqb6fh4HkWk75Som\n"
84e41f4b71Sopenharmony_ci    + "MzeSjFIOubSYxhq5tgZpBZjcpvUMhV7Zrw54kwASZ+YcUJvmyvKViAm9NQJBAKF7\n"
85e41f4b71Sopenharmony_ci    + "DyXSKrem8Ws0m1ybM7HQx5As6l3EVhePDmDQT1eyRbKp+xaD74nkJpnwYdB3jyyY\n"
86e41f4b71Sopenharmony_ci    + "qc7A1tj5J5NmeEFolR0CQQCn76Xp8HCjGgLHw9vg7YyIL28y/XyfFyaZAzzK+Yia\n"
87e41f4b71Sopenharmony_ci    + "akNwQ6NeGtXSsuGCcyyfpacHp9xy8qXQNKSkw03/5vDO\n"
88e41f4b71Sopenharmony_ci    + "-----END RSA PRIVATE KEY-----\n";
89e41f4b71Sopenharmony_ci  let publicPkcs1Str1024: string  =
90e41f4b71Sopenharmony_ci    "-----BEGIN RSA PUBLIC KEY-----\n"
91e41f4b71Sopenharmony_ci    + "MIGJAoGBALAg3eavbX433pOjGdWdpL7HIr1w1EAeIcaCtuMfDpECPdX6X5ZjrwiE\n"
92e41f4b71Sopenharmony_ci    + "h7cO51WXMT2gyN45DCQySr/8cLE2UiUVHo7qlrSatdLA9ETtgob3sJ4qTaBg5Lxg\n"
93e41f4b71Sopenharmony_ci    + "SHy2gC+bvEpuIuRe64yXGuM/aP+ZvmIj9QBIVI9mJD8jLEOvQBBpAgMBAAE=\n"
94e41f4b71Sopenharmony_ci    + "-----END RSA PUBLIC KEY-----\n";
95e41f4b71Sopenharmony_ci  function TestPkcs1ToPkcs8BySync() {
96e41f4b71Sopenharmony_ci    let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
97e41f4b71Sopenharmony_ci    try {
98e41f4b71Sopenharmony_ci      let keyPairData = asyKeyGenerator.convertPemKeySync(publicPkcs1Str1024, priKeyPkcs1Str1024);
99e41f4b71Sopenharmony_ci      if (keyPairData != null) {
100e41f4b71Sopenharmony_ci        console.info('[Sync]: convert pem key pair success');
101e41f4b71Sopenharmony_ci      } else {
102e41f4b71Sopenharmony_ci        console.error("[Sync]: convert pem key pair result fail!");
103e41f4b71Sopenharmony_ci      }
104e41f4b71Sopenharmony_ci      let priPemKey = keyPairData.priKey;
105e41f4b71Sopenharmony_ci      let pubPemKey = keyPairData.pubKey;
106e41f4b71Sopenharmony_ci      let priString = priPemKey.getEncodedPem('PKCS8');
107e41f4b71Sopenharmony_ci      let pubString = pubPemKey.getEncodedPem('X509');
108e41f4b71Sopenharmony_ci      console.info("[Sync]TestPkcs1ToPkcs8BySync priString output is " + priString);
109e41f4b71Sopenharmony_ci      console.info("[Sync]TestPkcs1ToPkcs8BySync pubString output is " + pubString);
110e41f4b71Sopenharmony_ci    } catch (e) {
111e41f4b71Sopenharmony_ci      console.error(`Sync error, ${e.code}, ${e.message}`);
112e41f4b71Sopenharmony_ci    }
113e41f4b71Sopenharmony_ci  }
114e41f4b71Sopenharmony_ci  ```
115