Using SM Series Cryptographic Algorithms to Encrypt and Decrypt Hive Table Data
Scenarios
In MRS 3.6.0-LTS or later, Hive supports a group of SM series cryptographic algorithms, which can be used to encrypt and decrypt the string data of tables.
- Encryption: sm4_encrypt('String data or field name to be encrypted', '16-character key', Encryption mode (optional))
- Decryption: sm4_decrypt('String data or field name to be decrypted', 'Key used for encryption', Encryption mode (optional))
Notes and Constraints
- During data encryption and decryption, you can select the encryption mode. If the encryption mode is not specified, SM4/CTR/NOPADDING is used by default.
- The SM4/ECB encryption mode is not supported because it is insecure.
- The String type is recommended because the data in Varchar or Char types may be longer after encryption. If the Varchar or Char type is required, adjust the field length to three times longer than the plaintext value and at least 50 characters. For example, you can run the following SQL statement to change the length of field a in the testtable table from 10 to 20:
alter table testtable change column a a char(20);
Encrypting and Decrypting Data
- Log in to the node where the client is installed as the Hive client installation user.
- Run the following command to go to the client installation directory, for example, /opt/client.
cd /opt/client - Configure environment variables.
source bigdata_env
- Check whether the cluster is in the security mode.
- Log in to the Hive client.
beeline
- Encrypt the string data of the table. For example:
select sm4_encrypt('testdata', 'ABCDEFGH12345678');testdata indicates the string data to be encrypted, and ABCDEFGH12345678 indicates the encryption key, a string of 16 characters. After the command is executed, the encrypted string data is returned. You can use it for decryption in next step.Figure 1 Encrypting string data
- Decrypt the encrypted string data. For example:
SELECT sm4_decrypt('01334750B78AB889A61C5E9F7DEE3EF819AA413EC94A17BC', 'ABCDEFGH12345678');The first parameter is the ciphertext of 6, and ABCDEFGH12345678 is the key used for encryption. After the command is executed, the decrypted string data is returned.Figure 2 Decrypting string data
- Encrypt the strings and specify the encryption mode.
For example, encrypt the following fields in the teststr table:
Figure 3 Encrypting data
- Encrypt the table fields and overwrite the data. For example:
insert overwrite table teststr select sm4_encrypt(name, '0123456789123456', 'SM4/CBC/PKCS5Padding') from teststr;
name indicates the name of the table field to be encrypted, 0123456789123456 indicates the encryption key, a string of 16 characters, and SM4/CBC/PKCS5Padding indicates the encryption algorithm mode.
Figure 4 Encrypting and overwriting data
- Check the encrypted Hive table.
select * from teststr;Figure 5 Checking the encrypted Hive table
- Encrypt the table fields and overwrite the data. For example:
- Decrypt the encrypted string field. For example:
select sm4_decrypt(name, '0123456789123456', 'SM4/CBC/PKCS5Padding') from teststr;
name indicates the name of the table field to be decrypted, 0123456789123456 indicates the key used for encryption, and SM4/CBC/PKCS5Padding indicates the encryption algorithm mode. After the command is executed, the decrypted field name is returned.Figure 6 Decrypting the string field
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot