• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Encryption Algorithm
2
3> **NOTE**<br>
4>
5> 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.
6
7
8## Modules to Import
9
10
11```js
12import cipher from '@system.cipher'
13```
14
15
16## cipher.rsa
17
18rsa(Object): void
19
20Encrypts or decrypts data using RSA.
21
22**System capability**: SystemCapability.Security.Cipher
23
24**Parameters**
25
26| Name| Type| Mandatory| Description|
27| -------- | -------- | -------- | -------- |
28| action | string | Yes| Action to perform. The options are as follows:<br>-&nbsp;encrypt<br>-&nbsp;decrypt|
29| text | string | Yes| Text to be encrypted or decrypted.<br> The text to be encrypted must be common text that meets the following requirement:<br> Maximum text length = Key length/8 - 66<br>For example, if the key is of 1024 bytes, the text to be encrypted cannot exceed 62 bytes (1024/8 -66 = 62).<br> The text to be decrypted must be binary text encoded in Base64. The default format is used for Base64 encoding.|
30| key | string | Yes| RSA key. The key is used as a public key in encryption and a private key in decryption.|
31| transformation | string | No| RSA padding. The default value is **RSA/None/OAEPWithSHA256AndMGF1Padding**.|
32| success | Function | No| Called when data is encrypted or decrypted successfully.|
33| fail | Function | No| Called when data fails to be encrypted or decrypted.|
34| complete | Function | No| Called when the execution is complete.|
35
36**Example**
37
38```js
39export default {
40  rsa() {
41    cipher.rsa({
42      // Encrypt data.
43      action: 'encrypt',
44      // Text to be encrypted.
45      text: 'hello',
46      // Base64-encoded public key used for encryption.
47      key:
48     'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCx414QSP3RsYWYzf9mkBMiBAXo\n' +
49     '6S7Lpva1fKlcuVxjoFC1iMnzD4mC0uiL4k5MNi43J64c7dbqi3qAJjdAtuwQ6NZJ\n' +
50     '+Enz0RzmVFh/4yk6lmqRzuEFQqhQqSZzaLq6sq2N2G0Sv2Xl3sLvqAfe2HNm2oBw\n' +
51     'jBpApTJ3TeneOo6Z5QIDAQAB',
52      success: function(data) {
53        console.log(`Handling successful:${data.text}`);
54      },
55      fail: function(data, code) {
56        console.log(`### Failed to encrypt cipher.rsa ### ${code}:${data}`);
57      },
58      complete: function() {
59        console.log(`operation complete!`);
60      }
61      });
62      cipher.rsa({
63        // Decrypt data.
64        action: 'decrypt',
65        // Text to be decrypted, which is binary text encoded in Base64. The decrypted text is "hello".
66        text:
67       'EPeCFPib6ayKbA0M6oSywARvFZ8dFYfjQv3nY8ikZGtS9UHq2sLPvAfpeIzggSiCxqbWeCftP1XQ\n' +
68       'Sa+jEpzFlT1qoSTunBbrYzugPTajIJDTg6R1IRsF/J+mmakn0POVPvi4jCo9wqavB324Bx0Wipnc\n' +
69       'EU5WO0oBHo5l4x6dTpU=',
70           // Base64-encoded private key used for decryption.
71         key:
72        'MIICXgIBAAKBgQCx414QSP3RsYWYzf9mkBMiBAXo6S7Lpva1fKlcuVxjoFC1iMnz\n' +
73        'D4mC0uiL4k5MNi43J64c7dbqi3qAJjdAtuwQ6NZJ+Enz0RzmVFh/4yk6lmqRzuEF\n' +
74        'QqhQqSZzaLq6sq2N2G0Sv2Xl3sLvqAfe2HNm2oBwjBpApTJ3TeneOo6Z5QIDAQAB\n' +
75        'AoGBAKPNtoRQcklxqo+2wQP0j2m3Qqnib1DggjVEgb/8f/LNYQSI3U2QdROemryU\n' +
76        'u3y6N3xacZ359PktTrRKfH5+8ohmHGhIuPAnefp6bLvAFUcl4t1xm74Cow62Kyw3\n' +
77        'aSbmuTG98dxPA1sXD0jiprdtsq2wQ9CoKNyY7/d/pKoqxNuBAkEA4GytZ60NCTj9\n' +
78        'w24jACFeko5YqCFY/TTLoc4SQvWtFMnimRPclLZhtUIK0P8dib71UFedx+AxklgL\n' +
79        'A5gjcfo+2QJBAMrqiwyCh3OQ5DhyRPDwt87x1/jg5fy4hhete2ufSf2FoQCVqO+w\n' +
80        'PKoljdXmJeS6rGgzGibstuHLrP3tcIho4+0CQD3ZFWzF/xq0jxKlrpWhnJuNCRfE\n' +
81        'oO6e9yNvVA8J/5oEDSOcmqSNIp4+RhbUx8InUxnCG6Ryv5aSFu71pYcKrPkCQQCL\n' +
82        'RUGcm3ZGTnslduB0knNF+V2ndwzDUQ7P74UXT+PjurTPhujFYiuxCEd6ORVnEOzG\n' +
83        'M9TORIgdH8MjIbWsGnndAkEAw9yURDaorE8IYPLF2IEn09g1uzvWPs3phDb6smVx\n' +
84        '8GfqIdUNf+aCG5TZK/kXBF1sqcsi7jXMAf4jBlejVbSVZg==',
85         success: function(data) {
86           console.log(`Handling successful:${data.text}`);
87         },
88         fail: function(data, code) {
89           console.log(`### Failed to encrypt cipher.rsa ### ${code}:${data}`);
90         },
91         complete: function() {
92           console.log(`operation complete!`);
93         }
94       });
95   }
96}
97```
98
99
100## cipher.aes
101
102aes(Object): void
103
104Encrypts or decrypts data using AES.
105
106**System capability**: SystemCapability.Security.Cipher
107
108**Parameters**
109
110| Name| Type| Mandatory| Description|
111| -------- | -------- | -------- | -------- |
112| action | string | Yes| Action to perform. The options are as follows:<br>-&nbsp;encrypt<br>-&nbsp;decrypt|
113| text | string | Yes| Text to be encrypted or decrypted.<br> The text to be encrypted must be common text. The text to be decrypted must be binary text encoded in Base64. The default format is used for Base64 encoding.|
114| key | string | Yes| Key used for encryption or decryption. It is a string encoded in Base64.|
115| transformation | string | No| Encryption mode and padding of the AES algorithm. The default value is **AES/CBC/PKCS5Padding**.|
116| iv | string | No| Initialization vector (IV) for AES-based encryption and decryption. The value is a string encoded in Base64. The default value is the key value.|
117| ivOffset | string | No| Offset of the IV for AES-based encryption and decryption. The default value is **0**, which is the only value supported.|
118| ivLen | string | No| Length of the IV, in bytes. This field is reserved. The default value is **16**, which is the only value supported.|
119| success | Function | No| Called when data is encrypted or decrypted successfully.|
120| fail | Function | No| Called when data fails to be encrypted or decrypted.|
121| complete | Function | No| Called when the execution is complete.|
122
123**Example**
124
125```js
126export default {
127  aes() {
128    cipher.aes({
129      // Encrypt data.
130      action: 'encrypt',
131      // Text to be encrypted.
132      text: 'hello',
133      // Base64-encoded key.
134      key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',
135      transformation: 'AES/CBC/PKCS5Padding',
136      ivOffset: '0',
137      ivLen: '16',
138      success: function(data) {
139        console.log(`Handling successful:${data.text}`);
140        },
141      fail: function(data, code) {
142        console.log(`### Failed to encrypt cipher.rsa ### ${code}:${data}`);
143        },
144      complete: function() {
145        console.log(`operation complete!`);
146      }
147    });
148    cipher.aes({
149      // Decrypt data.
150      action: 'decrypt',
151      // Text to be decrypted, which is binary text encoded in Base64.
152      text: '1o0kf2HXwLxHkSh5W5NhzA==',
153       // Base64-encoded key.
154       key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',
155       transformation: 'AES/CBC/PKCS5Padding',
156       ivOffset: '0',
157       ivLen: '16',
158       success: function(data) {
159         console.log(`Handling successful:${data.text}`);
160        },
161       fail: function(data, code) {
162         console.log(`### Failed to decrypt cipher.rsa ### ${code}:${data}`);
163       },
164       complete: function() {
165         console.log(`operation complete!`);
166        }
167     });
168    });
169  }
170}
171
172```
173