We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
quickApp ReferencesQuick AppAPISecurityCryptographic Algorithm

Cryptographic Algorithm

API Declaration

Add the following configuration to the features attribute in the manifest.json file:

Collapse
Word wrap
Dark theme
Copy code
  1. {"name": "system.cipher"}

Module Import

Add the following configuration to the script element on the page where the API will be called:

Collapse
Word wrap
Dark theme
Copy code
  1. import cipher from '@system.cipher'

Or

Collapse
Word wrap
Dark theme
Copy code
  1. var cipher = require("@system.cipher")

Application Scope

Expand

Item

Description

Applicable devices

Mobile phones, tablets, and Vision products

Applicable regions

Global

Definition

Expand

API

Description

cipher.rsa(OBJECT)

Encrypts or decrypts data through RSA.

cipher.aes(OBJECT)

Encrypts or decrypts data through AES.

cipher.getRandomValues(int)

Obtains a random number.

cipher.rsa(OBJECT)

Description

Encrypts or decrypts data through RSA.

OBJECT parameters

Expand

Parameter

Type

Mandatory/Optional

Description

action

string

Mandatory

Encryption and decryption type. The options are as follows:

  • encrypt
  • decrypt

text

string

Mandatory

Text to be encrypted or decrypted. The text to be encrypted must be a common text. The text to be decrypted must be a binary value encoded using Base64. The default format is used for Base64 encoding.

key

string

Mandatory

RSA key used for encryption or decryption, which is a character string encrypted using Base64. During encryption, the parameter is a public key. During decryption, it is a private key.

transformation

string

Optional

RSA algorithm padding. The default value is RSA/None/OAEPWithSHA256AndMGF1Padding.

success

function

Optional

Callback upon success.

fail

function

Optional

Callback upon failure.

complete

function

Optional

Callback upon completion.

Parameters returned in success

Expand

Parameter

Type

Description

text

string

Text generated after encryption or decryption.

Result codes returned in fail

Expand

Result Code

Description

202

Incorrect input parameter.

Sample code

  • Encryption
Collapse
Word wrap
Dark theme
Copy code
  1. cipher.rsa({
  2. action: 'encrypt',
  3. // Text to be encrypted.
  4. text: '',
  5. transformation: 'RSA/None/OAEPWithSHA256AndMGF1Padding',
  6. // Encryption public key after Base64 encoding.
  7. key: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDKmi0dUSVQ04hL6GZGPMFK8+d6\n' +
  8. 'GzulagP27qSUBYxIJfE04KT+OHVeFFb6K+8nWDea5mkmZrIgp022zZVDgdWPNM62\n' +
  9. '3ouBwHlsfm2ekey8PpQxfXaj8lhM9t8rJlC4FEc0s8Qp7Q5/uYrowQbT9m6t7BFK\n' +
  10. '3egOO2xOKzLpYSqfbQIDAQAB',
  11. success: function(data){
  12. console.log("handling success: " + data.text);
  13. },
  14. fail: function (erromsg, errocode) {
  15. console. log ('cipher.rsa fail--------' + errocode + ': ' + erromsg);
  16. }
  17. });
  • Decryption
