This topic uses AES and SM4 as an example to describe how to generate a symmetric key and obtain the binary data.
The symmetric key object may be used for subsequent encryption and decryption operations, and binary data may be used for storage or transmission.
For the corresponding algorithm specifications, see Symmetric Key Generation and Conversion Specifications: AES.
Call cryptoFramework.createSymKeyGenerator with the string parameter 'AES256' to create a symmetric key generator (SymKeyGenerator) object for a 256-bit AES key.
Call SymKeyGenerator.generateSymKey to randomly generate a symmetric key (SymKey) object.
Call SymKey.getEncoded to obtain the binary data of the key.
Example: Randomly generate a 256-bit AES key (using promise-based APIs).
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
function testGenerateAesKey() {
// Create a SymKeyGenerator instance.
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES256');
// Use SymKeyGenerator to randomly generate a symmetric key.
let promiseSymKey = symKeyGenerator.generateSymKey();
promiseSymKey.then(key => {
// Obtain the binary data of the symmetric key and output a 256-bit key, which is of 32 bytes.
let encodedKey = key.getEncoded();
console.info('key hex: ' + encodedKey.data);
}).catch((error: BusinessError) => {
console.error(`testGenerateAesKey failed: errCode: ${error.code}, message: ${error.message}`);
});
} Example using synchronous API generateSymKeySync:
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
function testSyncGenerateAesKey() {
try {
// Create a SymKeyGenerator instance.
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES256');
// Use the key generator to randomly generate a symmetric key.
let symKey = symKeyGenerator.generateSymKeySync();
// Obtain the binary data of the symmetric key and output a 256-bit key. The length is 32 bytes.
let encodedKey = symKey.getEncoded();
console.info('key hex: ' + encodedKey.data);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`testSyncGenerateAesKey failed: errCode: ${e.code}, errMsg: ${e.message}`);
}
} For the corresponding algorithm specifications, see Symmetric Key Generation and Conversion Specifications: SM4.
Call cryptoFramework.createSymKeyGenerator with the string parameter 'SM4_128' to create a symmetric key generator (SymKeyGenerator) object for a 128-bit SM4 key.
If you need to use other algorithms, modify the string parameter.
Call SymKeyGenerator.generateSymKey to randomly generate a symmetric key (SymKey) object.
Call SymKey.getEncoded to obtain the binary data of the key.
Example: Randomly generate a 128-bit SM4 key (using promise-based APIs).
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
function testGenerateSM4Key() {
// Create a SymKeyGenerator instance.
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('SM4_128');
// Use SymKeyGenerator to randomly generate a symmetric key.
let promiseSymKey = symKeyGenerator.generateSymKey();
promiseSymKey.then(key => {
// Obtain the binary data of the symmetric key and output a 128-bit byte stream, which is of 16 bytes.
let encodedKey = key.getEncoded();
console.info('key hex: ' + encodedKey.data);
}).catch((error: BusinessError) => {
console.error(`testGenerateSM4Key failed: errCode: ${error.code}, message: ${error.message}`);
});
} Example using synchronous API generateSymKeySync:
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
function testSyncGenerateSm4Key() {
try {
// Create a SymKeyGenerator instance.
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('SM4_128');
// Randomly generate a symmetric key using the key generator.
let symKey = symKeyGenerator.generateSymKeySync();
// Obtain the binary data of the symmetric key and output a 128-bit byte stream. The length is 16 bytes.
let encodedKey = symKey.getEncoded();
console.info('key hex: ' + encodedKey.data);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`testSyncGenerateSm4Key failed: errCode: ${e.code}, errMsg: ${e.message}`);
}
}