Method for realizing front-end data encryption storage based on WebAssembly

By compiling the SM4 symmetric encryption algorithm and key into WebAssembly module, the problem of insecurity of front-end data storage is solved, the security of data storage is improved, and the risk of key leakage is reduced.

CN119557895AInactive Publication Date: 2025-03-04HUBEI CENT CHINA TECH DEV OF ELECTRIC POWER +1
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202411487013.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-23
Publication Date
2025-03-04
Estimated Expiration
Not applicable · inactive patent

Smart Images

  • Figure CN119557895A_ABST
    Figure CN119557895A_ABST
Patent Text Reader

Abstract

The invention discloses a method for realizing front-end data encryption storage based on WebAssembly, which comprises the following steps of: 1, realizing a logic code of an SM4 symmetric encryption algorithm by using a Rust development language, and compiling the logic code into a WebAssembly module; step 2, a WebAssembly module is loaded in the JavaScript, a JavaScript adaptation layer code is created, and an encryption and decryption method of the WebAssembly module is packaged into a JavaScript method; step 3, realizing the encryption and decryption of the data through a packaged JavaScript method; and 4, performing data compression processing on the data generated after encryption by using an LZ77 compression algorithm, and storing the data in a Key-Value format to a front-end local storage. Data encryption storage can be provided for the front end, the problem that data stored at the front end is not safe is solved, and data safety is improved through encryption storage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of electronic information technology, and in particular to a method for implementing front-end data encryption and storage based on WebAssembly. Background Art

[0002] With the rapid development of digitalization, data security is not only a core issue for enterprises and organizations, but also directly affects the rights and interests of individuals and the stability of society. Therefore, all parties should attach importance to data security and take effective measures to protect data security. In the front-end field, front-end data encryption storage is also indispensable.

[0003] Currently in the field of front-end development, front-end data usually exists, but the data is stored in plain text, which poses a security risk. Sensitive data may be maliciously intercepted or tampered with. Even if an encryption algorithm is used to encrypt the original data before storing it, it means that the encryption algorithm and key need to be maintained in the project code, which also faces the risk of key leakage. Summary of the invention

[0004] In view of the above shortcomings of the prior art, the present invention provides a front-end data encryption storage method based on WebAssembly, implements a symmetric encryption algorithm through a high-level language, and compiles the encryption algorithm and key into a WebAssembly module. Due to the characteristics of the WebAssembly binary format, it is difficult to directly read and understand its content, which reduces the risk of algorithm and key leakage and improves the security of front-end data storage.

[0005] A method for implementing front-end data encryption and storage based on WebAssembly includes the following steps:

[0006] Step 1: Use the Rust development language to implement the logic code of the SM4 symmetric encryption algorithm and compile it into a WebAssembly module;

[0007] Step 2: Load the WebAssembly module in JavaScript, create a JavaScript adaptation layer code, and encapsulate the encryption and decryption methods of the WebAssembly module into JavaScript methods;

[0008] Step 3: Encrypt and decrypt data through encapsulated JavaScript methods;

[0009] Step 4: Use the LZ77 compression algorithm to compress the data generated after encryption and store it in the front-end local storage in Key-Value format.

[0010] Furthermore, in step one, the logic code of the SM4 symmetric encryption algorithm is implemented by using the Rust development language, wherein the SM4 symmetric encryption algorithm adopts the CBC mode, and the SM4 symmetric encryption algorithm and the corresponding key are compiled into the WebAssembly module.

[0011] Furthermore, when a data encryption action is received, the original data is uniformly converted into a string data format;

[0012] After obtaining the string format data, the string format data is Base64 encoded;

[0013] After obtaining the Base64 encoded data, the Base64 encoded data is encrypted using the SM4 encryption method.

[0014] Furthermore, after the data is encrypted in step 4, the LZ77 compression algorithm is used to compress the data and store it in the front end in Key-Value format. The mathematical model formula of the LZ77 compression algorithm is: C=k+(nk)log2(kn-k+1), C represents the compressed data size, k represents the length of the repeated part in the original data, and n represents the size of the original data. The compression method is: X3=LZ77.compress(X2), where X3 is the data after LZ77 compression.

