Bootstrap

非堆成加密SM2算法java实现

基于SM2算法的Java示例代码,展示了如何进行公钥加密、私钥解密、私钥签名和公钥验签。

非堆成加密公私钥使用学习请查看:非堆成加密公私钥使用-CSDN博客

RSA算法:非堆成加密RSA算法java实现-CSDN博客

代码示例

展示了以下步骤:

  1. 生成SM2密钥对。

  2. 使用公钥对数据进行加密。

  3. 使用私钥对加密后的数据进行解密。

  4. 使用私钥对数据进行签名。

  5. 使用公钥对签名进行验证。

添加依赖

添加了Bouncy Castle库到你的项目中

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.68</version>
</dependency>
Java代码示例
package org.jobslink.resource;

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.ECGenParameterSpec;

public class SM2Demo {
    static {
        Security.addProvider (new BouncyCastleProvider ());
    }

    public static void main (String[] args) throws Exception {
        // 生成SM2密钥对
        KeyPair keyPair = generateKeyPair ();
        BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic ();
        BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate ();

        String plainText = "你好, SM2!";

        // 公钥加密
        byte[] encryptedData = encrypt (plainText.getBytes (), publicKey);
        System.out.println ("公钥加密 数据: " + Hex.toHexString (encryptedData));

        // 私钥解密
        byte[] decryptedData = decrypt (encryptedData, privateKey);
        System.out.println ("私钥解密 数据: " + new String (decryptedData));

        // 私钥签名
        byte[] signature = sign (plainText.getBytes (), privateKey);
        System.out.println ("私钥签名数据: " + Hex.toHexString (signature));

        // 公钥验签
        boolean isVerified = verify (plainText.getBytes (), signature, publicKey);
        System.out.println ("公钥验签 数据: " + isVerified);
    }

    /**
     * 生成SM2密钥对
     *
     * @return
     * @throws Exception
     */
    public static KeyPair generateKeyPair () throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance ("EC", "BC");
        ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec ("sm2p256v1");
        keyPairGenerator.initialize (ecGenParameterSpec, new SecureRandom ());
        return keyPairGenerator.generateKeyPair ();
    }

    /**
     * 公钥加密
     *
     * @param data
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static byte[] encrypt (byte[] data, BCECPublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance ("SM2", "BC");
        cipher.init (Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal (data);
    }

    /**
     * 私钥解密
     *
     * @param data
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static byte[] decrypt (byte[] data, BCECPrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance ("SM2", "BC");
        cipher.init (Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal (data);
    }

    /**
     * 私钥签名
     *
     * @param data
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static byte[] sign (byte[] data, BCECPrivateKey privateKey) throws Exception {
        Signature signature = Signature.getInstance ("SM3withSM2", "BC");
        signature.initSign (privateKey);
        signature.update (data);
        return signature.sign ();
    }

    /**
     * 公钥验签
     *
     * @param data
     * @param signature
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static boolean verify (byte[] data, byte[] signature, BCECPublicKey publicKey) throws Exception {
        Signature verifier = Signature.getInstance ("SM3withSM2", "BC");
        verifier.initVerify (publicKey);
        verifier.update (data);
        return verifier.verify (signature);
    }
}

运行结果
Encrypted Data: 04eedf62fe91d2ea4ed150a0437e400ac867d07c018b9fc9f8076fff26ae675fa843db85580df6e762614bd005d7a67dd5a51b68aae1cfc4e0a86dc6afc9b3da1ba139200296ca4ef70e8d3d2801d8ceb677daf753c0d5c91b3b12686f63877a9618654bf106e4962c7231de
Decrypted Data: Hello, SM2!
Signature: 304502201bedfb9c52c475f08b9401928e1b55b5d1fdacff981248b94f310bf6f6e3dc38022100ebf96c89481b2257662a17588cbc60dfc1f13ac3b88bc919dde61cf7f43bf2ba
Verification Result: true




;