SM2 加密解密 公式密匙 导出

本文演示了如何使用hutool-all和bouncycastle库在Java中生成SM2密钥对,将密钥导出为文件,并利用这些密钥进行加密和解密操作。通过具体代码示例,读者可以了解SM2加密算法的工作流程。

借用 hutool-all,bouncycastle实现,公式密匙导出文件,解密再读取文件

pom.xml

 <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.7</version>
        </dependency>
          <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
             <version>1.64</version>
        </dependency>
       

demo

  String text = "我是一段测试aaaa";

        KeyPair pair = SecureUtil.generateKeyPair("SM2");
        byte[] privateKeyArray = pair.getPrivate().getEncoded();
        byte[] publicKeyArray = pair.getPublic().getEncoded();
        writeBytesToFile(privateKeyArray, "d:/privatekey.pem");
        writeBytesToFile(publicKeyArray, "d:/publickey.pem");

        PrivateKey privateKey = SecureUtil.generatePrivateKey("SM2", getBytesFromFile(new File("d:/privatekey.pem")));
        PublicKey publicKey = SecureUtil.generatePublicKey("SM2", getBytesFromFile(new File("d:/publickey.pem")));
        SM2 sm2 = new SM2();
        sm2.setPrivateKey(privateKey);
        sm2.setPublicKey(publicKey);

// 公钥加密,私钥解密
        String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
        System.out.println(encryptStr);
        String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        System.out.println(decryptStr);


    public static void writeBytesToFile(byte[] bs, String path) throws IOException {

        OutputStream out = new FileOutputStream(path);
        InputStream is = new ByteArrayInputStream(bs);
        byte[] buff = new byte[1024];
        int len = 0;
        while ((len = is.read(buff)) != -1) {
            out.write(buff, 0, len);
        }
        is.close();
        out.close();
    }

    // 返回一个byte数组
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);// 获取文件大小
        long lengths = file.length();
        System.out.println("lengths = " + lengths);
        if (lengths > Integer.MAX_VALUE) {
            // 文件太大,无法读取
            throw new IOException("File is to large " + file.getName());
        }
        // 创建一个数据来保存文件数据
        byte[] bytes = new byte[(int) lengths];// 读取数据到byte数组中
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }
        // 确保所有数据均被读取
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file " + file.getName());
        }
        // Close the input stream and return bytes
        is.close();
        return bytes;
    }
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员石磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值