1e41f4b71Sopenharmony_ci# Converting Binary Data into a Symmetric Key (ArkTS) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciThis topic uses 3DES and HMAC as an example to describe how to convert binary data into a symmetric key. That is, convert a piece of external or internal binary data into a key object for subsequent operations, such as encryption and decryption. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Converting Binary Data into a 3DES Key 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [3DES](crypto-sym-key-generation-conversion-spec.md#3des). 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci1. Obtain the 3DES key binary data and encapsulate it into a **DataBlob** object. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci2. Use [cryptoFramework.createSymKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatesymkeygenerator) with the string parameter **'3DES192'** to create a symmetric key generator (**SymKeyGenerator**) object for a 192-bit 3DES key. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci3. Use [SymKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-1) to convert the binary data into a symmetric key (**SymKey**). 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci4. Use [SymKey.getEncoded](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencoded) to obtain the binary data of the key. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- Example: Convert binary data into a 192-bit 3DES key (using callback-based APIs). 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci ```ts 22e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 23e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci function genKeyMaterialBlob(): cryptoFramework.DataBlob { 26e41f4b71Sopenharmony_ci let arr = [ 27e41f4b71Sopenharmony_ci 0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 28e41f4b71Sopenharmony_ci 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c, 29e41f4b71Sopenharmony_ci 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // The key length is 192 bits, that is, 24 bytes. 30e41f4b71Sopenharmony_ci let keyMaterial = new Uint8Array(arr); 31e41f4b71Sopenharmony_ci return { data: keyMaterial }; 32e41f4b71Sopenharmony_ci } 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci function testConvertSymKey() { 35e41f4b71Sopenharmony_ci // Create a SymKeyGenerator instance. 36e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); 37e41f4b71Sopenharmony_ci // Convert the data into a symmetric key. 38e41f4b71Sopenharmony_ci let keyMaterialBlob = genKeyMaterialBlob(); 39e41f4b71Sopenharmony_ci try { 40e41f4b71Sopenharmony_ci symKeyGenerator.convertKey(keyMaterialBlob, (error, key) => { 41e41f4b71Sopenharmony_ci if (error) {// If the service logic fails to be executed, the first parameter of the callback returns error information, that is, throw an exception asynchronously. 42e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 43e41f4b71Sopenharmony_ci console.error(`convertKey error, ${e.code}, ${e.message}`); 44e41f4b71Sopenharmony_ci return; 45e41f4b71Sopenharmony_ci } 46e41f4b71Sopenharmony_ci console.info('key algName: ' + key.algName); 47e41f4b71Sopenharmony_ci console.info('key format: ' + key.format); 48e41f4b71Sopenharmony_ci let encodedKey = key.getEncoded(); // Obtain the binary data of the symmetric key and output the data as a byte array. The length is 24 bytes. 49e41f4b71Sopenharmony_ci console.info('key getEncoded hex: ' + encodedKey.data); 50e41f4b71Sopenharmony_ci }) 51e41f4b71Sopenharmony_ci } catch (error) {// Throw an exception immediately when an error is detected during parameter check. 52e41f4b71Sopenharmony_ci let e: BusinessError = error as BusinessError; 53e41f4b71Sopenharmony_ci console.error(`convertKey failed, ${e.code}, ${e.message}`); 54e41f4b71Sopenharmony_ci } 55e41f4b71Sopenharmony_ci } 56e41f4b71Sopenharmony_ci ``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci- Example using synchronous API [convertKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkeysync12): 59e41f4b71Sopenharmony_ci ```ts 60e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci function genKeyMaterialBlob(): cryptoFramework.DataBlob { 63e41f4b71Sopenharmony_ci let arr = [ 64e41f4b71Sopenharmony_ci 0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 65e41f4b71Sopenharmony_ci 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c, 66e41f4b71Sopenharmony_ci 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // The key length is 192 bits, that is, 24 bytes. 67e41f4b71Sopenharmony_ci let keyMaterial = new Uint8Array(arr); 68e41f4b71Sopenharmony_ci return { data: keyMaterial }; 69e41f4b71Sopenharmony_ci } 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci function testConvertSymKey() { 72e41f4b71Sopenharmony_ci // Create a SymKeyGenerator instance. 73e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); 74e41f4b71Sopenharmony_ci // Convert the data into a symmetric key. 75e41f4b71Sopenharmony_ci let keyMaterialBlob = genKeyMaterialBlob(); 76e41f4b71Sopenharmony_ci let key = symKeyGenerator.convertKeySync(keyMaterialBlob); 77e41f4b71Sopenharmony_ci let encodedKey = key.getEncoded(); // Obtain the binary data of the symmetric key and output the data as a byte array. The length is 24 bytes. 78e41f4b71Sopenharmony_ci console.info('key getEncoded hex' + encodedKey.data); 79e41f4b71Sopenharmony_ci } 80e41f4b71Sopenharmony_ci ``` 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci## Converting Binary Data into an HMAC Key 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ciFor details about the algorithm specifications, see [HMAC](crypto-sym-key-generation-conversion-spec.md#hmac). 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci1. Obtain the HMAC binary key and encapsulate it into a **DataBlob** object. 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci2. Use [cryptoFramework.createSymKeyGenerator](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#cryptoframeworkcreatesymkeygenerator) with the string parameter **'HMAC'** to create a symmetric key generator (**SymKeyGenerator**) object for an HMAC key of [1, 32768] bits. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci3. Use [SymKeyGenerator.convertKey](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkey-1) to convert the binary data into a symmetric key (**SymKey**). 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci4. Use [SymKey.getEncoded](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#getencoded) to obtain the binary data of the key. 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci- Example: Convert binary data into an HMAC key in await mode. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci ```ts 97e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 98e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci async function testConvertHmacKey() { 101e41f4b71Sopenharmony_ci // The symmetric key length is 64 bytes and 512 bits. 102e41f4b71Sopenharmony_ci let keyMessage = '12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh'; 103e41f4b71Sopenharmony_ci let keyBlob: cryptoFramework.DataBlob = { 104e41f4b71Sopenharmony_ci data : new Uint8Array(buffer.from(keyMessage, 'utf-8').buffer) 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('HMAC'); 107e41f4b71Sopenharmony_ci let key = await symKeyGenerator.convertKey(keyBlob); 108e41f4b71Sopenharmony_ci let encodedKey = key.getEncoded(); 109e41f4b71Sopenharmony_ci console.info('key encoded data: ' + encodedKey.data); 110e41f4b71Sopenharmony_ci } 111e41f4b71Sopenharmony_ci ``` 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci- Example using synchronous API [convertKeySync](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#convertkeysync12): 114e41f4b71Sopenharmony_ci ```ts 115e41f4b71Sopenharmony_ci import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 116e41f4b71Sopenharmony_ci import { buffer } from '@kit.ArkTS'; 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci function testConvertKeySync() { 119e41f4b71Sopenharmony_ci // The symmetric key length is 64 bytes and 512 bits. 120e41f4b71Sopenharmony_ci let keyMessage = '12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh'; 121e41f4b71Sopenharmony_ci let keyBlob: cryptoFramework.DataBlob = { 122e41f4b71Sopenharmony_ci data : new Uint8Array(buffer.from(keyMessage, 'utf-8').buffer) 123e41f4b71Sopenharmony_ci } 124e41f4b71Sopenharmony_ci let symKeyGenerator = cryptoFramework.createSymKeyGenerator('HMAC'); 125e41f4b71Sopenharmony_ci let key = symKeyGenerator.convertKeySync(keyBlob); 126e41f4b71Sopenharmony_ci let encodedKey = key.getEncoded(); 127e41f4b71Sopenharmony_ci console.info('key encoded data: ' + encodedKey.data); 128e41f4b71Sopenharmony_ci } 129e41f4b71Sopenharmony_ci ``` 130