OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.3.5

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.3.5
    iconOceanBase Database
    SQL - V 4.3.5
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    SM4_ENCRYPT

    Last Updated:2026-04-15 15:09:26  Updated
    share
    What is on this page
    Syntax
    Purpose
    Examples

    folded

    share

    Notice

    OceanBase Database Community Edition does not support the SM4_ENCRYPT system function.

    Syntax

    SM4_ENCRYPT(str,key_str[,init_vector])
    

    Purpose

    SM4_ENCRYPT() uses the key specified by key_str to encrypt the string specified by str and returns the encrypted binary string. It does not limit the length of str, and automatically pads str to a length that is an integral multiple of the encrypted blocks. The length of the ciphertext is calculated by using the following formula:

    16 × (trunc(string_length/16) + 1)
    

    If any argument is NULL, the function returns NULL.

    The SM4 algorithm is used for data encryption. SM4 is a public block cipher algorithm released by the State Cryptography Administration Office of Security Commercial Code Administration (OSCCA). Both the block and the key are 128 bits long. The longer the key, the higher the security, but the lower the encryption speed.

    The SM4_ENCRYPT() function specifies the block cipher mode by using an initialization vector:

    • The system variable block_encryption_mode specifies the mode for block-based encryption. The default value aes-128-ecb indicates that a 128-bit key and the electronic codebook (ECB) mode are used for encryption.

    • init_vector indicates the initialization vector.

      • When the init_vector argument is required in an encryption mode, it must be 16 bytes or greater in length. Bytes in excess of 16 are ignored. If init_vector is missing, an error occurs.

      • When the init_vector argument is not required in an encryption mode, it is ignored.

    Examples

    1. In sm4-ecb mode, SM4_ENCRYPT() accepts two arguments {str, key_str}. If three values are passed in, the third value is ignored. Here is an example:
    SET block_encryption_mode = 'sm4-ecb';
    
    SELECT hex(sm4_encrypt('asdasdasdasd', '12312313123'))  FROM dual;
    +--------------------------------------------------------------------------------------------------+
    | hex(sm4_encrypt('asdasdasdasd', '12312313123')) |
    +--------------------------------------------------------------------------------------------------+
    | E0640595B963E365A70CB24DC8A3E349                                                                 |
    +--------------------------------------------------------------------------------------------------+
    
    SELECT hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj'))  FROM dual;
    +--------------------------------------------------------------------------------------------------+
    | hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) |
    +--------------------------------------------------------------------------------------------------+
    | E0640595B963E365A70CB24DC8A3E349                                                                 |
    +--------------------------------------------------------------------------------------------------+
    
    1. In sm4-cbc, sm4-cfb, and sm4-ofb modes, SM4_ENCRYPT() accepts only three arguments. Here is an example:
    SELECT hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj'))  FROM dual;
    +--------------------------------------------------------------------------------------------------+
    | hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) |
    +--------------------------------------------------------------------------------------------------+
    | 646B83FDA4E969244E2B9BD835578D50                                                                 |
    +-------------------------------------------------------------------------------------
    

    Previous topic

    SM3
    Last

    Next topic

    SM4_DECRYPT
    Next
    What is on this page
    Syntax
    Purpose
    Examples