All Products
Search
Document Center

Key Management Service:Sample code for signing and verification

Last Updated:Sep 09, 2026

These examples show how to call the Sign and Verify APIs using the KMS instance SDK for C#. Each example uses an asymmetric key to sign a message and then verify the signature.

Prerequisites

Before you begin, make sure you have:

  • A KMS instance with an asymmetric key created

  • The KMS instance SDK for C# installed (AlibabaCloud.Dkms.Gcs.Sdk on NuGet)

  • Your Client Key file and password

  • The CA certificate file for your KMS instance

  • Your KMS instance endpoint in the format <instance-id>.cryptoservice.kms.aliyuncs.com

For client initialization details, see Initialize the client.

Complete example

The following example initializes the KMS client, signs a message using an asymmetric key, and then verifies the signature. The verification result is printed to the console.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

using Tea;
using Tea.Utils;


namespace AlibabaCloud.Dkms.Gcs.Sdk.Example
{
    public class AsymmetricSignVerifySample
    {

        public static AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config CreateKmsInstanceConfig(string clientKeyFile, string password, string endpoint, string caFilePath)
        {
            AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config config = new AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config();
            config.ClientKeyFile = clientKeyFile;
            config.Password = password;
            config.Endpoint = endpoint;
            config.CaFilePath = caFilePath;
            return config;
        }

        public static async Task<AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config> CreateKmsInstanceConfigAsync(string clientKeyFile, string password, string endpoint, string caFilePath)
        {
            AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config config = new AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config();
            config.ClientKeyFile = clientKeyFile;
            config.Password = password;
            config.Endpoint = endpoint;
            config.CaFilePath = caFilePath;
            return config;
        }

        public static AlibabaCloud.Dkms.Gcs.Sdk.Client CreateClient(AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config kmsInstanceConfig)
        {
            return new AlibabaCloud.Dkms.Gcs.Sdk.Client(kmsInstanceConfig);
        }

        public static async Task<AlibabaCloud.Dkms.Gcs.Sdk.Client> CreateClientAsync(AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config kmsInstanceConfig)
        {
            return new AlibabaCloud.Dkms.Gcs.Sdk.Client(kmsInstanceConfig);
        }

        public static AlibabaCloud.Dkms.Gcs.Sdk.Models.SignResponse Sign(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string keyId, string algorithm, byte[] message, string messageType)
        {
            AlibabaCloud.Dkms.Gcs.Sdk.Models.SignRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.SignRequest
            {
                KeyId = keyId,
                Algorithm = algorithm,
                Message = message,
                MessageType = messageType,
            };
            //Ignore CA certificate authentication.
            //AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
            //runtime.IgnoreSSL = true;
            //return client.SigntWithOptions(request,runtime);
            return client.Sign(request);
        }

        public static async Task<AlibabaCloud.Dkms.Gcs.Sdk.Models.SignResponse> SignAsync(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string keyId, string algorithm, byte[] message, string messageType)
        {
            AlibabaCloud.Dkms.Gcs.Sdk.Models.SignRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.SignRequest
            {
                KeyId = keyId,
                Algorithm = algorithm,
                Message = message,
                MessageType = messageType,
            };
            //Ignore CA certificate authentication.
            //AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
            //runtime.IgnoreSSL = true;
            //return await client.SignWithOptionsAsync(request,runtime);
            return await client.SignAsync(request);
        }

        public static AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyResponse Verify(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string keyId, string algorithm, byte[] message, string messageType, byte[] signature)
        {
            AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyRequest
            {
                KeyId = keyId,
                Algorithm = algorithm,
                Message = message,
                MessageType = messageType,
                Signature = signature,
            };
            //Ignore CA certificate authentication.
            //AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
            //runtime.IgnoreSSL = true;
            //return client.VerifyWithOptions(request,runtime);
            return client.Verify(request);
        }

        public static async Task<AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyResponse> VerifyAsync(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string keyId, string algorithm, byte[] message, string messageType, byte[] signature)
        {
            AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyRequest
            {
                KeyId = keyId,
                Algorithm = algorithm,
                Message = message,
                MessageType = messageType,
                Signature = signature,
            };
            //Ignore CA certificate authentication.
            //AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
            //runtime.IgnoreSSL = true;
            //return await client.VerifWithOptionsAsync(request,runtime);
            return await client.VerifyAsync(request);
        }