Collapse
Word wrap
Dark theme
Copy code
  1. var that=this;
  2. cipher.rsa({
  3. action: 'decrypt',
  4. // Content to be decrypted, which is a binary value after Base64 encoding and a text after decryption.
  5. text: that.encryptedValue,
  6. transformation: 'RSA/None/OAEPWithSHA256AndMGF1Padding',
  7. // Private key for decryption after Base64 encoding.
  8. key: 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMqaLR1RJVDTiEvo\n' +
  9. 'ZkY8wUrz53obO6VqA/bupJQFjEgl8TTgpP44dV4UVvor7ydYN5rmaSZmsiCnTbbN\n' +
  10. 'lUOB1Y80zrbei4HAeWx+bZ6R7Lw+lDF9dqPyWEz23ysmULgURzSzxCntDn+5iujB\n' +
  11. 'BtP2bq3sEUrd6A47bE4rMulhKp9tAgMBAAECgYBjsfRLPdfn6v9hou1Y2KKg+F5K\n' +
  12. 'ZsY2AnIK+6l+sTAzfIAx7e0ir7OJZObb2eyn5rAOCB1r6RL0IH+MWaN+gZANNG9g\n' +
  13. 'pXvRgcZzFY0oqdMZDuSJjpMTj7OEUlPyoGncBfvjAg0zdt9QGAG1at9Jr3i0Xr4X\n' +
  14. '6WrFhtfVlmQUY1VsoQJBAPK2Qj/ClkZNtrSDfoD0j083LcNICqFIIGkNQ+XeuTwl\n' +
  15. '+Gq4USTyaTOEe68MHluiciQ+QKvRAUd4E1zeZRZ02ikCQQDVscINBPTtTJt1JfAo\n' +
  16. 'wRfTzA0Lvgig136xLLeQXREcgq1lzgkf+tGyUGYoy9BXsV0mOuYAT9ldja4jhJeq\n' +
  17. 'cEulAkEAuSJ5KjV9dyb0RIFAz5C8d8o5KAodwaRIxJkPv5nCZbT45j6t9qbJxDg8\n' +
  18. 'N+vghDlHI4owvl5wwVlAO8iQBy8e8QJBAJe9CVXFV0XJR/n/XnER66FxGzJjVi0f\n' +
  19. '185nOlFARI5CHG5VxxT2PUCo5mHBl8ctIj+rQvalvGs515VQ6YEVDCECQE3S0AU2\n' +
  20. 'BKyFVNtTpPiTyRUWqig4EbSXwjXdr8iBBJDLsMpdWsq7DCwv/ToBoLg+cQ4Crc5/\n' +
  21. '5DChU8P30EjOiEo=',
  22. success: function(data){
  23. console.log("handling success: " + data.text);
  24. },
  25. fail: function (erromsg, errocode) {
  26. console. log ('cipher.rsa fail--------' + errocode + ': ' + erromsg);
  27. }
  28. });

cipher.aes(OBJECT) (1060+)

Description

Encrypts or decrypts data through AES.

OBJECT parameters

Expand

Parameter

Type

Mandatory/Optional

Description

action

string

Mandatory

Encryption and decryption type. The options are as follows:

  • encrypt
  • decrypt

text

string

Optional

Text to be encrypted or decrypted. The text to be encrypted must be a common text. The text to be decrypted must be a binary value encoded using Base64. The default format is used for Base64 encoding.

key

string

Optional

Key used for encryption or decryption, which is a character string encrypted using Base64. This parameter is required for processing the content specified by the text parameter.

transformation

string

Optional

Encryption mode and padding of the AES algorithm. The default value is AES/CBC/PKCS5Padding.

iv

string

Optional

Initial vector for AES-based encryption or decryption. The value is a character string encoded using Base64. The default value is the key value.

ivOffset

int

Optional

Offset of the initial vector for AES-based encryption or decryption. The default value is 0.

ivLen

int

Optional

Length of the initial vector for AES-based encryption or decryption. The default value is 16.

input(1079+)

ArrayBuffer

Optional

Content to be encrypted or decrypted. If text is set, the content set here will not be processed.

secretKey(1079+)

ArrayBuffer

Optional

Key used for encryption or decryption, which is required for processing the content specified by the input parameter.

initialVector(1079+)

ArrayBuffer

Optional

Initial vector for AES encryption and decryption, which is required for processing the content specified by the input parameter. If this parameter is not set, the value of secretKey will be used by default.

success

function

Optional

Callback upon success.

fail

function

Optional

Callback upon failure.

complete

function

Optional

Callback upon completion.

Parameters returned in success

Expand

Parameter

Type

Description

text

string

Text generated after encryption or decryption. The encrypted content is a binary value after Base64 encoding, and the decrypted content is a common text. If the decrypted content cannot be converted into a UTF-8 character string, an error occurs.

output(1079+)

ArrayBuffer

Encryption or decryption result of the value of input.

Result codes returned in fail

Expand

Result Code

Description

202

Incorrect input parameter.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. // Encryption
  2. cipher.aes({
  3. action: 'encrypt',
  4. // Text to be encrypted.
  5. text: 'hello',
  6. // Key after Base64 encoding.
  7. key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',
  8. transformation: 'AES/CBC/PKCS5Padding',
  9. ivOffset: 0,
  10. ivLen: 16,
  11. success: (data) => {
  12. console.log(`handling success: ${data.text}`)
  13. },
  14. fail: (data, code) => {
  15. console.log(`### cipher.aes fail ### ${code}: ${data}`)
  16. }
  17. })
  18. // Decryption
  19. cipher.aes({
  20. action: 'decrypt',
  21. // Content to be decrypted, which is a binary value after Base64 encoding.
  22. text: '1o0kf2HXwLxHkSh5W5NhzA==\n',
  23. // Key after Base64 encoding.
  24. key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',
  25. transformation: 'AES/CBC/PKCS5Padding',
  26. ivOffset: 0,
  27. ivLen: 16,
  28. success: (data) => {
  29. this.dealTxt = data.text
  30. },
  31. fail: (data, code) => {
  32. prompt.showToast({
  33. message: ("Decryption failed, code=" + code + ":" + data)
  34. })
  35. }
  36. })