[0015] Furthermore, obtaining decrypted data after encrypting the data in step 3 specifically includes:

[0016] When receiving a request to access encrypted data, the front end obtains the encrypted data from local storage based on the Key value;

[0017] After obtaining the encrypted data, the LZ77 compression algorithm is used to decompress the encrypted data;

[0018] After the decompressed data is obtained, the decompressed data is decoded using the Base64 encoding algorithm;

[0019] After obtaining the decoded data, the encrypted data is decrypted using the SM4 decryption method.

[0020] The present invention can solve the problem of insecure front-end storage data. It uses a high-level programming language to develop a symmetric encryption algorithm, and compiles the encryption algorithm and the corresponding key into a WebAssembly module, which can prevent the encryption algorithm and the key from being directly exposed, thereby improving the security of the algorithm and the key. Using a symmetric encryption algorithm to encrypt and decrypt data can ensure the security of the data during the storage process, thereby effectively preventing hacker attacks and data leakage. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1 It is a flowchart of a method for implementing front-end data encryption and storage based on WebAssembly provided by an embodiment of the present invention.

[0022] Figure 2 It is a flowchart of data encryption and decryption in a front-end data encryption storage method based on WebAssembly provided by an embodiment of the present invention.

[0023] Figure 3 It is a rendering of data encryption in a front-end data encryption storage method based on WebAssembly provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0024] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0025] like Figure 1 As shown, an embodiment of the present invention provides a method for implementing front-end data encryption storage based on WebAssembly, comprising the following steps:

[0026] Step 1: Use the Rust development language to implement the logic code of the SM4 symmetric encryption algorithm and compile it into a WebAssembly module. Specifically, use the Rust development language to implement the logic code of the SM4 symmetric encryption algorithm, where the SM4 symmetric encryption algorithm adopts the CBC mode, and compile the SM4 symmetric encryption algorithm and the corresponding key into the WebAssembly module. The SM4 symmetric encryption algorithm implemented by Rust is as follows:

[0027] Encryption: pub fn sm4_cbc_encrypt<'a>(plain:&'a str,secret_key:&'a str,secret_iv:&'a str), plain is the original text before encryption, secret_key and secret_iv are encryption and decryption keys;

[0028] Decryption: pub fn sm4_cbc_decrypt<'a>(cipher:&'a str,secret_key:&'a str,secret_iv:&'a str), cipher is the ciphertext before decryption, secret_key and secret_iv are the encryption and decryption keys;

[0029] Step 2: Load the WebAssembly module in JavaScript, create a JavaScript adaptation layer code, and encapsulate the encryption and decryption methods of the WebAssembly module into JavaScript methods. The specific JavaScript adaptation layer code is as follows:

[0030] Encryption: WebAssembly.instance.sm4_cbc_encrypt(plain), where plain is the original text before encryption;

[0031] Decryption: WebAssembly.instance.sm4_cbc_decrypt(cipher), cipher is the ciphertext before decryption;

[0032] Step 3: Encrypt and decrypt data through encapsulated JavaScript methods;

[0033] like Figure 2 As shown, data encryption includes the following steps:

[0034] Step 1. When receiving a data encryption action, convert the original data into a string data format to obtain string data X;

[0035] Step 2. After obtaining the string format data, perform Base64 encoding on the string format data, wherein the encoding method is: X1=Base64.encode(X), wherein X1 is the data after Base64 encoding;

[0036] Step 3. After obtaining the Base64 encoded data, use the SM4 encryption method to encrypt the Base64 encoded data, where the encryption method is: X2 = SM4.encrypt(X1), where X2 is the data after SM4 encryption.

[0037] Obtaining decrypted data after data encryption also includes the following steps:

[0038] Step 1. When receiving an access request for encrypted data, the front end obtains the encrypted data X3 from the local storage according to the Key value;

[0039] Step 2. After obtaining the encrypted data, use the LZ77 compression algorithm to decompress the encrypted data. The specific decompression method is: X2 = LZ77.decompress (X3), where X2 is the data after LZ77 decompression;

