首页 > 其他分享 >Bouncy Castle SM2加解密

Bouncy Castle SM2加解密

时间:2023-06-03 22:24:47浏览次数:46  
标签:String org 加解密 Bouncy import new Castle ecParameterSpec security

配置过程

代码运行

sm2_demo类://调用SM2加解密功能

package BC;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.*;
import java.security.spec.ECGenParameterSpec;

public class sm2_demo {
    private static String M = "20201217王菁";

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
        SM2Util sm2 = new SM2Util();
        final ECGenParameterSpec sm2Spec = new ECGenParameterSpec("sm2p256v1");
        // 获取一个椭圆曲线类型的密钥对生成器
        final KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", new BouncyCastleProvider());
        // 使用SM2参数初始化生成器
        kpg.initialize(sm2Spec);
        // 获取密钥对
        KeyPair keyPair = kpg.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        String data = sm2.encrypt(publicKey, M);
        System.out.println("明文:"+M);
        System.out.println("密文:"+data);

        String text = sm2.decrypt(privateKey, data);
        System.out.println("解密结果:"+text);
    }
}

SM2Util类:

package BC;

import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.util.encoders.Hex;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;

public class SM2Util {
    /**
     * SM2加密算法
     *
     * @param publicKey 公钥
     * @param data      明文数据
     * @return
     */
    public String encrypt(PublicKey publicKey, String data) {
        ECPublicKeyParameters ecPublicKeyParameters = null;
        if (publicKey instanceof BCECPublicKey) {
            BCECPublicKey bcecPublicKey = (BCECPublicKey) publicKey;
            ECParameterSpec ecParameterSpec = bcecPublicKey.getParameters();
            ECDomainParameters ecDomainParameters = new ECDomainParameters(ecParameterSpec.getCurve(),
                    ecParameterSpec.getG(), ecParameterSpec.getN());
            ecPublicKeyParameters = new ECPublicKeyParameters(bcecPublicKey.getQ(), ecDomainParameters);
        }
        SM2Engine sm2Engine = new SM2Engine();
        sm2Engine.init(true, new ParametersWithRandom(ecPublicKeyParameters, new SecureRandom()));
        byte[] arrayOfBytes = null;
        try {
            byte[] in = data.getBytes("utf-8");
            arrayOfBytes = sm2Engine.processBlock(in, 0, in.length);
        } catch (Exception e) {
            System.out.println("SM2加密时出现异常:");
        }
        return Hex.toHexString(arrayOfBytes);
    }

    /**
     * SM2解密算法
     *
     * @param privateKey 私钥
     * @param cipherData 密文数据
     * @return
     */
    public String decrypt(PrivateKey privateKey, String cipherData) {
        byte[] cipherDataByte = Hex.decode(cipherData);
        BCECPrivateKey bcecPrivateKey = (BCECPrivateKey) privateKey;
        ECParameterSpec ecParameterSpec = bcecPrivateKey.getParameters();
        ECDomainParameters ecDomainParameters = new ECDomainParameters(ecParameterSpec.getCurve(),
                ecParameterSpec.getG(), ecParameterSpec.getN());
        ECPrivateKeyParameters ecPrivateKeyParameters = new ECPrivateKeyParameters(bcecPrivateKey.getD(),
                ecDomainParameters);
        SM2Engine sm2Engine = new SM2Engine();
        sm2Engine.init(false, ecPrivateKeyParameters);
        String result = null;
        try {
            byte[] arrayOfBytes = sm2Engine.processBlock(cipherDataByte, 0, cipherDataByte.length);
            return new String(arrayOfBytes, "utf-8");
        } catch (Exception e) {
            System.out.println("SM2解密时出现异常");
        }
        return result;
    }
}

运行结果

标签:String,org,加解密,Bouncy,import,new,Castle,ecParameterSpec,security
From: https://www.cnblogs.com/ssssspm/p/17454752.html

相关文章

  • BouncyCastle
    BouncyCastle任务详情在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务参考附件内容完成SM2加解密的内容,提交运行结果截图(10‘)完成SM3,SM4算法的调用,提交运行结果截图和代码(15’,选做)1.jar包下载bcprov-ext-jdk15to18-1.73.jar[bcprov-jdk15to18-1.73.jar](htt......
  • BouncyCastle
    BouncyCastle在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务参考附件内容完成SM2加解密的内容,提交运行结果截图完成SM3,SM4算法的调用,提交运行结果截图和代码BouncyCastle配置下载jar包bcprov-ext-jdk15to18-1.73.jarhttps://jar-download.com/?search_bo......
  • BouncyCastle
    任务详情在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务参考附件内容完成SM2加解密的内容,提交运行结果截图(10‘)2完成SM3,SM4算法的调用,提交运行结果截图和代码(15’,选做)jar包下载官网:https://www.bouncycastle.org/latest_releases.htmlbcprov-ext-jdk15to18-1.......
  • java 实现AES加解密后,与在线工具测试结果不一致
    最近有个项目,因为参数里面带有sql可能是客户网关对参数做了防侵入,用简单的base64加密后居然还是不行,决定用AES加密。代码如下。/***参数加密私钥*/staticfinalStringparamPrivateKey="3dae12897b044f96";声明密钥/***加密*@paramsSrc......
  • 编解码、加解密常见特征
    编码编码:Base系列、Unicode、Escape、URL、Hex;https://book.hacktricks.xyz/crypto-and-stego/crypto-ctfs-tricksbase系列Base64是我们最常见的编码,除此之外,其实还有Base16、Base32、Base58、Base85、Base100等,他们之间最明显的区别就是使用了不同数量的可打印字符对任意......
  • BouncyCastle
    任务详情在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务参考附件内容完成SM2加解密的内容,提交运行结果截图(10‘)完成SM3,SM4算法的调用,提交运行结果截图和代码(15’,选做)jar包下载官网:https://www.bouncycastle.org/latest_releases.htmlbcprov-ext-jdk15to18-......
  • 文件加解密
    JAVApackagecom.test;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;/***加密解密类**@author*@version1.00*/publicclassTest{ //staticbooleandebug=false; //加密KEY不能随便改动 staticfina......
  • 超级加解密转换工具——todo,编解码 md5 sha解密可以到https://www.cmd5.com/ 注意有sa
    超级加解密转换工具V2.1绿色免费版       超级加解密转换工具可以说一款万能加密解密转换工具,支持75种方式多种转换,火星最强软件!MD5、16位MD5、MD4、拼音、大小写转换、简繁转换、GBK《-》Big5、GBK简体《-》Big5、GBK《-》SJIS、火星文转换、数字到大写金额、迅雷Thunder......
  • WEB漏洞—SQL注入之加解密,二次,DNS等注入
    1、加解密注入sqli-labs-master(LESS-21)靶机示例数据包抓包,找到cookie数据包Cookie:uname=YWRtaW4%3D%3D是URL编码里的=,所以直接改为等号,在base64里解密这里想要注入直接写and1=1不现实,对admin'and1=1加密后再提交给数据包这里直接报错注入,在这之前需要Base64加密......
  • FIT3155 S1 加解密算法
    FIT3155S1/2023:Assignment3(Duemidnight11:55pmonSunday28May2023)[Weight:10=5+5marks.]Yourassignmentwillbemarkedontheperformance/efficiencyofyourprograms.Youmustwriteallthecodeyourself,andshouldnotuseanyexternallibrary......