# Encryption Algorithm > ![icon-note.gif](public_sys-resources/icon-note.gif) **Note:** > > - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. > - This API is defined but not implemented in OpenHarmony 3.1 Release. It will be available for use in OpenHarmony 3.1 MR. ## Modules to Import ``` import cipher from '@system.cipher' ``` ## cipher.rsa rsa(Object): void Encrypts or decrypts data using RSA. **System capability**: SystemCapability.Security.Cipher **Parameters** | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | action | string | Yes | Action type. The options are as follows:
1. **encrypt**: Encrypts data.
2. **decrypt**: Decrypts data. | | text | string | Yes | Text content to be encrypted or decrypted. The text to be encrypted must be a common text and cannot exceed the length calculated based on the formula (keySize/8 - 66). **keySize** indicates the key length. For example, if the key length is 1024 bytes, the text cannot exceed 62 bytes (1024/8 - 66 = 62). The text content to be decrypted must be a binary value encoded using Base64. The default format is used for Base64 encoding. | | key | string | Yes | Keys encrypted using RSA. During encryption, this parameter is a public key. During decryption, it is a private key. | | transformation | string | No | RSA algorithm padding. The default value is **RSA/None/OAEPWithSHA256AndMGF1Padding**. | | success | Function | No | Called when data is encrypted or decrypted successfully. | | fail | Function | No | Called when data fails to be encrypted or decrypted. | | complete | Function | No | Called when the execution is complete. | **Example** ``` export default { rsa() { cipher.rsa({ // Encrypt data. action: 'encrypt', // Text content to be encrypted text: 'hello', // Base64-encoded public key used for encryption key: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDc7GR2MrfAoefES+wrs1ns2afT\n' + 'eJXSfIkEHfPXG9fVFjaws1ho4KcZfsxlA0+SXvc83f2SVGCuzULmM2lxxRCtcUN/\n' + 'h7SoaYEeluhqFimL2AEjfSwINHCLqObJkcjCfoZpE1JCehPiDOJsyT50Auc08h/4\n' + 'jHQfanyC1nc62LqUCQIDAQAB', success: function(data) { console.log('handling success: ${data.text}'); }, fail: function(data, code) { console.log(`### cipher.rsa encrypt fail ### ${code}: ${data}`); } }); cipher.rsa({ // Decrypt data. action: 'decrypt', // The text to be decrypted is a Base64-encoded binary value, and the decrypted text is "hello". text: 'CUg3tTxTIdpCfreIxIBdws3uhd5qXLwcrVl3XDnQzZFVHyjVVCDHS16rjopaZ4C5xU2Tc8mSDzt7\n' + 'gp9vBfSwi7bMtSUvXG18DlncsKJFDkJpS5t0PkpS9YrJXrY80Gpe+ME6+6dN9bjgqMljbitDdBRf\n' + 'S/ZWNI4Q8Q0suNjNkGU=', // Base64-encoded public key used for encryption key: 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANzsZHYyt8Ch58RL\n' + '7CuzWezZp9N4ldJ8iQQd89cb19UWNrCzWGjgpxl+zGUDT5Je9zzd/ZJUYK7NQuYz\n' + 'aXHFEK1xQ3+HtKhpgR6W6GoWKYvYASN9LAg0cIuo5smRyMJ+hmkTUkJ6E+IM4mzJ\n' + 'PnQC5zTyH/iMdB9qfILWdzrYupQJAgMBAAECgYEAkibhH0DWR13U0gvYJeD08Lfd\n' + 'Sw1PMHyquEqIcho9Yv7bF3LOXjOg2EEGPx09mvuwXFgP1Kp1e67XPytr6pQQPzK7\n' + 'XAPcLPx80R/ZjZs8vNFndDOd1HgD3vSVmYQarNzmKi72tOUWMPevsaFXPHo6Xx3X\n' + '8x0wYb7XuBsQguRctTECQQD7GWX3JUiyo562iVrpTDPOXsrUxmzCrgz2OZildxMd\n' + 'Pp/PkyDrx7mEXTpk4K/XnQJ3GpJNi2iDSxDuPSAeJ/aPAkEA4Tw4+1Z43S/xH3C3\n' + 'nfulYBNyB4si6KEUuC0krcC1pDJ21Gd12efKo5VF8SaJI1ZUQOzguV+dqNsB/JUY\n' + 'OFfX5wJAB1dKv9r7MR3Peg6x9bggm5vx2h6i914XSuuMJupASM6X5X2rrLj+F3yS\n' + 'RHi9K1SPyeOg+1tkBtKfABgRZFBOyQJAbuTivUSe73AqTKuHjB4ZF0ubqgEkJ9sf\n' + 'Q2rekzm9dOFvxjZGPQo1qALX09qATMi1ZN376ukby8ZAnSafLSZ64wJBAM2V37go\n' + 'Sj44HF76ksRow8gecuQm48NCTGAGTicXg8riKog2GC9y8pMNHAezoR9wXJF7kk+k\n' + 'lz5cHyoMZ9mcd30=', success: function(data) { console.log('handling success: ${data.text}'); }, fail: function(data, code) { console.log(`### cipher.rsa decrypt fail ### ${code}: ${data}`); }, }); } } ``` ## cipher.aes aes(Object): void Encrypts or decrypts data using AES. **System capability**: SystemCapability.Security.Cipher **Parameters** | Name | Type | Mandatory | Description | | -------- | -------- | -------- | -------- | | action | string | Yes | Action type. The options are as follows:
1. **encrypt**: Encrypts data.
2. **decrypt**: Decrypts data. | | text | string | Yes | Text content to be encrypted or decrypted. The text to be encrypted must be a common text. The text content to be decrypted must be a binary value encoded using Base64. The default format is used for Base64 encoding. | | key | string | Yes | Key used for encryption or decryption, which is a character string encrypted using Base64. | | transformation | string | No | Encryption mode and padding of the AES algorithm. The default value is **AES/CBC/PKCS5Padding**. | | iv | string | No | Initial vector for AES-based encryption and decryption. The value is a character string encoded using Base64. The default value is the key value. | | ivOffset | string | No | Offset of the initial vector for AES-based encryption and decryption. The default value is **0**. | | ivLen | string | No | Length of the initial vector for AES-based encryption and decryption. The default value is **16**. | | success | Function | No | Called when data is encrypted or decrypted successfully. | | fail | Function | No | Called when data fails to be encrypted or decrypted. | | complete | Function | No | Called when the execution is complete. | **Example** ``` export default { aes() { cipher.aes({ // Encrypt data. action: 'encrypt', // Text content to be encrypted text: 'hello', // Base64-encoded key used for encryption key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=', transformation: 'AES/CBC/PKCS5Padding', ivOffset: 0, ivLen: 16, success: (data) => { console.log('handling success: ${data.text}'); }, fail: (data, code) => { console.log(`### cipher.aes encrypt fail ### ${code}: ${data}`); } }); cipher.aes({ // Decrypt data. action: 'decrypt', // Text to be decrypted, which is a Base64-encoded binary value text: 'CUg3tTxTIdpCfreIxIBdws3uhd5qXLwcrVl3XDnQzZFVHyjVVCDHS16rjopaZ4C5xU2Tc8mSDzt7\n' + 'gp9vBfSwi7bMtSUvXG18DlncsKJFDkJpS5t0PkpS9YrJXrY80Gpe+ME6+6dN9bjgqMljbitDdBRf\n' + 'S/ZWNI4Q8Q0suNjNkGU=', // Base64-encoded key used for decryption key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=', transformation: 'AES/CBC/PKCS5Padding', ivOffset: 0, ivLen: 16, success: (data) => { this.dealTxt = data.text; }, fail: (data, code) => { prompt.showToast({ message: (`### cipher.aes decrypt fail ### code = ${code}: ${data}`) }) }, }); } } ```