目录

Cross-Platform Data Encryption and Decryption Compatibility

Overview

Data is encrypted and decrypted during transmission for security purposes. Since the encryption and decryption may be performed on different environments, it is critical to ensure interoperability, that is, the compatibility of cross-platform data. To achieve this purpose, you need to observe the following:

  1. Different data encoding formats: Different platforms may use different data formats, causing incompatibility during cross-platform data transmission and storage.
  2. Different uses of encryption and decryption algorithms: While different platforms may support the same encryption and decryption algorithms, the frameworks implementing these algorithms can differ across platforms.

This topic analyzes encryption and decryption failures caused by different data encoding formats and different uses of encryption and decryption algorithms, and provides solutions to help you efficiently and accurately complete encryption and decryption tasks on the HarmonyOS platform and ensure cross-platform data compatibility.

Encryption and Decryption

The encryption and decryption process is as follows:

  1. Determine the encryption and decryption algorithm and key to use.

  2. Obtain the data to be encrypted and decrypted.

    • Determine the data to be encrypted, such as text, files, or database records.
    • Preprocess the data, for example, format or convert the data to ensure that it meets the format requirements of the encryption algorithm.
  3. Initialize the encryption and decryption engine.

    Initialize the encryption engine based on the selected encryption algorithm. You can set parameters such as the encryption mode (CBC or ECB) and padding mode to meet specific security requirements.

  4. Perform the encryption operation.

    • Input the plaintext to the encryption engine.
    • The encryption engine encrypts data using the specified algorithm and key. This process is usually performed block by block, especially for large files.
  5. Output the result.

    • Obtain the ciphertext.
    • The ciphertext can be stored in a file or database, or transmitted over the network.

Different Data Encoding Formats

Encryption and decryption are usually performed across platforms. For example, the data encrypted on a server may need to be decrypted on a mobile device. The keys and data processed by the encryption and decryption APIs of the HarmonyOS platform must be in Uint8Array format. Therefore, you need to convert data between the Uint8Array format and other encoding formats to ensure data consistency.

The following describes the conversions between three data formats:

Different Uses of Encryption and Decryption Algorithms

Different platforms may support the same algorithms, but the usage of encryption and decryption algorithms can vary across platforms. These differences may lead to issues with cross-platform encryption and decryption.

The encryption and decryption processes on HarmonyOS differ from those on other platforms in the following aspects:

  1. The keys and data processed by the encryption and decryption APIs of the HarmonyOS platform must be in Uint8Array format. You need to convert data between Uint8Array and other formats. For details, see Different Data Encoding Formats.
  2. HarmonyOS supports the SM2 algorithm by default. However, it supports only the data (such as keys and ciphertext) in ASN.1 format. If SM2 is used for cross-platform encryption and decryption, conversion between the raw data and ASN.1 formats is required. This includes converting both the key format and the ciphertext format. For details, see SM2 Encryption and Decryption.
  3. When initializing the encryption/decryption engine on HarmonyOS, fixed parameters must be passed in. For details about the parameter format, see Encryption and Decryption Algorithm Specifications. The following uses AES as an example to analyze the key length and offset parameter settings. For details, see AES Encryption and Decryption.

[h2]SM2 Encryption and Decryption

SM2 is a public key cryptographic algorithm based on elliptic curves.

The following describes how to prevent the key and ciphertext format issues when SM2 is used for cross-platform encryption and decryption.

[h2]AES Encryption and Decryption

AES is the most common symmetric encryption algorithm.

The following describes how to prevent the key length problems and set the offset for the specified block cipher mode when AES is used for cross-platform encryption and decryption.

Sample Code