1e41f4b71Sopenharmony_ci# Symmetric Key Encryption and Decryption Algorithm Specifications
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThis topic describes the supported algorithms and specifications for symmetric key encryption and decryption.
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciFor details about the cipher modes supported by each algorithm, see the specifications of each algorithm.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## AES
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciThe Crypto framework provides the following cipher modes for [AES](crypto-sym-key-generation-conversion-spec.md#aes) encryption and decryption: ECB, CBC, OFB, CFB, CTR, GCM, and CCM. The encryption and decryption parameters vary depending on the cipher mode. For details, see [ParamsSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#paramsspec).
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ciAES is a block cipher, with a fixed block size of 128 bits. If the last block is less than 128 bits (16 bytes), you can specify the [padding mode](#padding-mode) to pad data.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciBecause the data is padded to the block size, **PKCS5** and **PKCS7** used in the Crypto framework use the block size as the padding length. That is, data is padded to 16 bytes for AES encryption.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci> **NOTE**
19e41f4b71Sopenharmony_ci>
20e41f4b71Sopenharmony_ci> - In ECB and CBC modes, the plaintext must be padded if its length is not an integer multiple of 128 bits.
21e41f4b71Sopenharmony_ci> - In CCM encryption mode, the additional authentication data (AAD) must be specified and its length must be greater than 1 byte and less than 2048 bytes.
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciThe AES encryption and decryption can be implemented based a string parameter. When creating a **Cipher** instance, you need to specify the algorithm specifications in a string parameter. The string parameter consists of the symmetric key type (algorithm and key length), cipher block mode, and padding mode with a vertical bar (|) in between.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci- In the following table, the options included in the square brackets ([]) are mutually exclusive. You can use only one of them in a string parameter.
26e41f4b71Sopenharmony_ci  
27e41f4b71Sopenharmony_ci  Example:
28e41f4b71Sopenharmony_ci  - If the cipher block mode is ECB and padding mode is **PKCS7** for a 128-bit AES key, the string parameter is **AES128|ECB|PKCS7**.
29e41f4b71Sopenharmony_ci  
30e41f4b71Sopenharmony_ci  - If the cipher block mode is CFB and padding mode is **NoPadding** for a 256-bit AES key, the string parameter is **AES256|CFB|NoPadding**.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci  | Cipher Mode | Key Length (Bit) | Padding Mode | API Version |
33e41f4b71Sopenharmony_ci  | -------- | -------- | -------- | -------- |
34e41f4b71Sopenharmony_ci  | ECB | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
35e41f4b71Sopenharmony_ci  | CBC | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
36e41f4b71Sopenharmony_ci  | CTR | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
37e41f4b71Sopenharmony_ci  | OFB | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
38e41f4b71Sopenharmony_ci  | CFB | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
39e41f4b71Sopenharmony_ci  | GCM | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
40e41f4b71Sopenharmony_ci  | CCM | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci- Since API version 10, symmetric encryption and decryption support the algorithm specifications without the key length. If the symmetric key type does not contain the key length, the encryption and decryption operations vary with the actual key length.
43e41f4b71Sopenharmony_ci  
44e41f4b71Sopenharmony_ci  For example, if the block mode is CFB and the padding mode is **NoPadding** for an AES key with key length not specified, the string parameter is **AES|CFB|NoPadding**.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci## 3DES
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci[3DES](crypto-sym-key-generation-conversion-spec.md#3des) encryption and decryption apply the DES cipher three times to each data block to obtain the ciphertext or plaintext.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ciThe Crypto framework provides the following cipher modes for 3DES encryption and decryption: ECB, CBC, OFB, and CFB. The encryption and decryption parameters vary depending on the cipher mode. For details, see [ParamsSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#paramsspec).
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciDES is a block cipher, with a fixed block size of 64 bits. If the last block is less than 64 bits (8 bytes), you can specify the [padding mode](#padding-mode) to pad data.
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ciBecause the data is padded to the block size, **PKCS5** and **PKCS7** used in the Crypto framework use the block size as the padding length. That is, data is padded to 8 bytes for 3DES encryption.
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci> **NOTE**
58e41f4b71Sopenharmony_ci>
59e41f4b71Sopenharmony_ci> In ECB and CBC modes, the plaintext must be padded if its length is not an integer multiple of 64 bits.
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ciThe 3DES encryption and decryption can be implemented based a string parameter. When creating a **Cipher** instance, you need to specify the algorithm specifications in a string parameter. The string parameter consists of the symmetric key type (algorithm and key length), cipher block mode, and padding mode with a vertical bar (|) in between.
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci- In the following table, the options included in the square brackets ([]) are mutually exclusive. You can use only one of them in a string parameter.
64e41f4b71Sopenharmony_ci  
65e41f4b71Sopenharmony_ci  Example:
66e41f4b71Sopenharmony_ci  - If the cipher block mode is ECB and padding mode is **PKCS7** for a 192-bit 3DES key, the string parameter is **3DES192|ECB|PKCS7**.
67e41f4b71Sopenharmony_ci  
68e41f4b71Sopenharmony_ci  - If the cipher block mode is OFB and padding mode is **NoPadding** for a 192-bit 3DES key, the string parameter is **3DES192|OFB|NoPadding**.
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci  | Cipher Mode | Key Length (Bit) | Padding Mode | API Version |
71e41f4b71Sopenharmony_ci  | -------- | -------- | -------- | -------- |
72e41f4b71Sopenharmony_ci  | ECB | 192 | [NoPadding\|PKCS5\|PKCS7] | 9+ |
73e41f4b71Sopenharmony_ci  | CBC | 192 | [NoPadding\|PKCS5\|PKCS7] | 9+ |
74e41f4b71Sopenharmony_ci  | OFB | 192 | [NoPadding\|PKCS5\|PKCS7] | 9+ |
75e41f4b71Sopenharmony_ci  | CFB | 192 | [NoPadding\|PKCS5\|PKCS7] | 9+ |
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci- Since API version 10, symmetric encryption and decryption support the algorithm specifications without the key length. If the symmetric key type does not contain the key length, the encryption and decryption operations vary with the actual key length.
78e41f4b71Sopenharmony_ci  For example, if the block mode is CFB and the padding mode is **NoPadding** for a 3DES key with key length not specified, the string parameter is **3DES|CFB|NoPadding**.
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci## SM4
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ciThe Crypto framework provides the following cipher modes for [SM4](crypto-sym-key-generation-conversion-spec.md#sm4) encryption and decryption: ECB, CBC, CTR, OFB, CFB, CFB128, and GCM. The encryption and decryption parameters vary depending on the cipher mode. For details, see [ParamsSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#paramsspec).
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ciSM4 is a block cipher, with a fixed block size of 128 bits. If the last block is less than 128 bits (16 bytes), you can specify the [padding mode](#padding-mode) to pad data.
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ciBecause the data is padded to the block size, **PKCS5** and **PKCS7** used in the Crypto framework use the block size as the padding length. That is, data is padded to 16 bytes for SM4 encryption.
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci> **NOTE**
90e41f4b71Sopenharmony_ci>
91e41f4b71Sopenharmony_ci> In ECB and CBC modes, the plaintext must be padded if its length is not an integer multiple of 128 bits.
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ciThe SM4 encryption and decryption can be implemented based a string parameter. When creating a **Cipher** instance, you need to specify the algorithm specifications in a string parameter. The string parameter consists of the symmetric key type (algorithm_key length), cipher block mode, and padding mode with a vertical bar (|) in between.
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci- In the following table, the options included in the square brackets ([]) are mutually exclusive. You can use only one of them in a string parameter. The SM4 algorithm and key length are separated by an underscore (_).
96e41f4b71Sopenharmony_ci  
97e41f4b71Sopenharmony_ci  Example:
98e41f4b71Sopenharmony_ci  - If the cipher block mode is ECB and padding mode is **PKCS7** for a 128-bit SM4 key, the string parameter is **SM4_128|ECB|PKCS7**.
99e41f4b71Sopenharmony_ci  
100e41f4b71Sopenharmony_ci  - If the cipher block mode is CFB and padding mode is **NoPadding** for a 128-bit SM4 key, the string parameter is **SM4_128|CFB|NoPadding**.
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci  - If the cipher block mode is GCM and padding mode is **NoPadding** for a 128-bit SM4 key, the string parameter is **SM4_128|GCM|NoPadding**.
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci  | Cipher Mode | Key Length (Bit) | Padding Mode | API Version |
105e41f4b71Sopenharmony_ci  | -------- | -------- | -------- | -------- |
106e41f4b71Sopenharmony_ci  | ECB | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
107e41f4b71Sopenharmony_ci  | CBC | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
108e41f4b71Sopenharmony_ci  | CTR | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
109e41f4b71Sopenharmony_ci  | OFB | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
110e41f4b71Sopenharmony_ci  | CFB | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
111e41f4b71Sopenharmony_ci  | CFB128 | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
112e41f4b71Sopenharmony_ci  | GCM | 128 | [NoPadding\|PKCS5\|PKCS7] | 12+ |
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci## Padding Mode
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ciThe block cipher algorithm has a fixed block length. If the length of the last block does not meet the requirement, data will be added to extend the block to the required length based on the padding mode. The following padding modes are supported:
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci- **NoPadding**: no padding. The length of the input data must match the block length.
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci- **PKCS5**: pads a block cipher with a block size of 8 bytes. PKCS#5 applies padding in whole bytes. The value of each added byte is the number of bytes that are added.
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci- **PKCS7**: pads a block cipher with a block size from 1 to 255 bytes. The padding scheme is the same as that of PKCS#5. PKCS#5 is defined for 8-byte block sizes, while PKCS#7 can work with block size ranging from 1 to 255 bytes.
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ciFor the modes that convert block ciphers into stream ciphers, such as CFB, OFB, CTR, GCM, and CCM, padding is not required. Therefore, **NoPadding** is used no matter whether the padding mode is specified.
126