SoFunction
Updated on 2025-04-23

Basic examples of Java implementation of SM3 with SM2 signature and verification

Preface

SM3withSM2 in Java involves using two cryptographic algorithms formulated by the China State Cryptography Administration: SM2 and SM3. SM2 is a public key cryptography algorithm based on elliptic curves, which is usually used for digital signatures, key exchange, etc., while SM3 is a cryptographic hashing algorithm for data integrity verification.

In cryptography, SM3withSM2 usually refers to the process of using the SM2 algorithm for signature and using the SM3 algorithm as a message digest (hash) function. SM2 is a public key cryptography algorithm based on elliptic curve formulated by the State Cryptography Administration of China, which is mainly used for digital signatures, key exchange, etc. SM3 is a password hashing algorithm used for data integrity verification.

When mentioning SM3withSM2, it usually involves the following steps:

  • Message summary: First, the message is hashed using the SM3 algorithm to generate a fixed-length message digest. The purpose of this step is to convert messages of any length into a fixed length hash value for subsequent signature processing.
  • sign: Then, the message digest is signed using the private key of the SM2 algorithm. The signature process generates a signature value that can be used to verify the integrity and authenticity of the message.
  • verify: After receiving the message and signature, the receiver first hash the message using the same SM3 algorithm to generate a message digest. The message digest is then verified using the public key and signature value of the SM2 algorithm. If the verification is successful, it means that the message has not been tampered with during transmission and is indeed sent by the claimed sender.

Implementing SM3withSM2 signatures and verification in Java usually requires the use of encryption libraries that support these algorithms, such as Bouncy Castle. Bouncy Castle is a widely used open source encryption library that provides support for a variety of encryption algorithms, including the Chinese national cryptographic standard.

Please note that since SM2 and SM3 are Chinese national cryptographic standards, the use of these algorithms in certain countries or regions may require compliance with specific laws, regulations and licensing requirements. In addition, it is also very important to ensure that the version of Bouncy Castle is used to support these algorithms.

Implementing SM3 with SM2 in Java may involve the use of the Bouncy Castle library, a widely used open source encryption library that supports a variety of encryption algorithms, including the Chinese national cryptographic standard. Here is a basic example showing how to use the Bouncy Castle library in Java to implement a combination of SM2 signatures and SM3 hashing.

1: Add Bouncy Castle dependency

If you use Maven to manage project dependencies, you can add the following dependencies to the file:

<dependency>
    <groupId></groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>Latest version number</version>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>bcpkix-jdk15on</artifactId>
    <version>Latest version number</version>
</dependency>

2: Generate SM2 key pair

import ;
import ;
import ;
 
import .*;
import ;
 
public class SM2KeyPairGenerator {
    static {
        (new BouncyCastleProvider());
    }
 
    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, OperatorCreationException {
        KeyPairGenerator keyPairGenerator = ("EC", "BC");
        ECGenParameterSpec ecSpec = new ECGenParameterSpec("sm2p256v1");
        (ecSpec, new SecureRandom());
        return ();
    }
 
    public static void main(String[] args) throws Exception {
        KeyPair keyPair = generateKeyPair();
        PublicKey publicKey = ();
        PrivateKey privateKey = ();
 
        byte[] publicKeyBytes = ();
        byte[] privateKeyBytes = ();
 
        ("Public Key: " + (publicKeyBytes));
        ("Private Key: " + (privateKeyBytes));
    }
}

3: Use SM3 to calculate the hash and use SM2 signature

import ;
import ;
import ;
import ;
 
import .*;
import .MGF1ParameterSpec;
import ;
 
public class SM3withSM2Example {
    static {
        (new BouncyCastleProvider());
    }
 
    public static byte[] sm3Hash(String data) throws NoSuchAlgorithmException {
        MessageDigest digest = ("SM3", "BC");
        return (());
    }
 
    public static byte[] signData(String data, PrivateKey privateKey) throws Exception {
        Signature signature = ("SM3withSM2", "BC");
        (privateKey, new SecureRandom());
        (());
        return ();
    }
 
    public static boolean verifySignature(String data, byte[] signatureBytes, PublicKey publicKey) throws Exception {
        Signature signature = ("SM3withSM2", "BC");
        (publicKey);
        (());
        return (signatureBytes);
    }
 
    public static void main(String[] args) throws Exception {
        String data = "Hello, SM2 and SM3!";
        KeyPair keyPair = ();
        PrivateKey privateKey = ();
        PublicKey publicKey = ();
 
        byte[] hash = sm3Hash(data);
        ("SM3 Hash: " + (hash));
 
        byte[] signature = signData(data, privateKey);
        ("Signature: " + (signature));
 
        boolean isVerified = verifySignature(data, signature, publicKey);
        ("Signature Verified: " + isVerified);
    }
}

Things to note

Dependency version: Make sure you are using the Bouncy Castle version that supports SM2 and SM3 algorithms.

Security: In practical applications, key management should be properly handled to avoid key leakage.

Exception handling:Exception handling in the sample code is relatively simple, and the exception handling logic should be improved as needed in actual applications.

Summarize

This is the article about Java implementation of basic examples of SM3 with SM2 signature and verification. For more related Java SM3 with SM2 signature and verification content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!