Description

Syntax

VARCHAR SM4_DECRYPT(VARCHAR str, VARCHAR key_str[, VARCHAR init_vector][, VARCHAR encryption_mode])

Returns the decrypted result, where:

  • str is the text to be decrypted;
  • key_str is the key. Note that this key is not a hexadecimal encoding, but a string representation of the encoded key. For example, for 128-bit key encryption, key_str should be 16-length. If the key is not long enough, use zero padding to make it up. If it is longer than that, the final key is found using a cyclic xor method. For example, if the 128-bit key used by the algorithm finally is key, then key[i] = key_str[i] ^ key_str[i+128] ^ key_str[i+256] ^ ...
  • init_vector is the initial vector to be used in the algorithm, this is only valid for some algorithms, if not specified then Doris will use the built-in value;
  • encryption_mode is the encryption algorithm, optionally available in variables

SM4_DECRYPT - 图1danger

Until 3.0.2, function with two arguments will ignore session variable block_encryption_mode and always use SM4_128_ECB to do decryption. So it’s not recommended to use it.

Since 3.0.3, it works as expected.

Example

  1. mysql> set block_encryption_mode='';
  2. Query OK, 0 rows affected (0.11 sec)
  3. mysql> select SM4_DECRYPT(FROM_BASE64('aDjwRflBrDjhBZIOFNw3Tg=='),'F3229A0B371ED2D9441B830D21A390C3');
  4. +--------------------------------------------------------------------------------+
  5. | sm4_decrypt(from_base64('aDjwRflBrDjhBZIOFNw3Tg=='), '***', '', 'SM4_128_ECB') |
  6. +--------------------------------------------------------------------------------+
  7. | text |
  8. +--------------------------------------------------------------------------------+
  9. 1 row in set (0.19 sec)
  10. MySQL> set block_encryption_mode="SM4_128_CBC";
  11. Query OK, 0 rows affected (0.006 sec)
  12. mysql> select SM4_DECRYPT(FROM_BASE64('FSYstvOmH2cXy7B/072Mug=='),'F3229A0B371ED2D9441B830D21A390C3'); -- since 3.0.3
  13. +--------------------------------------------------------------------------------+
  14. | sm4_decrypt(from_base64('FSYstvOmH2cXy7B/072Mug=='), '***', '', 'SM4_128_CBC') |
  15. +--------------------------------------------------------------------------------+
  16. | text |
  17. +--------------------------------------------------------------------------------+
  18. 1 row in set (0.11 sec)
  19. MySQL > select SM4_DECRYPT(FROM_BASE64('G7yqOKfEyxdagboz6Qf01A=='),'F3229A0B371ED2D9441B830D21A390C3', '0123456789');
  20. +--------------------------------------------------------------------------------------------------------+
  21. | sm4_decrypt(from_base64('G7yqOKfEyxdagboz6Qf01A=='), 'F3229A0B371ED2D9441B830D21A390C3', '0123456789') |
  22. +--------------------------------------------------------------------------------------------------------+
  23. | text |
  24. +--------------------------------------------------------------------------------------------------------+
  25. 1 row in set (0.012 sec)

Keywords

  1. SM4_DECRYPT, SM4, DECRYPT