my.rsa
This interface is only supported by mPaaS 10.1.32 and later versions.
The interface is for asymmetric encryption. You need to complete encryption and decryption in the client-side and the service-side separately with the private key in the service-side. The operation of putting the private key in the client-side will lead to security problems.
Parameters
Name | Type | Required | Description |
action | String | Required | Use RSA to encrypt or decrypt. |
text | String | Required | If you need to process the text, encrypt it as original text and decrypt it in Base64 encoding format. |
key | String | Required | RSA private key. Use public key for encryption and private key for decryption. |
success | Function | No | The callback function that indicates a successful call. |
fail | Function | No | The callback function that indicates a failed call. |
complete | Function | No | The callback function that indicates the call is completed (this will be executed regardless of whether the call succeeds or fails). |
Return value of success
Name | Type | Description |
text | String | Encrypt the processed text in Base64 encoding format, and decrypt as original text. |
Error code
Error code | Description | Solutions |
10 | Parameter error | Check if the parameter is correct. |
11 | key error | Check if the key is correct. |
Sample code
Encryption and decryption in the client:
Page({ data: { inputValue: '', outputValue: '', }, onInput: function (e) { this.setData({ inputValue: e.detail.value }); }, onEncrypt: function () { my.rsa({ action: 'encrypt', text: this.data.outputValue, //Set public key key: 'MIGfMA0GCSqXXXXXXAQUAA4GNADCBiQKBgQDKmi0dUSVQ04hL6GZGPMFK8+d6\n' + 'GzulagP27qSUBYxIJfE04KT+OHVeFFb6XXXXXXea5mkmZrIgp022zZXXXXXXNM62\n' + '3ouBXXXsfm2ekey8PpQxfXaj8lhM9t8rJlXXXXXXs8Qp7Q5/uYrowQbT9m6t7BFK\n' + '3egOO2xOKzLpYSqfbQIDAQAB', success: (result) => { this.setData({ outputValue: result.text }); }, fail(e) { my.alert({ content: e.errorMessage || e.error, }); }, }); }, onDecrypt: function () { my.rsa({ action: 'decrypt', text: this.data.inputValue, //Set private key key: 'MIICdwIXXXXXXgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMqaLR1RJVDTiEvo\n' + 'ZkY8wUrz53obO6VqA/bupJQFjEgl8TTgpP44dVXXXXXX7ydYN5rmaSZmsiCnTbbN\n' + 'lUOB1Y80zrbeXXXXXXx+bZ6R7Lw+lDF9dqPyWEz23ysmULgURzSzxCntDn+5iujB\n' + 'BtP2bq3sEUrd6A47bE4rMulhKp9tAgMBAAECgYBjsfRLXXXXXX9hou1Y2KKg+F5K\n' + 'ZsY2AnIK+6l+XXXXXXAx7e0ir7OJZObb2eyn5rAOCB1r6RL0IH+XXXXXXZANNG9g\n' + 'pXvRgcZzFY0oqdMZDuSJjpMTj7OXXXXXXGncBfvjAg0zdt9QGAG1at9Jr3i0Xr4X\n' + '6WrFhtfVlmQUY1VsoQJBAPK2Qj/ClkZNtrSDfoXXXXXXLcNICqFIIGkNQ+XeuTwl\n' + '+Gq4USTyaTOEe68MHluiciQ+QKvRAUd4E1zeZRZ02ikCQQDVscINBPTtTJt1JfAo\n' + 'wRfTzA0Lvgig136xLLeQXREcgq1lzgkf+tGyUGYoy9BXsV0mOuYAT9ldja4jhJeq\n' + 'cEulAkEAuSJ5KjV9dyb0XXXXXXC8d8o5KAodwaRIxJkPv5nCZbT45j6t9qbJxDg8\n' + 'N+vghDlHI4owvl5wwVlAO8iQBy8e8QJBAJe9CVXFV0XJR/XXXXXX66FxGzJjVi0f\n' + '185nOXXXXXXCHG5VxxT2PUCo5mHBl8ctIj+rQvalvGs515VQ6YEVDCECQE3S0AU2\n' + 'BKyFVNtTpPiTyRUWqig4EbSXwjXdr8iBBJDLsMpdWsq7DCwv/ToBoLgXXXXXXc5/\n5DChU8P30EjOiEo=', success: (result) => { this.setData({ outputValue: result.text }); }, fail(e) { my.alert({ content: e.errorMessage || e.error, }); }, }); }, });Encryption and decryption in the server:
private static void testJieMi(String miwen, String privateKeyStr) { //Switch the private key after Base64 encoding to PrivateKey object //Use Base64 to decode the encrypted content //Use the private key to decrypt try { PrivateKey privateKey = RSAUtil.string2PrivateKey(privateKeyStr); byte[] base642Byte = RSAUtil.base642Byte(miwen); byte[] privateDecrypt = RSAUtil.privateDecrypt(base642Byte, privateKey); System.out.println ("Decrypted plaintext:" + new String(privateDecrypt)); } catch (Exception e) { e.printStackTrace(); } } private static void testJiaMi(String message, String publicKeyStr) { try { //Switch the public key after Base64 encoding to PublicKey object PublicKey publicKey = RSAUtil.string2PublicKey(publicKeyStr); //Use the public key to encrypt byte[] publicEncrypt = RSAUtil.publicEncrypt(message.getBytes(), publicKey); //Use Base64 to encode the encrypted content String byte2Base64 = RSAUtil.byte2Base64(publicEncrypt); System.out.println(byte2Base64); } catch (Exception e) { e.printStackTrace(); } }