        public static void Main(string[] args)
        {
            string regionId = "<REGION_ID>";
            // CA certificate of the KMS instance.
            string caFilePath = "<CA_FILE_PATH>";
            // Set the endpoint to <your KMS Instance Id>.cryptoservice.kms.aliyuncs.com.
            string endpoint = "<ENDPOINT>";
            // Set the Client Key and Client Key password.
            AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config kmsInstanceConfig = CreateKmsInstanceConfig(AlibabaCloud.DarabonbaEnv.Client.GetEnv("ClientKeyFile"), AlibabaCloud.DarabonbaEnv.Client.GetEnv("Password"), endpoint, caFilePath);
            AlibabaCloud.Dkms.Gcs.Sdk.Client client = CreateClient(kmsInstanceConfig);
            //sign
            string keyId = "<KEY_ID>";
            string algorithm = "<ALGORITHM>";
            byte[] message = AlibabaCloud.DarabonbaEncodeUtil.Encoder.Base64Decode("<MESSAGE_BASE64>");
            string messageType = "<MESSAGE_TYPE>";
            AlibabaCloud.Dkms.Gcs.Sdk.Models.SignResponse signRes = Sign(client, keyId, algorithm, message, messageType);
            //verify
            AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyResponse verifyRes = Verify(client, signRes.KeyId, signRes.Algorithm, message, signRes.MessageType, signRes.Signature);
            string verifyResJson = AlibabaCloud.TeaUtil.Common.ToJSONString(AlibabaCloud.TeaUtil.Common.ToMap(verifyRes));
            AlibabaCloud.TeaConsole.Client.Log("verifyRes:" + verifyResJson);
        }


    }
}

Replace the following placeholders with your actual values:

Placeholder

Description

Example

<REGION_ID>

ID of the region where your KMS instance is deployed

cn-hangzhou

<CA_FILE_PATH>

Local path to the CA certificate file for your KMS instance

/path/to/ca.pem

<ENDPOINT>

KMS instance endpoint

<instance-id>.cryptoservice.kms.aliyuncs.com

<KEY_ID>

ID of the asymmetric key to use for signing

key-abc123

<ALGORITHM>

Signing algorithm supported by the key type

RSA_PKCS1_SHA_256

<MESSAGE_BASE64>

Base64-encoded message to sign

SGVsbG8gV29ybGQ=

<MESSAGE_TYPE>

Whether the message is raw data or a pre-computed digest

RAW or DIGEST

The ClientKeyFile and Password values are read from environment variables to avoid hardcoding credentials in your code.

Example walkthrough

Initialize the client

Initialize the KMS client with your instance credentials. For the full initialization guide, see Initialize the client.

// CA certificate of the KMS instance.
string caFilePath = "<CA_FILE_PATH>";

// Set the endpoint to <your KMS Instance Id>.cryptoservice.kms.aliyuncs.com.
string endpoint = "<ENDPOINT>";

// Set the Client Key and Client Key password.
AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config kmsInstanceConfig = CreateKmsInstanceConfig(AlibabaCloud.DarabonbaEnv.Client.GetEnv("ClientKeyFile"), AlibabaCloud.DarabonbaEnv.Client.GetEnv("Password"), endpoint, caFilePath);
AlibabaCloud.Dkms.Gcs.Sdk.Client client = CreateClient(kmsInstanceConfig);

Sign a message using the Sign API

Build a SignRequest with your key ID, algorithm, message bytes, and message type, then call client.Sign().

public static AlibabaCloud.Dkms.Gcs.Sdk.Models.SignResponse Sign(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string keyId, string algorithm, byte[] message, string messageType)
{
    AlibabaCloud.Dkms.Gcs.Sdk.Models.SignRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.SignRequest
    {
        KeyId = keyId,
        Algorithm = algorithm,
        Message = message,
        MessageType = messageType,
    };
    //Ignore CA certificate authentication.
    //AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
    //runtime.IgnoreSSL = true;
    //return client.SigntWithOptions(request,runtime);
    return client.Sign(request);
}

The response includes KeyId, Algorithm, MessageType, and Signature — pass these directly to the Verify call.

Verify the signature using the Verify API

Pass the signature from the Sign response along with the original message to client.Verify(). Check verifyRes.Value — true means the signature is valid.

public static AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyResponse Verify(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string keyId, string algorithm, byte[] message, string messageType, byte[] signature)
{
    AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.VerifyRequest
    {
        KeyId = keyId,
        Algorithm = algorithm,
        Message = message,
        MessageType = messageType,
        Signature = signature,
    };
    //Ignore CA certificate authentication.
    //AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
    //runtime.IgnoreSSL = true;
    //return client.VerifyWithOptions(request,runtime);
    return client.Verify(request);
}

What's next