1# Key Agreement Using X25519 2 3 4For details about the algorithm specifications, see [X25519](crypto-key-agreement-overview.md#x25519). 5 6 7## How to Develop 8 91. Use [cryptoFramework.createAsyKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreateasykeygenerator), [AsyKeyGenerator.generateKeyPair](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatekeypair-1), and [AsyKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-3) to generate an X25519 asymmetric key pair (**KeyPair**). 10 In addition to the example in this topic, [X25519](crypto-asym-key-generation-conversion-spec.md#x25519) and [Randomly Generating an Asymmetric Key Pair](crypto-generate-asym-key-pair-randomly.md) may help you better understand how to generate an X25519 asymmetric key pair. Note that the input parameters in the reference documents may be different from those in the example below. 11 122. Use [cryptoFramework.createKeyAgreement](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatekeyagreement) with the string parameter **'X25519'** to create an X25519 key agreement (**KeyAgreement**) instance. 13 143. Use [KeyAgreement.generateSecret](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#generatesecret-1) to perform key agreement with the specified private key (**KeyPair.priKey**) and public key (**KeyPair.pubKey**), and return the shared secret. 15 16- Example: Perform key agreement using **await**. 17 18 ```ts 19 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 20 21 async function x25519Await() { 22 // The public and private key pair data is transferred from an external system. 23 let pubKeyArray = new Uint8Array([48, 42, 48, 5, 6, 3, 43, 101, 110, 3, 33, 0, 36, 98, 216, 106, 74, 99, 179, 203, 81, 145, 147, 101, 139, 57, 74, 225, 119, 196, 207, 0, 50, 232, 93, 147, 188, 21, 225, 228, 54, 251, 230, 52]); 24 let priKeyArray = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 110, 4, 34, 4, 32, 112, 65, 156, 73, 65, 89, 183, 39, 119, 229, 110, 12, 192, 237, 186, 153, 21, 122, 28, 176, 248, 108, 22, 242, 239, 179, 106, 175, 85, 65, 214, 90]); 25 let keyGen = cryptoFramework.createAsyKeyGenerator('X25519'); 26 // Key pair A transferred from an external system. 27 let keyPairA = await keyGen.convertKey({ data: pubKeyArray }, { data: priKeyArray }); 28 // Key pair B generated internally. 29 let keyPairB = await keyGen.generateKeyPair(); 30 let keyAgreement = cryptoFramework.createKeyAgreement('X25519'); 31 // Use the public key of A and the private key of B to perform key agreement. 32 let secret1 = await keyAgreement.generateSecret(keyPairB.priKey, keyPairA.pubKey); 33 // Use the private key of A and the public key of B to perform key agreement. 34 let secret2 = await keyAgreement.generateSecret(keyPairA.priKey, keyPairB.pubKey); 35 // The two key agreement results should be the same. 36 if (secret1.data.toString() == secret2.data.toString()) { 37 console.info('x25519 success'); 38 console.info('x25519 output is ' + secret1.data); 39 } else { 40 console.error('x25519 result is not equal'); 41 } 42 } 43 ``` 44 45- Example (using synchronous APIs): 46 47 ```ts 48 import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 49 50 function x25519Await() { 51 // The public and private key pair data is transferred from an external system. 52 let pubKeyArray = new Uint8Array([48, 42, 48, 5, 6, 3, 43, 101, 110, 3, 33, 0, 36, 98, 216, 106, 74, 99, 179, 203, 81, 145, 147, 101, 139, 57, 74, 225, 119, 196, 207, 0, 50, 232, 93, 147, 188, 21, 225, 228, 54, 251, 230, 52]); 53 let priKeyArray = new Uint8Array([48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 110, 4, 34, 4, 32, 112, 65, 156, 73, 65, 89, 183, 39, 119, 229, 110, 12, 192, 237, 186, 153, 21, 122, 28, 176, 248, 108, 22, 242, 239, 179, 106, 175, 85, 65, 214, 90]); 54 let keyGen = cryptoFramework.createAsyKeyGenerator('X25519'); 55 // Key pair A transferred from an external system. 56 let keyPairA = keyGen.convertKeySync({ data: pubKeyArray }, { data: priKeyArray }); 57 // Key pair B generated internally. 58 let keyPairB = keyGen.generateKeyPairSync(); 59 let keyAgreement = cryptoFramework.createKeyAgreement('X25519'); 60 // Use the public key of A and the private key of B to perform key agreement. 61 let secret1 = keyAgreement.generateSecretSync(keyPairB.priKey, keyPairA.pubKey); 62 // Use the private key of A and the public key of B to perform key agreement. 63 let secret2 = keyAgreement.generateSecretSync(keyPairA.priKey, keyPairB.pubKey); 64 // The two key agreement results should be the same. 65 if (secret1.data.toString() == secret2.data.toString()) { 66 console.info('x25519 success'); 67 console.info('x25519 output is ' + secret1.data); 68 } else { 69 console.error('x25519 result is not equal'); 70 } 71 } 72 ``` 73