cipher.getRandomValues(int) (1073+)

Description

Obtains a random number.

OBJECT parameters

Expand

Type

Mandatory/Optional

Return Value

Description

int

Mandatory

string

When the input value is a positive integer ranging from 1 to 32, indicating the number of binary bits of a generated random number, a hexadecimal character string all in lowercase is returned.

For example, if the input value is 3 and the generated 3-digit random number is ['a','b','c'], 616263 is returned.

An empty string is returned in the case of invalid parameters or other exceptions.

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. // Obtain a 16-digit random number and record logs.
  2. let r16 = cipher.getRandomValues(16);
  3. console.log(`random16:${r16}`);

Demo

Collapse
Word wrap
Dark theme
Copy code
  1. <template>
  2. <!-- Only one root node is allowed in template. -->
  3. <div class="container">
  4. <input class="text" placeholder="please enter encrypt content" value="{{inputvale}}" onchange="getvalue" />
  5. <div class="cipherstyle">
  6. <input class="input" type="button" value="rsa encrypt" onclick="rsaencrypt" />
  7. <input class="input" type="button" value="rsa deciphering" onclick="rsadeciphering" />
  8. <input class="input" type="button" value="aes encrypt" onclick="aesencrypt" />
  9. <input class="input" type="button" value="aes deciphering" onclick="aesdeciphering" />
  10. <input class="input" type="button" value="getRandomValues" onclick="getRandomValues" />
  11. </div>
  12. </div>
  13. </template>
  14. <style>
  15. .container {
  16. flex-direction: column;
  17. align-items: center;
  18. justify-content: center;
  19. }
  20. .cipherstyle {
  21. flex-direction: column;
  22. justify-content: center;
  23. align-items: center;
  24. width: 100%;
  25. }
  26. .text {
  27. font-size: 40px;
  28. width: 80%;
  29. height: 80px;
  30. margin-bottom: 10px;
  31. }
  32. .input {
  33. font-size: 50px;
  34. width: 80%;
  35. height: 80px;
  36. border-radius: 40px;
  37. margin-bottom: 10px;
  38. background-color: #00ced1;
  39. }
  40. </style>
  41. <script>
  42. import cipher from '@system.cipher';
  43. import prompt from '@system.prompt';
  44. module.exports = {
  45. data: {
  46. rsaencryptedValue: '',
  47. aesencryptedValue: '',
  48. inputvale: ''
  49. },
  50. onInit() {
  51. this.$page.setTitleBar({
  52. text: 'cipher',
  53. textColor: '#ffffff',
  54. backgroundColor: '#007DFF',
  55. backgroundOpacity: 0.5,
  56. menu: true
  57. });
  58. },
  59. getvalue(e) {
  60. this.inputvale = e.value;
  61. console.log(this.inputvale);
  62. },
  63. rsaencrypt() {
  64. var that = this;
  65. cipher.rsa({
  66. action: 'encrypt',
  67. text: that.inputvale,
  68. transformation: 'RSA/None/OAEPWithSHA256AndMGF1Padding',
  69. key: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDKmi0dUSVQ04hL6GZGPMFK8+d6\n' +
  70. 'GzulagP27qSUBYxIJfE04KT+OHVeFFb6K+8nWDea5mkmZrIgp022zZVDgdWPNM62\n' +
  71. '3ouBwHlsfm2ekey8PpQxfXaj8lhM9t8rJlC4FEc0s8Qp7Q5/uYrowQbT9m6t7BFK\n' +
  72. '3egOO2xOKzLpYSqfbQIDAQAB',
  73. success: function (data) {
  74. console.log("encrypt success: " + data.text);
  75. that.rsaencryptedValue = data.text
  76. prompt.showToast({
  77. message: data.text,
  78. duration: 2000,
  79. gravity: 'center'
  80. })
  81. },
  82. fail: function (erromsg, errocode) {
  83. console.log('encrypt fail--------' + errocode + ': ' + erromsg);
  84. }
  85. });
  86. },
  87. rsadeciphering() {
  88. var that = this;
  89. cipher.rsa({
  90. action: 'decrypt',
  91. text: that.rsaencryptedValue,
  92. transformation: 'RSA/None/OAEPWithSHA256AndMGF1Padding',
  93. key: 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMqaLR1RJVDTiEvo\n' +
  94. 'ZkY8wUrz53obO6VqA/bupJQFjEgl8TTgpP44dV4UVvor7ydYN5rmaSZmsiCnTbbN\n' +
  95. 'lUOB1Y80zrbei4HAeWx+bZ6R7Lw+lDF9dqPyWEz23ysmULgURzSzxCntDn+5iujB\n' +
  96. 'BtP2bq3sEUrd6A47bE4rMulhKp9tAgMBAAECgYBjsfRLPdfn6v9hou1Y2KKg+F5K\n' +
  97. 'ZsY2AnIK+6l+sTAzfIAx7e0ir7OJZObb2eyn5rAOCB1r6RL0IH+MWaN+gZANNG9g\n' +
  98. 'pXvRgcZzFY0oqdMZDuSJjpMTj7OEUlPyoGncBfvjAg0zdt9QGAG1at9Jr3i0Xr4X\n' +
  99. '6WrFhtfVlmQUY1VsoQJBAPK2Qj/ClkZNtrSDfoD0j083LcNICqFIIGkNQ+XeuTwl\n' +
  100. '+Gq4USTyaTOEe68MHluiciQ+QKvRAUd4E1zeZRZ02ikCQQDVscINBPTtTJt1JfAo\n' +
  101. 'wRfTzA0Lvgig136xLLeQXREcgq1lzgkf+tGyUGYoy9BXsV0mOuYAT9ldja4jhJeq\n' +
  102. 'cEulAkEAuSJ5KjV9dyb0RIFAz5C8d8o5KAodwaRIxJkPv5nCZbT45j6t9qbJxDg8\n' +
  103. 'N+vghDlHI4owvl5wwVlAO8iQBy8e8QJBAJe9CVXFV0XJR/n/XnER66FxGzJjVi0f\n' +
  104. '185nOlFARI5CHG5VxxT2PUCo5mHBl8ctIj+rQvalvGs515VQ6YEVDCECQE3S0AU2\n' +
  105. 'BKyFVNtTpPiTyRUWqig4EbSXwjXdr8iBBJDLsMpdWsq7DCwv/ToBoLg+cQ4Crc5/\n' +
  106. '5DChU8P30EjOiEo=',
  107. success: function (data) {
  108. console.log("decrypt success: " + data.text);
  109. prompt.showToast({
  110. message: data.text,
  111. duration: 2000,
  112. gravity: 'center'
  113. })
  114. },
  115. fail: function (erromsg, errocode) {
  116. console.log('decrypt fail--------' + errocode + ': ' + erromsg);
  117. }
  118. });
  119. },
  120. aesencrypt() {
  121. var that = this
  122. cipher.aes({
  123. action: 'encrypt',
  124. text: that.inputvale,
  125. key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',
  126. transformation: 'AES/CBC/PKCS5Padding',
  127. ivOffset: 0,
  128. ivLen: 16,
  129. success: (data) => {
  130. console.log(`encrypt success: ${data.text}`)
  131. that.aesencryptedValue = data.text
  132. prompt.showToast({
  133. message: data.text,
  134. duration: 2000,
  135. gravity: 'center'
  136. })
  137. },
  138. fail: (data, code) => {
  139. console.log(`encrypt fail ### ${code}: ${data}`)
  140. }
  141. })
  142. },
  143. aesdeciphering() {
  144. var that = this
  145. cipher.aes({
  146. action: 'decrypt',
  147. text: that.aesencryptedValue,
  148. key: 'NDM5Qjk2UjAzMEE0NzVCRjlFMkQwQkVGOFc1NkM1QkQ=',
  149. transformation: 'AES/CBC/PKCS5Padding',
  150. ivOffset: 0,
  151. ivLen: 16,
  152. success: (data) => {
  153. this.dealTxt = data.text
  154. console.log("decrypt success: " + data.text);
  155. prompt.showToast({
  156. message: data.text,
  157. duration: 2000,
  158. gravity: 'center'
  159. })
  160. },
  161. fail: (data, code) => {
  162. console.log(`decrypt fail ### ${code}: ${data}`);
  163. }
  164. })
  165. },
  166. getRandomValues() {
  167. let r16 = cipher.getRandomValues(16);
  168. console.log(`random16:${r16}`);
  169. prompt.showToast({
  170. message: `random16:${r16}`,
  171. duration: 2000,
  172. gravity: 'center'
  173. })
  174. }
  175. }
  176. </script>

The following figure shows the final effect.

Version Change History

Expand

Version

Date

Description

1060

2019-12-10

Added the cipher.aes method.

Search in References
Enter a keyword.