Use the my.rsa API for RSA asymmetric encryption and decryption in mPaaS mini programs.
my.rsa
Requires mPaaS 10.1.32 or later.
Performs RSA asymmetric encryption and decryption across the client and server. Store the private key on the server only — storing it on the client creates a security risk.
Parameters
|
Name |
Type |
Required |
Description |
|
action |
String |
Required |
The RSA operation type. Set to |
|
text |
String |
Required |
The text to process. For encryption, provide the plaintext. For decryption, provide the Base64-encoded ciphertext. |
|
key |
String |
Required |
The RSA key. Use the public key for encryption and the private key for decryption. |
|
success |
Function |
No |
Callback invoked on success. |
|
fail |
Function |
No |
Callback invoked on failure. |
|
complete |
Function |
No |
Callback invoked when the call completes, regardless of the result. |
Success response
|
Name |
Type |
Description |
|
text |
String |
The processed text. Encryption returns Base64-encoded ciphertext; decryption returns the original plaintext. |
Error codes
|
Error code |
Description |
Solutions |
|
10 |
Parameter error |
Verify the parameters. |
|
11 |
key error |
Verify the RSA key. |
Examples
-
Client-side encryption and decryption:
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, }); }, }); }, }); -
Server-side encryption and decryption:
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(); } }