Encrypt local data for enhanced security

By default, IAPMiniProgram SDK stores data from local-storage JSAPIs (my.getStorage, my.getStorageSync, my.setStorage, and my.setStorageSync) in plaintext. To enhance local data security, the super app can customize the encryption and decryption process using the GRVSecureLocalStorageEncryptorProtocol protocol. This topic guides you through the steps and provides sample codes for local data encryption and decryption.

Procedures

Take the following two steps to customize the encryption and decryption process of local data:

Step 1: Implement GRVSecureLocalStorageEncryptorProtocol

Create an implementation of the GRVSecureLocalStorageEncryptorProtocol protocol. Within this implementation, customize the encryption and decryption logic with your desired algorithm using the encrypt: and decrypt:identifier: methods respectively. Refer to the following sample code for implementation:

copy
@implementation _Your_Impl_GRVSecureLocalStorageEncryptorProtocol

- (NSString *)identifier {
    // Return an identifier that represents the encryptor
}

- (nonnull NSString *)encrypt:(nonnull NSString *)data { 
    // Implement the logic to encrypt data
}

- (NSString * _Nullable)decrypt:(nonnull NSString *)data identifier:(nonnull NSString *)identifier {
    // Implement the logic to decrypt data
}

@end

For more information about this protocol, refer to GRVSecureLocalStorageEncryptorProtocol. When writing the data encryption and decryption logic, you can also use the public instance methods provided by IAPSecurityGuardLite to implement the AES256 algorithm. Refer to the following sample code for the implementation:

copy
import IAPSecurityGuardLite

class _Your_Impl_GRVSecureLocalStorageEncryptorProtocol: NSObject, GRVSecureLocalStorageEncryptorProtocol {

    var identifier: String = "_Your_Encryptor_Identifier_"

        func encrypt(_ data: String) -> String {
        guard let result = try? IAPSecurityGuardLiteService().encrypt(data) else {
            // Handle errors here
            return data
            }

        return result
        }

    func decrypt(_ data: String, identifier: String) -> String? {
        guard let result = try? IAPSecurityGuardLiteService().decrypt(data) else {
            // Handle errors here
            return nil
            }

        return result
        }

}

Step 2: Configure encryptorForLocalStorage

After the SDK initialization logic, configure the encryptorForLocalStorage property and register the implemented GRVSecureLocalStorageEncryptorProtocol protocol to the SDK with the following sample code:

copy
GRVAppContainerLaunch.sharedInstance.extensionDelegate.encryptorForLocalStorage = _Your_Impl_GRVSecureLocalStorageEncryptorProtocol

Protocols

GRVSecureLocalStorageEncryptorProtocol

The GRVSecureLocalStorageEncryptorProtocol protocol is used to customize the encryption and decryption process of local data. The following code shows the definition of this protocol:

copy
@protocol GRVSecureLocalStorageEncryptorProtocol <NSObject>

@required

@property (nonatomic, copy, readonly) NSString *identifier;

- (NSString *)encrypt:(NSString *)data;

- (NSString * _Nullable)decrypt:(NSString *)data identifier: (NSString *)identifier;

@end

As shown by the protocol definition, the GRVSecureLocalStorageEncryptorProtocol protocol provides the following property and methods:

  • Refer to the following table for the property information:

Field

Data type

Description

Required

identifier

NSString *

An identifier that the super app specifies to represent the encryptor. This identifier is passed by the SDK to the decrypt:identifier: method to indicate that the data is encrypted and determine whether to execute the decryption logic.

M

  • Refer to the following table for the method information:

Method

Description

Required

encrypt:

The method that the super app uses to encrypt the given data with a specific algorithm. Set the returned value to the following:

  • The encrypted data if the encryption succeeds.
  • nil if the encryption fails.

For more information, refer to encrypt:.

M

decrypt:identifier:

The method that the super app uses to decrypt the given data with the same algorithm used for encryption. Set the returned value to the following:

  • The decrypted data if the decryption succeeds.
  • nil if the decryption fails.

For more information, refer to decrypt:identifier:.

M

encrypt:

The encrypt: method has the following input parameter:

Field

Data type

Description

Required

data

NSString *

Data to be encrypted.

M

decrypt:identifier:

The decrypt:identifier: method has the following input parameters:

Field

Data type

Description

Required

data

NSString *

Data to be decrypted.

M

identifier

NSString *

The identifier that the super app specifies to represent the encryptor. This identifier must match the value of the identifier property to execute the decryption logic. If the values mismatch, return nil via the decrypt:identifier: method.

M