js-apis-system-cipher.md 9.7 KB
Newer Older
Z
zengyawen 已提交
1 2
# Encryption Algorithm

A
Annie_wang 已提交
3
> **NOTE**<br>
Z
zengyawen 已提交
4
>
A
Annie_wang 已提交
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.
Z
zengyawen 已提交
6 7 8 9 10


## Modules to Import


A
Annie_wang 已提交
11
```js
A
Annie_wang 已提交
12
import cipher from '@system.cipher';
Z
zengyawen 已提交
13 14
```

A
Annie_wang 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
## CipherResponse

Defines the response to the cipher interface called.

**System capability**: SystemCapability.Security.Cipher

| Name| Type  | Mandatory| Description        |
| ------ | ------ | ---- | ------------ |
| text   | string | Yes  | Response content.|

## CipherRsaOptions

Defines the input parameters of **cipher.rsa()**.

**System capability**: SystemCapability.Security.Cipher

| Name        | Type                                | Mandatory| Description                                                        |
| -------------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| action         | string                               | Yes  | Action to perform. The options are as follows:<br>1. **encrypt**: Encrypts data.<br>2. **decrypt**: Decrypts data.|
| 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.|
| key            | string                               | Yes  | RSA key. It is a public key in encryption and a private key in decryption.     |
| transformation | string                               | No  | RSA padding. The default value is **RSA/None/OAEPWithSHA256AndMGF1Padding**.|
| success        | (data: [CipherResponse](#cipherresponse)) => void       | No  | Called when data is encrypted or decrypted successfully.                                    |
| fail           | (data: string, code: number) => void | No  | Called when data fails to be encrypted or decrypted.                                    |
| complete       | () => void                           | No  | Called when the execution is complete.                                    |

## CipherAesOptions

Defines the input parameters of **cipher.aes()**.

**System capability**: SystemCapability.Security.Cipher

| Name        | Type                                | Mandatory| Description                                                        |
| -------------- | ------------------------------------ | ---- | ------------------------------------------------------------ |
| action         | string                               | Yes  | Action to perform. The options are as follows:<br>1. **encrypt**: Encrypts data.<br>2. **decrypt**: Decrypts data.|
| 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.|
| key            | string                               | Yes  | Key used for encryption or decryption. It is a Base64 encoded string.|
| transformation | string                               | No  | Encryption mode and padding of the AES algorithm. The default value is **AES/CBC/PKCS5Padding**.         |
| 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.|
| ivOffset       | string                               | No  | Offset of the IV for AES-based encryption and decryption. The default value is **0**, which is the only value supported.                 |
| ivLen          | string                               | No  | Length of the IV, in bytes. This field is reserved. The default value is **16**, which is the only value supported.|
| success        | (data: [CipherResponse](#cipherresponse)) => void       | No  | Called when data is encrypted or decrypted successfully.                                    |
| fail           | (data: string, code: number) => void | No  | Called when data fails to be encrypted or decrypted.                                    |
| complete       | () => void                           | No  | Called when the execution is complete.                                    |
Z
zengyawen 已提交
59 60 61

## cipher.rsa

A
Annie_wang 已提交
62
rsa(options: CipherRsaOptions): void
Z
zengyawen 已提交
63 64 65 66 67 68 69

Encrypts or decrypts data using RSA.

**System capability**: SystemCapability.Security.Cipher

**Parameters**

A
Annie_wang 已提交
70
| Name| Type| Mandatory| Description|
Z
zengyawen 已提交
71
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
72
| options | [CipherRsaOptions](#cipherrsaoptions) | Yes| Parameters set for RSA encryption or decryption.|
Z
zengyawen 已提交
73 74 75

**Example**

A
Annie_wang 已提交
76
```js
Z
zengyawen 已提交
77 78 79
export default {    
  rsa() {        
    cipher.rsa({            
A
Annie_wang 已提交
80
      // Encrypt data.           
Z
zengyawen 已提交
81
      action: 'encrypt',            
A
Annie_wang 已提交
82
      // Text to be encrypted.           
Z
zengyawen 已提交
83
      text: 'hello',            
A
Annie_wang 已提交
84
      // Base64-encoded public key used for encryption.           
A
Annie_wang 已提交
85 86 87 88 89 90
      key: 
     'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCx414QSP3RsYWYzf9mkBMiBAXo\n' + 
     '6S7Lpva1fKlcuVxjoFC1iMnzD4mC0uiL4k5MNi43J64c7dbqi3qAJjdAtuwQ6NZJ\n' + 
     '+Enz0RzmVFh/4yk6lmqRzuEFQqhQqSZzaLq6sq2N2G0Sv2Xl3sLvqAfe2HNm2oBw\n' +
     'jBpApTJ3TeneOo6Z5QIDAQAB',  
      success: function(data) {                
A
Annie_wang 已提交
91
        console.log(`Handling successful:${data.text}`);          
A
Annie_wang 已提交
92 93
      },            
      fail: function(data, code) {               
A
Annie_wang 已提交
94
        console.log(`### cipher.rsa encryption failed ### ${code}:${data}`); 
A
Annie_wang 已提交
95 96 97 98
      },
      complete: function() {
        console.log(`operation complete!`);
      }
Z
zengyawen 已提交
99 100
      });        
      cipher.rsa({            
A
Annie_wang 已提交
101
        // Decrypt data.           
Z
zengyawen 已提交
102
        action: 'decrypt',            
A
Annie_wang 已提交
103
        // Text to be decrypted, which is binary text encoded in Base64. The decrypted text is "hello".           
Z
zengyawen 已提交
104
        text:            
A
Annie_wang 已提交
105 106 107
       'EPeCFPib6ayKbA0M6oSywARvFZ8dFYfjQv3nY8ikZGtS9UHq2sLPvAfpeIzggSiCxqbWeCftP1XQ\n' +
       'Sa+jEpzFlT1qoSTunBbrYzugPTajIJDTg6R1IRsF/J+mmakn0POVPvi4jCo9wqavB324Bx0Wipnc\n' +
       'EU5WO0oBHo5l4x6dTpU=',           
A
Annie_wang 已提交
108
           // Base64-encoded private key used for decryption.           
Z
zengyawen 已提交
109
         key:            
A
Annie_wang 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123
        'MIICXgIBAAKBgQCx414QSP3RsYWYzf9mkBMiBAXo6S7Lpva1fKlcuVxjoFC1iMnz\n' +
        'D4mC0uiL4k5MNi43J64c7dbqi3qAJjdAtuwQ6NZJ+Enz0RzmVFh/4yk6lmqRzuEF\n' +
        'QqhQqSZzaLq6sq2N2G0Sv2Xl3sLvqAfe2HNm2oBwjBpApTJ3TeneOo6Z5QIDAQAB\n' +
        'AoGBAKPNtoRQcklxqo+2wQP0j2m3Qqnib1DggjVEgb/8f/LNYQSI3U2QdROemryU\n' +
        'u3y6N3xacZ359PktTrRKfH5+8ohmHGhIuPAnefp6bLvAFUcl4t1xm74Cow62Kyw3\n' +
        'aSbmuTG98dxPA1sXD0jiprdtsq2wQ9CoKNyY7/d/pKoqxNuBAkEA4GytZ60NCTj9\n' +
        'w24jACFeko5YqCFY/TTLoc4SQvWtFMnimRPclLZhtUIK0P8dib71UFedx+AxklgL\n' +
        'A5gjcfo+2QJBAMrqiwyCh3OQ5DhyRPDwt87x1/jg5fy4hhete2ufSf2FoQCVqO+w\n' +
        'PKoljdXmJeS6rGgzGibstuHLrP3tcIho4+0CQD3ZFWzF/xq0jxKlrpWhnJuNCRfE\n' +
        'oO6e9yNvVA8J/5oEDSOcmqSNIp4+RhbUx8InUxnCG6Ryv5aSFu71pYcKrPkCQQCL\n' +
        'RUGcm3ZGTnslduB0knNF+V2ndwzDUQ7P74UXT+PjurTPhujFYiuxCEd6ORVnEOzG\n' +
        'M9TORIgdH8MjIbWsGnndAkEAw9yURDaorE8IYPLF2IEn09g1uzvWPs3phDb6smVx\n' + 
        '8GfqIdUNf+aCG5TZK/kXBF1sqcsi7jXMAf4jBlejVbSVZg==',
         success: function(data) {                
A
Annie_wang 已提交
124
           console.log(`Handling successful:${data.text}`);          
A
Annie_wang 已提交
125 126
         },            
         fail: function(data, code) {               
A
Annie_wang 已提交
127
           console.log(`### cipher.rsa encryption failed ### ${code}:${data}`); 
A
Annie_wang 已提交
128 129 130 131
         },
         complete: function() {
           console.log(`operation complete!`);
         }        
Z
zengyawen 已提交
132 133 134 135 136 137 138 139
       });    
   }
}
```


## cipher.aes

A
Annie_wang 已提交
140
aes(options: CipherAesOptions): void
Z
zengyawen 已提交
141 142 143 144 145 146 147

Encrypts or decrypts data using AES.

**System capability**: SystemCapability.Security.Cipher

**Parameters**

A
Annie_wang 已提交
148
| Name| Type| Mandatory| Description|
Z
zengyawen 已提交
149
| -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
150
| options | [CipherAesOptions](#cipheraesoptions) | Yes| Parameters set for AES encryption or decryption.|
Z
zengyawen 已提交
151 152 153

**Example**

A
Annie_wang 已提交
154
```js
Z
zengyawen 已提交
155 156 157
export default {    
  aes() {        
    cipher.aes({            
A
Annie_wang 已提交
158
      // Encrypt data.           
Z
zengyawen 已提交
159
      action: 'encrypt',            
A
Annie_wang 已提交
160
      // Text to be encrypted.           
Z
zengyawen 已提交
161
      text: 'hello',            
A
Annie_wang 已提交
162
      // Base64-encoded key.           
Z
zengyawen 已提交
163 164
      key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',            
      transformation: 'AES/CBC/PKCS5Padding',            
A
Annie_wang 已提交
165 166 167
      ivOffset: '0',            
      ivLen: '16',            
      success: function(data) {                
A
Annie_wang 已提交
168
        console.log(`Handling successful:${data.text}`);          
A
Annie_wang 已提交
169 170
        },            
      fail: function(data, code) {               
A
Annie_wang 已提交
171
        console.log(`### cipher.rsa encryption failed ### ${code}:${data}`); 
A
Annie_wang 已提交
172 173 174 175
        },
      complete: function() {
        console.log(`operation complete!`);
      }
Z
zengyawen 已提交
176 177
    });        
    cipher.aes({            
A
Annie_wang 已提交
178
      // Decrypt data.           
Z
zengyawen 已提交
179
      action: 'decrypt',            
A
Annie_wang 已提交
180
      // Text to be decrypted, which is binary text encoded in Base64.           
A
Annie_wang 已提交
181
      text: '1o0kf2HXwLxHkSh5W5NhzA==',            
A
Annie_wang 已提交
182
       // Base64-encoded key.           
Z
zengyawen 已提交
183 184
       key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',            
       transformation: 'AES/CBC/PKCS5Padding',            
A
Annie_wang 已提交
185 186 187
       ivOffset: '0',            
       ivLen: '16',            
       success: function(data) {                
A
Annie_wang 已提交
188
         console.log(`Handling successful:${data.text}`);          
A
Annie_wang 已提交
189 190
        },            
       fail: function(data, code) {               
A
Annie_wang 已提交
191
         console.log(`### cipher.aes encryption failed ### ${code}:${data}`); 
A
Annie_wang 已提交
192 193 194 195 196
       },
       complete: function() {
         console.log(`operation complete!`);
        }
     });        
Z
zengyawen 已提交
197 198 199
  }
}

A
Annie_wang 已提交
200
```