【无标题】

SM2 加解密demo

说明

依赖包为 bcprov-jdk15on-1.58.jar

代码

import org.bouncycastle.jce.ECNamedCurveTable;  
import org.bouncycastle.jce.provider.BouncyCastleProvider;  
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;  
import org.bouncycastle.jce.spec.ECPublicKeySpec;  
import org.bouncycastle.jce.spec.ECPrivateKeySpec;  
import org.bouncycastle.math.ec.ECPoint;  
  
import javax.crypto.Cipher;  
import java.security.*;  
import java.security.spec.InvalidKeySpecException;  
import java.security.spec.PKCS8EncodedKeySpec;  
import java.security.spec.X509EncodedKeySpec;  
import java.util.Base64;  
  
public class SM2Example {  
  
    static {  
        Security.addProvider(new BouncyCastleProvider());  
    }  
  
    public static void main(String[] args) throws Exception {  
        // 生成密钥对
        KeyPair keyPair = generateKeyPair();  
        PublicKey publicKey = keyPair.getPublic();  
        PrivateKey privateKey = keyPair.getPrivate();  
  
        // 解码 Base64 字符串为字节数组 
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKey.getPublic().getEncoded());  
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKey.getPrivate().getEncoded());  
  
        // 将字节数组转换为 Key 对象  
        PublicKey publicKey = generatePublicKey(publicKeyBytes);  
        PrivateKey privateKey = generatePrivateKey(privateKeyBytes);  
  
        // 待加密的信息  
        String originalText = "Hello, SM2!";  
        byte[] data = originalText.getBytes();  
  
        // 加密  
        byte[] encryptedData = encrypt(data, publicKey);  
        System.out.println("Encrypted Data: " + Base64.getEncoder().encodeToString(encryptedData));  
  
        // 解密  
        byte[] decryptedData = decrypt(encryptedData, privateKey);  
        System.out.println("Decrypted Text: " + new String(decryptedData));  
    }  

    private static KeyPair generateKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {  
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");  
        ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("sm2p256v1");  
        keyPairGenerator.initialize(ecSpec);  
        return keyPairGenerator.generateKeyPair();  
    } 
  
    private static PublicKey generatePublicKey(byte[] publicKeyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {  
        X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(publicKeyBytes);  
        KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");  
        return keyFactory.generatePublic(keySpecX509);  
    }  
  
    private static PrivateKey generatePrivateKey(byte[] privateKeyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException {  
        PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(privateKeyBytes);  
        KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");  
        return keyFactory.generatePrivate(keySpecPKCS8);  
    }  
  
    private static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception {  
        Cipher cipher = Cipher.getInstance("ECIES", "BC");  
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
        return cipher.doFinal(data);  
    }  
  
    private static byte[] decrypt(byte[] encryptedData, PrivateKey privateKey) throws Exception {  
        Cipher cipher = Cipher.getInstance("ECIES", "BC");  
        cipher.init(Cipher.DECRYPT_MODE, privateKey);  
        return cipher.doFinal(encryptedData);  
    }  
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值