• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @system.cipher (Cipher Algorithm)
2
3<!--Kit: Crypto Architecture Kit-->
4<!--Subsystem: Security-->
5<!--Owner: @zxz--3-->
6<!--Designer: @lanming-->
7<!--Tester: @PAFT-->
8<!--Adviser: @zengyawen-->
9
10> **NOTE**
11>
12> 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.
13>
14> The APIs of this module are deprecated since API version 9. You are advised to use [@ohos.security.cryptoFramework Cipher](js-apis-cryptoFramework.md#cipher).
15
16## Modules to Import
17
18
19```js
20import cipher from '@system.cipher';
21```
22
23## CipherResponse
24
25Defines the response to the cipher interface called.
26
27**System capability**: SystemCapability.Security.Cipher
28
29| Name| Type  | Readable| Writable|Description        |
30| ------ | ------ | ---- | ---- | ------------ |
31| text   | string | Yes  | No  | Response content.|
32
33
34## CipherRsaOptions
35
36Defines the input parameters of **cipher.rsa()**.
37
38**System capability**: SystemCapability.Security.Cipher
39
40| Name        | Type                                | Mandatory| Description                                                        |
41| -------------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
42| action         | string                               | Yes  | Action to perform. The options are as follows:<br>1. **encrypt**: Encrypts data.<br>2. **decrypt**: Decrypts data.|
43| text           | string                               | Yes  | Text to be encrypted or decrypted.<br> 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 to be decrypted must be a binary value encoded in Base64. The default format is used for Base64 encoding.|
44| key            | string                               | Yes  | RSA key. It is a public key in encryption and a private key in decryption.     |
45| transformation | string                               | No  | RSA padding. The default value is **RSA/None/OAEPWithSHA256AndMGF1Padding**.|
46| success        | (data: [CipherResponse](#cipherresponse)) => void       | No  | Called when data is encrypted or decrypted successfully.                                    |
47| fail           | (data: string, code: number) => void | No  | Called when data fails to be encrypted or decrypted.                                    |
48| complete       | () => void                           | No  | Called when the execution is complete.                                    |
49
50## CipherAesOptions
51
52Defines the input parameters of **cipher.aes()**.
53
54**System capability**: SystemCapability.Security.Cipher
55
56| Name        | Type                                | Mandatory| Description                                                        |
57| -------------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
58| action         | string                               | Yes  | Action to perform. The options are as follows:<br>1. **encrypt**: Encrypts data.<br>2. **decrypt**: Decrypts data.|
59| 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 a binary value encoded in Base64. The default format is used for Base64 encoding.|
60| key            | string                               | Yes  | Key used for encryption or decryption. It is a Base64 encoded string.|
61| transformation | string                               | No  | Encryption mode and padding of the AES algorithm. The default value is **AES/CBC/PKCS5Padding**.         |
62| 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.|
63| ivOffset       | string                               | No  | Offset of the IV for AES-based encryption and decryption. The default value is **0**, which is the only value supported.                 |
64| ivLen          | string                               | No  | Length of the IV, in bytes. This field is reserved. The default value is **16**, which is the only value supported.|
65| success        | (data: [CipherResponse](#cipherresponse)) => void       | No  | Called when data is encrypted or decrypted successfully.                                    |
66| fail           | (data: string, code: number) => void | No  | Called when data fails to be encrypted or decrypted.                                    |
67| complete       | () => void                           | No  | Called when the execution is complete.                                    |
68
69## cipher.rsa
70
71rsa(options: CipherRsaOptions): void
72
73Encrypts or decrypts data using RSA.
74
75**System capability**: SystemCapability.Security.Cipher
76
77**Parameters**
78
79| Name| Type| Mandatory| Description|
80| -------- | -------- | -------- | -------- |
81| options | [CipherRsaOptions](#cipherrsaoptions) | Yes| Parameters set for RSA encryption or decryption.|
82
83**Example**
84
85```js
86export default {
87  rsa() {
88    cipher.rsa({
89      // Encrypt data.
90      action: 'encrypt',
91      // Text to be encrypted.
92      text: 'hello',
93      // Base64-encoded public key used for encryption.
94      key:
95     'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCx414QSP3RsYWYzf9mkBMiBAXo\n' +
96     '6S7Lpva1fKlcuVxjoFC1iMnzD4mC0uiL4k5MNi43J64c7dbqi3qAJjdAtuwQ6NZJ\n' +
97     '+Enz0RzmVFh/4yk6lmqRzuEFQqhQqSZzaLq6sq2N2G0Sv2Xl3sLvqAfe2HNm2oBw\n' +
98     'jBpApTJ3TeneOo6Z5QIDAQAB',
99      success: function(data) {
100        console.log(`handling success:${data.text}`);
101      },
102      fail: function(data, code) {
103        console.log(`### cipher.rsa encrypt fail ### ${code}:${data}`);
104      },
105      complete: function() {
106        console.log(`operation complete!`);
107      }
108      });
109      cipher.rsa({
110        // Decrypt data.
111        action: 'decrypt',
112        // Text to be decrypted, which is binary text encoded in Base64. The decrypted text is "hello".
113        text:
114       'EPeCFPib6ayKbA0M6oSywARvFZ8dFYfjQv3nY8ikZGtS9UHq2sLPvAfpeIzggSiCxqbWeCftP1XQ\n' +
115       'Sa+jEpzFlT1qoSTunBbrYzugPTajIJDTg6R1IRsF/J+mmakn0POVPvi4jCo9wqavB324Bx0Wipnc\n' +
116       'EU5WO0oBHo5l4x6dTpU=',
117         // Base64-encoded private key used for decryption.
118         key:
119        'MIICXgIBAAKBgQCx414QSP3RsYWYzf9mkBMiBAXo6S7Lpva1fKlcuVxjoFC1iMnz\n' +
120        'D4mC0uiL4k5MNi43J64c7dbqi3qAJjdAtuwQ6NZJ+Enz0RzmVFh/4yk6lmqRzuEF\n' +
121        'QqhQqSZzaLq6sq2N2G0Sv2Xl3sLvqAfe2HNm2oBwjBpApTJ3TeneOo6Z5QIDAQAB\n' +
122        'AoGBAKPNtoRQcklxqo+2wQP0j2m3Qqnib1DggjVEgb/8f/LNYQSI3U2QdROemryU\n' +
123        'u3y6N3xacZ359PktTrRKfH5+8ohmHGhIuPAnefp6bLvAFUcl4t1xm74Cow62Kyw3\n' +
124        'aSbmuTG98dxPA1sXD0jiprdtsq2wQ9CoKNyY7/d/pKoqxNuBAkEA4GytZ60NCTj9\n' +
125        'w24jACFeko5YqCFY/TTLoc4SQvWtFMnimRPclLZhtUIK0P8dib71UFedx+AxklgL\n' +
126        'A5gjcfo+2QJBAMrqiwyCh3OQ5DhyRPDwt87x1/jg5fy4hhete2ufSf2FoQCVqO+w\n' +
127        'PKoljdXmJeS6rGgzGibstuHLrP3tcIho4+0CQD3ZFWzF/xq0jxKlrpWhnJuNCRfE\n' +
128        'oO6e9yNvVA8J/5oEDSOcmqSNIp4+RhbUx8InUxnCG6Ryv5aSFu71pYcKrPkCQQCL\n' +
129        'RUGcm3ZGTnslduB0knNF+V2ndwzDUQ7P74UXT+PjurTPhujFYiuxCEd6ORVnEOzG\n' +
130        'M9TORIgdH8MjIbWsGnndAkEAw9yURDaorE8IYPLF2IEn09g1uzvWPs3phDb6smVx\n' +
131        '8GfqIdUNf+aCG5TZK/kXBF1sqcsi7jXMAf4jBlejVbSVZg==',
132         success: function(data) {
133           console.log(`handling success:${data.text}`);
134         },
135         fail: function(data, code) {
136           console.log(`### cipher.rsa decrypt fail ### ${code}:${data}`);
137         },
138         complete: function() {
139           console.log(`operation complete!`);
140         }
141       });
142   }
143}
144```
145
146
147## cipher.aes
148
149aes(options: CipherAesOptions): void
150
151Encrypts or decrypts data using AES.
152
153**System capability**: SystemCapability.Security.Cipher
154
155**Parameters**
156
157| Name| Type| Mandatory| Description|
158| -------- | -------- | -------- | -------- |
159| options | [CipherAesOptions](#cipheraesoptions) | Yes| Parameters set for AES encryption or decryption.|
160
161**Example**
162
163```js
164export default {
165  aes() {
166    cipher.aes({
167      // Encrypt data.
168      action: 'encrypt',
169      // Text to be encrypted.
170      text: 'hello',
171      // Base64-encoded key.
172      key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',
173      transformation: 'AES/CBC/PKCS5Padding',
174      ivOffset: '0',
175      ivLen: '16',
176      success: function(data) {
177        console.log(`handling success:${data.text}`);
178        },
179      fail: function(data, code) {
180        console.log(`### cipher.aes encrypt fail ### ${code}:${data}`);
181        },
182      complete: function() {
183        console.log(`operation complete!`);
184      }
185    });
186    cipher.aes({
187      // Decrypt data.
188      action: 'decrypt',
189      // Text to be decrypted, which is binary text encoded in Base64.
190      text: '1o0kf2HXwLxHkSh5W5NhzA==',
191       // Base64-encoded key.
192       key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',
193       transformation: 'AES/CBC/PKCS5Padding',
194       ivOffset: '0',
195       ivLen: '16',
196       success: function(data) {
197         console.log(`handling success:${data.text}`);
198        },
199       fail: function(data, code) {
200         console.log(`### cipher.aes decrypt fail ### ${code}:${data}`);
201       },
202       complete: function() {
203         console.log(`operation complete!`);
204        }
205     });
206  }
207}
208
209```
210