1e41f4b71Sopenharmony_ci# Encryption and Decryption by Segment 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe Crypto framework does not have a limit on the amount of the data to be passed in one time or cumulatively during the encryption/decryption process. However, when the amount of data is greater than 2 MB, you are advised to pass in and encrypt/decrypt the data by segment to improve processing efficiency. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## Symmetric Encryption and Decryption 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciUse [Cipher.update](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#update-1) to pass in and encrypt/decrypt the data of a symmetric key by segment. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciYou can set the amount of the data to be passed in each time (**updateLength** in the example) and call **update()** multiple times to pass in the data. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciCurrently, the amount of data that can be passed in each time cannot exceed **INT_MAX** (maximum length of the Uint8Array type). 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciThe value returned by each **update** and **doFinal** must be checked. If the return value is not null, obtain and combine the data segments into the complete ciphertext or plaintext. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciExample: [Encryption and Decryption by Segment with an AES Symmetric Key (GCM Mode)](crypto-aes-sym-encrypt-decrypt-gcm-by-segment.md) 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciExample: [Encryption and Decryption by Segment with an SM4 Symmetric Key (GCM Mode)](crypto-sm4-sym-encrypt-decrypt-gcm-by-segment.md) 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## Asymmetric Encryption and Decryption 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciUse [Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1) and [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1) to pass in and encrypt/decrypt the data of an asymmetric key pair by segment. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciIf the plaintext length is greater than the data length supported by a single encryption (see [Asymmetric Key Encryption and Decryption Algorithm Specifications](crypto-asym-encrypt-decrypt-spec.md)), divide the data to be encrypted into data segments of an appropriate length and encrypt each data segment. Specifically, create a **Cipher** instance and call [Cipher.init](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#init-1) and [Cipher.doFinal](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#dofinal-1). 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciThe amount of data to be passed in each time varies, depending on the key specifications. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci- RSA: The input data varies depending on the padding mode. Determine the amount of the data to be passed in at a time based on [RSA specifications](crypto-asym-encrypt-decrypt-spec.md#rsa). 28e41f4b71Sopenharmony_ci- SM2: The amount of the data to be encrypted must be of the fixed length. For details, see [SM2](crypto-asym-encrypt-decrypt-spec.md#sm2). 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ciThe value returned by each **doFinal** must be checked. If the return value is not null, obtain and combine the data segments into the complete ciphertext or plaintext. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ciExample: [Encryption and Decryption by Segment with an RSA Asymmetric Key Pair](crypto-rsa-asym-encrypt-decrypt-by-segment.md) 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci## FAQs 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci- Does the cipher mode affect the amount of the data to be passed in by each **update** in encryption and decryption by segment? 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci You can set the amount of the data to be passed in by each **update**. It has nothing to do with the cipher mode. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci The encryption/decryption parameters vary with the cipher mode. For details, see [ParamsSpec](../../reference/apis-crypto-architecture-kit/js-apis-cryptoFramework.md#paramsspec). 41