All Products
Search
Document Center

Mobile Platform as a Service:Data security

Last Updated:Feb 02, 2021

my.rsa

Note: 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. encrypt means encryption and decrypt means decryption.
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 upon successful callback

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:

    1. Page({
    2. data: {
    3. inputValue: '',
    4. outputValue: '',
    5. },
    6. onInput: function (e) {
    7. this.setData({ inputValue: e.detail.value });
    8. },
    9. onEncrypt: function () {
    10. my.rsa({
    11. action: 'encrypt',
    12. text: this.data.outputValue,
    13. //Set public key
    14. key: 'MIGfMA0GCSqXXXXXXAQUAA4GNADCBiQKBgQDKmi0dUSVQ04hL6GZGPMFK8+d6\n' +
    15. 'GzulagP27qSUBYxIJfE04KT+OHVeFFb6XXXXXXea5mkmZrIgp022zZXXXXXXNM62\n' +
    16. '3ouBXXXsfm2ekey8PpQxfXaj8lhM9t8rJlXXXXXXs8Qp7Q5/uYrowQbT9m6t7BFK\n' +
    17. '3egOO2xOKzLpYSqfbQIDAQAB',
    18. success: (result) => {
    19. this.setData({ outputValue: result.text });
    20. },
    21. fail(e) {
    22. my.alert({
    23. content: e.errorMessage || e.error,
    24. });
    25. },
    26. });
    27. },
    28. onDecrypt: function () {
    29. my.rsa({
    30. action: 'decrypt',
    31. text: this.data.inputValue,
    32. //Set private key
    33. key: 'MIICdwIXXXXXXgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMqaLR1RJVDTiEvo\n' +
    34. 'ZkY8wUrz53obO6VqA/bupJQFjEgl8TTgpP44dVXXXXXX7ydYN5rmaSZmsiCnTbbN\n' +
    35. 'lUOB1Y80zrbeXXXXXXx+bZ6R7Lw+lDF9dqPyWEz23ysmULgURzSzxCntDn+5iujB\n' +
    36. 'BtP2bq3sEUrd6A47bE4rMulhKp9tAgMBAAECgYBjsfRLXXXXXX9hou1Y2KKg+F5K\n' +
    37. 'ZsY2AnIK+6l+XXXXXXAx7e0ir7OJZObb2eyn5rAOCB1r6RL0IH+XXXXXXZANNG9g\n' +
    38. 'pXvRgcZzFY0oqdMZDuSJjpMTj7OXXXXXXGncBfvjAg0zdt9QGAG1at9Jr3i0Xr4X\n' +
    39. '6WrFhtfVlmQUY1VsoQJBAPK2Qj/ClkZNtrSDfoXXXXXXLcNICqFIIGkNQ+XeuTwl\n' +
    40. '+Gq4USTyaTOEe68MHluiciQ+QKvRAUd4E1zeZRZ02ikCQQDVscINBPTtTJt1JfAo\n' +
    41. 'wRfTzA0Lvgig136xLLeQXREcgq1lzgkf+tGyUGYoy9BXsV0mOuYAT9ldja4jhJeq\n' +
    42. 'cEulAkEAuSJ5KjV9dyb0XXXXXXC8d8o5KAodwaRIxJkPv5nCZbT45j6t9qbJxDg8\n' +
    43. 'N+vghDlHI4owvl5wwVlAO8iQBy8e8QJBAJe9CVXFV0XJR/XXXXXX66FxGzJjVi0f\n' +
    44. '185nOXXXXXXCHG5VxxT2PUCo5mHBl8ctIj+rQvalvGs515VQ6YEVDCECQE3S0AU2\n' +
    45. 'BKyFVNtTpPiTyRUWqig4EbSXwjXdr8iBBJDLsMpdWsq7DCwv/ToBoLgXXXXXXc5/\n5DChU8P30EjOiEo=',
    46. success: (result) => {
    47. this.setData({ outputValue: result.text });
    48. },
    49. fail(e) {
    50. my.alert({
    51. content: e.errorMessage || e.error,
    52. });
    53. },
    54. });
    55. },
    56. });
  • Encryption and decryption in the server:

    1. private static void testJieMi(String miwen, String privateKeyStr) {
    2. //Switch the private key after Base64 encoding to PrivateKey object
    3. //Use Base64 to decode the encrypted content
    4. //Use the private key to decrypt
    5. try {
    6. PrivateKey privateKey = RSAUtil.string2PrivateKey(privateKeyStr);
    7. byte[] base642Byte = RSAUtil.base642Byte(miwen);
    8. byte[] privateDecrypt = RSAUtil.privateDecrypt(base642Byte, privateKey);
    9. System.out.println ("Decrypted plaintext:" + new String(privateDecrypt));
    10. } catch (Exception e) {
    11. e.printStackTrace();
    12. }
    13. }
    14. private static void testJiaMi(String message, String publicKeyStr) {
    15. try {
    16. //Switch the public key after Base64 encoding to PublicKey object
    17. PublicKey publicKey = RSAUtil.string2PublicKey(publicKeyStr);
    18. //Use the public key to encrypt
    19. byte[] publicEncrypt = RSAUtil.publicEncrypt(message.getBytes(), publicKey);
    20. //Use Base64 to encode the encrypted content
    21. String byte2Base64 = RSAUtil.byte2Base64(publicEncrypt);
    22. System.out.println(byte2Base64);
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. }
    26. }