• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Converting a Compressed or Uncompressed ECC Public Key (ArkTS)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10You can generate a public key object (**PubKey**) from ECC public key data or obtain the ECC public key data from a public key (**PubKey**) object.
11Currently, only the X.509-certified compressed or uncompressed ECC public key data is supported. <br>The public key data mentioned in this topic is a complete X509 public key. For details about the operations on point data, see [Converting Compressed or Uncompressed ECC Point Data](crypto-convert-compressed-or-uncompressed-ECC-point.md).
12<br>For details about the ECC algorithm specifications, see [ECC](crypto-asym-key-generation-conversion-spec.md#ecc).
13<br>You can specify the string parameter **format** to set the format of the ECC public key to obtain. To obtain a compressed public key that complies with the X.509 standard, set **format** to **X509|COMPRESSED**. To obtain an uncompressed public key, set **format** to **X509|UNCOMPRESSED**.
14
15##  Converting Uncompressed Public Key Data to Compressed Public Key Data
16
171. Encapsulate uncompressed ECC public key data of the Uint8Array type into a **DataBlob** object.
18You can pass either the public key or the private key. In the following example, an uncompressed public key is passed.
192. Call [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator) with the string parameter **'ECC_BrainPoolP256r1'** to create an asymmetric key generator (**AsyKeyGenerator**) object for a 256-bit ECC key pair.
203. Call [AsyKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-3) to convert the **DataBlob** object into an asymmetric key (**KeyPair**) object.
214. Call [PubKey.getEncodedDer](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencodedder12) with **format** set to **'X509|COMPRESSED'** to obtain the byte stream of the compressed public key.
22
23```ts
24import { cryptoFramework } from '@kit.CryptoArchitectureKit';
25
26async function eccPubUncompressedToCompressed() {
27  let pkData = new Uint8Array([48, 90, 48, 20, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 9, 43, 36, 3, 3, 2, 8, 1, 1, 7, 3, 66, 0, 4, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178, 121, 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 157, 111, 40, 217, 215, 148, 120, 224, 205, 82, 83, 92, 185, 21, 211, 184, 5, 19, 114, 33, 86, 85, 228, 123, 242, 206, 200, 98, 178, 184, 130, 35, 232, 45, 5, 202, 189, 11, 46, 163, 156, 152]);
28  let pubKeyBlob: cryptoFramework.DataBlob = { data: pkData };
29  let generator = cryptoFramework.createAsyKeyGenerator('ECC_BrainPoolP256r1');
30  let keyPair = await generator.convertKey(pubKeyBlob, null);
31  let returnBlob = keyPair.pubKey.getEncodedDer('X509|COMPRESSED');
32  console.info('returnBlob data:' + returnBlob.data);
33}
34```
35