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.2.2

    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.2.2
    iconOceanBase Database
    SQL - V 4.2.2
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    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

    Authentication based on public-key encryption

    Last Updated:2026-04-15 08:27:14  Updated
    Share
    What is on this page
    Procedure
    Sample code for ciphertext generation in Python
    Sample code for ciphertext generation in Go
    Sample code for ciphertext generation in Java

    folded

    Share

    If you send an HTTP request to OceanBase Shell (OBShell), OBShell authenticates your identify to protect the data transmission from replay attacks.

    Procedure

    1. Step 1: Generate a ciphertext string.

      1. Obtain the public key of the target agent by calling the /api/v1/secret API.
      2. Generate a JSON string in the format of { "password": ${plaintext password}, "ts": ${expiration timestamp} }.
      3. Encrypt the JSON string by using the public key based on the RSA algorithm to generate a byte sequence.
      4. Convert the byte sequence into a Base64-encoded ciphertext string.
    2. Step 2: Send an HTTP request that contains the ciphertext string. Make sure that you specify the X-OCS-Auth field in the header of the request, so that OBShell can authenticate the request.

    X-OCS-Auth: {ciphertext}.

    After receiving the request, OBShell parses the password of the root user of the sys tenant and verifies the password. If the verification succeeds, OBShell continues to process the request. Otherwise, an error is returned. Only the HTTP header can contain authentication strings.

    Sample code for ciphertext generation in Python

    import requests as req
    import json
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher
    import base64
    import time
    
    def encrypt(s, pk):
        key = RSA.import_key(base64.b64decode(pk))
        cipher = PKCS1_cipher.new(key)
        return base64.b64encode(cipher.encrypt(bytes(s.encode('utf8')))).decode('utf8')
    
    
    def auth(pwd, pk):
        auth_json = json.dumps({'password': pwd, 'ts': int(time.time()) + 5})
        return encrypt(auth_json, pk)
    
    resp = req.get('http://xxx.xxx.1:2886/api/v1/secret').text
    resp = json.loads(resp)
    pk = resp['data']['public_key']
    pwd = '1111'
    print(auth(pwd, pk))
    

    Sample code for ciphertext generation in Go

    package main
    
    import (
        "crypto/rand"
        "crypto/rsa"
        "crypto/x509"
        "encoding/base64"
        "encoding/json"
        "fmt"
        "io"
        "net/http"
        "time"
    )
    
    // RSAEncrypt function encrypts a byte array using the provided public key
    func RSAEncrypt(raw []byte, pk string) (string, error) {
        pkix, err := base64.StdEncoding.DecodeString(pk)
        if err != nil {
            return "", err
        }
        pub, err := x509.ParsePKCS1PublicKey(pkix)
        if err != nil {
            return "", err
        }
        b, err := rsa.EncryptPKCS1v15(rand.Reader, pub, raw)
        return base64.StdEncoding.EncodeToString(b), err
    }
    
    // getPublicKey function retrieves the public key from the API
    func getPublicKey(url string) (string, error) {
        resp, err := http.Get(url)
        if err != nil {
            return "", err
        }
        defer resp.Body.Close()
    
        body, err := io.ReadAll(resp.Body)
        if err != nil {
            return "", err
        }
    
        var response struct {
            Data struct {
                PublicKey string `json:"public_key"`
            } `json:"data"`
        }
        if err = json.Unmarshal(body, &response); err != nil {
            return "", err
        }
        return response.Data.PublicKey, nil
    }
    
    // auth function creates an authentication JSON object and encrypts it
    func auth(pwd string, pk string) (string, error) {
        authMap := map[string]interface{}{
            "password": pwd,
            "ts":       time.Now().Unix() + 5,
        }
        authJSON, err := json.Marshal(authMap)
        if err != nil {
            return "", err
        }
        return RSAEncrypt(authJSON, pk)
    }
    
    func genAuth() (err error) {
        publicKey, err := getPublicKey("http://xxx.xxx.1:2886/api/v1/secret")
        if err != nil {
            return
        }
        // Authenticate using the password and public key
        encryptedAuth, err := auth("1111", publicKey)
        if err != nil {
            return
        }
        fmt.Println(encryptedAuth)
        return nil
    }
    
    func main() {
        if err := genAuth(); err != nil {
            fmt.Println("err: ", err)
        }
    }
    
    

    Sample code for ciphertext generation in Java

    package com.oceanbase.vos
    
    import java.math.BigInteger;
    import java.security.KeyFactory;
    import java.security.PublicKey;
    import java.security.spec.RSAPublicKeySpec;
    import java.util.Base64;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import javax.crypto.Cipher;
    import org.bouncycastle.asn1.ASN1InputStream;
    import org.bouncycastle.asn1.ASN1Integer;
    import org.bouncycastle.asn1.ASN1Sequence;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    
    public class Main {
        public static void main(String[] args) {
            try {
                // Obtains the public key.
                String pkcs1PublicKeyStr = getPublicKey("http://xxx.xxx.1:2886/api/v1/secret");
                String password = "1111";
                long timestamp = System.currentTimeMillis() / 1000 + 100000;
                PublicKey publicKey = convertPKCS1ToPublicKey(pkcs1PublicKeyStr);
                String encryptedPassword = encryptPasswordWithRSA(password, timestamp, publicKey);
                System.out.println(encryptedPassword);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static String getPublicKey(String urlStr) throws Exception {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
    
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
                    String inputLine;
                    StringBuilder response = new StringBuilder();
    
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
    
                    JSONObject jsonResponse = JSON.parseObject(response.toString());
                    return jsonResponse.getJSONObject("data").getString("public_key");
                }
            } else {
                throw new Exception("HTTP request failed with code " + responseCode);
            }
        }
    
        private static PublicKey convertPKCS1ToPublicKey(String pkcs1PublicKeyStr) throws Exception {
            pkcs1PublicKeyStr = pkcs1PublicKeyStr.replaceAll("\\n", "")
                    .replace("-----BEGIN RSA PUBLIC KEY-----", "")
                    .replace("-----END RSA PUBLIC KEY-----", "");
            byte[] pkcs1PublicKey = Base64.getDecoder().decode(pkcs1PublicKeyStr);
    
            ASN1InputStream asn1InputStream = new ASN1InputStream(pkcs1PublicKey);
            ASN1Sequence sequence = (ASN1Sequence) asn1InputStream.readObject();
            BigInteger modulus = ((ASN1Integer) sequence.getObjectAt(0)).getValue();
            BigInteger publicExponent = ((ASN1Integer) sequence.getObjectAt(1)).getValue();
            asn1InputStream.close();
    
            RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
    
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            return keyFactory.generatePublic(publicKeySpec);
        }
    
    
        private static String encryptPasswordWithRSA(String password, long timestamp, PublicKey publicKey) throws Exception {
            JSONObject json = new JSONObject();
            json.put("password", password);
            json.put("ts", timestamp);
    
            String jsonString = json.toString();
    
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = cipher.doFinal(jsonString.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }
    }
    

    Previous topic

    OBShell API requests
    Last

    Next topic

    AddNodeBeforeClusterInitialization
    Next
    What is on this page
    Procedure
    Sample code for ciphertext generation in Python
    Sample code for ciphertext generation in Go
    Sample code for ciphertext generation in Java