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 bytesis.close();return bytes;}
SM2 加密解密 公式密匙 导出相关推荐
- php 密匙加密解密,带密匙的php加密解密示例分享
这篇文章主要介绍了php加密解密示例,大家参考使用吧 复制代码 代码如下: /************************************************************* ...
- c++国密算法SM2加密解密demo
c++国密算法SM2加密解密 一.代码 一.代码 封装加密.解密接口: 加密接口: Encrpt_SM2() 解密接口:Decrypt_SM2() 加密解密结果可以和nodejs的模块sm-crypt ...
- SM2加密解密执行报 Invalid point encoding 0x5c
SM2加密解密执行报 Invalid point encoding 0x5c 私钥前加00,密文前加04,公钥前加04 确实可以解决问题.内在原因? 解决方法一: SM2加密数据 由C1,C2,C3组 ...
- 国密算法SM2加密解密
一.依赖包 <!-- hutool的 SM2 加密--><dependency><groupId>org.bouncycastle</groupId>& ...
- 页游安全攻与防,SWF加密和隐藏密匙
原文链接:http://netsecurity.51cto.com/art/201211/364775.htm 页游,最最核心的就是客户端(swf)与服务端的游戏通信了.游戏通信产生的封包,内容是否可 ...
- (转)页游安全攻与防,SWF加密和隐藏密匙
原文链接:http://netsecurity.51cto.com/art/201211/364775.htm 页游,最最核心的就是客户端(swf)与服务端的游戏通信了.游戏通信产生的封包,内容是否可 ...
- Java通用C# SM2加密解密
起因是对接一个接口 他们只能提供java deom c#的没有, 百度了一天不能适配, 只能看java 还原的C#. 代码 public class SM2CryptoUtil{public SM2 ...
- SM2加密解密工具类
Maven依赖 <dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jd ...
- 带密匙的php加密解密示例分享
<?phpheader("content-type:text/html;charset=utf-8");$id = "http://www.jb51.net&quo ...
最新文章
- json-lib 常用功能
- linux(4)-Ptrace 系统调用的使用
- iOS开发隐藏键盘方法总结
- C/C++协程实现-学习笔记
- 前端学习(3265):js中undefine中3相关属性
- linux shell脚本 删除指定目录下文件夹(可指定文件夹名、时间)
- java peek方法_Java ArrayDeque peek()方法与示例
- 重量计算python月球_千年前的古诗,苏轼的不知月亮上是何年何月|现在我用Python来计算出来了...
- oracle11g的用户名是什么,oracle11g默认用户名和密码
- AI给植物看病,宾大用TensorFlow做的这款应用造福坦桑尼亚农民
- Windows 7安装到虚拟磁盘VHD文件中
- MySQL数据丢失情况分析
- SpringBoot启动报jdbc连接池错误
- 【5分钟 Paper】Continuous Control With Deep Reinforcement Learning
- 数学建模算法之动态规划
- 什么是SSL安全证书?
- 中小企业如何才能招聘到合适的程序员?
- win10 系统不显示机械硬盘(装在光驱位置)
- 2019年校招:美光半导体笔试题目
- PDF加密文件的解密和打印