[0040] Step 3. After obtaining the decompressed data, use the Base64 encoding algorithm to decode the decompressed data, where the Base64 decoding method is: X1=Base64.decode(X2), where X1 is the data after Base64 decoding.

[0041] Step 4. After obtaining the decoded data, use the SM4 decryption method to decrypt the encrypted data, where the SM4 decryption method is: X=SM4.decrypt(X1), where X is the decrypted data.

[0042] Step 4: Use the LZ77 compression algorithm to compress the encrypted data and store it in the front-end local storage in Key-Value format, such as Figure 3 The effect is shown.

[0043] Specifically, after obtaining the encrypted data, the LZ77 compression algorithm is used to compress the data and store it in the Key-Value format to the front end, where the mathematical model formula of the compression algorithm is: C = k + (nk) log2 (kn-k + 1), C represents the size of the compressed data, k represents the length of the repeated part in the original data, and n represents the size of the original data. The compression method is: X3 = LZ77.compress (X2), where X3 is the data after LZ77 compression. By re-compressing the encrypted data, the risk of data leakage is avoided. Even if the data is intercepted, it is difficult for the attacker to directly decompress or interpret the original data, which further improves the security of the data and increases the difficulty of cracking; data compression reduces the size of the data, so that the data occupies less space when stored, and can also speed up the data transmission speed.

[0044] The above is only a specific embodiment of the present invention, and the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.

Claims

1. A method for implementing front-end data encryption and storage based on WebAssembly, characterized in that The steps include: Step 1: Use the Rust development language to implement the logic code of the SM4 symmetric encryption algorithm and compile it into a WebAssembly module; Step 2: Load the WebAssembly module in JavaScript, create a JavaScript adaptation layer code, and encapsulate the encryption and decryption methods of the WebAssembly module into JavaScript methods; Step 3: Encrypt and decrypt data through encapsulated JavaScript methods; Step 4: Use the LZ77 compression algorithm to compress the data generated after encryption and store it in the front-end local storage in Key-Value format.

2. A method for implementing front-end data encryption and storage based on WebAssembly as claimed in claim 1, characterized in that: In step 1, the logic code of the SM4 symmetric encryption algorithm is implemented through the Rust development language, where the SM4 symmetric encryption algorithm adopts the CBC mode, and the SM4 symmetric encryption algorithm and the corresponding key are compiled into the WebAssembly module.

3. A method for implementing front-end data encryption and storage based on WebAssembly as claimed in claim 2, characterized in that: The specific encryption process in step 3: When receiving the data encryption action, the original data is uniformly converted into a string data format; After obtaining the string format data, the string format data is Base64 encoded; After obtaining the Base64 encoded data, the Base64 encoded data is encrypted using the SM4 encryption method.

4. A method for implementing front-end data encryption and storage based on WebAssembly as claimed in claim 3, characterized in that: After the data is encrypted in step 4, the LZ77 compression algorithm is used to compress the data and store it in the Key-Value format to the front end. The mathematical model formula of the LZ77 compression algorithm is: C = k + (nk) log2 (kn-k + 1), C represents the size of the compressed data, k represents the length of the repeated part in the original data, and n represents the size of the original data. The compression method is: X3 = LZ77.compress (X2), where X3 is the data after LZ77 compression.

5. A method for implementing front-end data encryption and storage based on WebAssembly as claimed in claim 4, characterized in that: After the data is encrypted in step 3, the decrypted data is obtained, which specifically includes: When receiving a request to access encrypted data, the front end obtains the encrypted data from local storage based on the Key value; After obtaining the encrypted data, the LZ77 compression algorithm is used to decompress the encrypted data; After the decompressed data is obtained, the decompressed data is decoded using the Base64 encoding algorithm; After obtaining the decoded data, the encrypted data is decrypted using the SM4 decryption method.

Citation Information

Patent Citations

  • Encryption and decryption module for security transmission of big data

    CN106506518A

  • Web end plaintext data interaction security encryption processing method and system

    CN115422561A

  • Important data transmission confidentiality method based on SM4 in distributed cloud scene

    CN117061091A

  • Web end encryption method based on WASM, medium and equipment

    CN118316595A

  • Safe and efficient interface data transmission encryption and decryption device and method

    CN118353695A