1# Symmetric Key Encryption and Decryption Algorithm Specifications
2
3
4This topic describes the supported algorithms and specifications for symmetric key encryption and decryption.
5
6
7For details about the cipher modes supported by each algorithm, see the specifications of each algorithm.
8
9
10## AES
11
12The 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).
13
14AES 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.
15
16Because 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.
17
18> **NOTE**
19>
20> - In ECB and CBC modes, the plaintext must be padded if its length is not an integer multiple of 128 bits.
21> - 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.
22
23The 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.
24
25- 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.
26
27  Example:
28  - 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**.
29
30  - 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**.
31
32  | Cipher Mode| Key Length (Bit)| Padding Mode| API Version|
33  | -------- | -------- | -------- | -------- |
34  | ECB | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
35  | CBC | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
36  | CTR | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
37  | OFB | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
38  | CFB | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
39  | GCM | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
40  | CCM | [128\|192\|256] | [NoPadding\|PKCS5\|PKCS7] | 9+ |
41
42- 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.
43
44  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**.
45
46
47## 3DES
48
49[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.
50
51The 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).
52
53DES 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.
54
55Because 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.
56
57> **NOTE**
58>
59> In ECB and CBC modes, the plaintext must be padded if its length is not an integer multiple of 64 bits.
60
61The 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.
62
63- 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.
64
65  Example:
66  - 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**.
67
68  - 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**.
69
70  | Cipher Mode| Key Length (Bit)| Padding Mode| API Version|
71  | -------- | -------- | -------- | -------- |
72  | ECB | 192 | [NoPadding\|PKCS5\|PKCS7] | 9+ |
73  | CBC | 192 | [NoPadding\|PKCS5\|PKCS7] | 9+ |
74  | OFB | 192 | [NoPadding\|PKCS5\|PKCS7] | 9+ |
75  | CFB | 192 | [NoPadding\|PKCS5\|PKCS7] | 9+ |
76
77- 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.
78  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**.
79
80
81## SM4
82
83The 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).
84
85SM4 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.
86
87Because 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.
88
89> **NOTE**
90>
91> In ECB and CBC modes, the plaintext must be padded if its length is not an integer multiple of 128 bits.
92
93The 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.
94
95- 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 (_).
96
97  Example:
98  - 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**.
99
100  - 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**.
101
102  - 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**.
103
104  | Cipher Mode| Key Length (Bit)| Padding Mode| API Version|
105  | -------- | -------- | -------- | -------- |
106  | ECB | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
107  | CBC | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
108  | CTR | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
109  | OFB | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
110  | CFB | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
111  | CFB128 | 128 | [NoPadding\|PKCS5\|PKCS7] | 10+ |
112  | GCM | 128 | [NoPadding\|PKCS5\|PKCS7] | 12+ |
113
114
115## Padding Mode
116
117The 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:
118
119- **NoPadding**: no padding. The length of the input data must match the block length.
120
121- **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.
122
123- **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.
124
125For 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