package com.guomi;
import org.apache.commons.lang3.StringUtils;
import com.antherd.smcrypto.sm2.Keypair;
import com.antherd.smcrypto.sm2.Sm2;
public class SM2Encryptor {
/**
* 加密,使用公钥
*
* @param publicKey
* @param originalText
* @return
*/
public static String encryptText(String publicKey, String originalText) throws Exception {
if (StringUtils.isNotEmpty(publicKey)) {
throw new Exception("密钥不能为空...");
}
if (StringUtils.isNotEmpty(originalText)) {
throw new Exception("明文不能为空...");
}
try {
return Sm2.doEncrypt(originalText, publicKey);//HexUtil.encodeHexStr(cipherText); // 加密结果
} catch (Exception e) {
throw new Exception("加密错误:密钥不正确...");
}
}
/**
* 解密,使用私钥
*
* @param privateKey
* @param cipherText
* @return
*/
public static String decryptText(String privateKey, String cipherText) throws Exception {
if (StringUtils.isNotEmpty(privateKey)) {
throw new Exception("密钥不能为空...");
}
if (StringUtils.isNotEmpty(cipherText)) {
throw new Exception("明文不能为空...");
}
try {
return Sm2.doDecrypt(cipherText, privateKey); // new String(sm2.decrypt(sourceData,prvKey)); // 解密结果
} catch (Exception e) {
throw new Exception("解密错误:密钥不正确...");
}
}
/**
* 获取sm2密钥对,
*
* @return 返回String[];第0个为公钥,第1个为私钥
* @throws Exception
*/
public static String[] generateKeyPair() throws Exception {
try {
Keypair keypair = Sm2.generateKeyPairHex();
String[] result = new String[2];
if (keypair != null) {
result[0] = keypair.getPublicKey(); //公钥
result[1] = keypair.getPrivateKey(); // 私钥
}
return result;
} catch (Exception e) {
throw new Exception("生成密钥对失败...");
}
}
public static void main(String[] args) throws Exception {
//生成一对 公钥与私钥
String[] keys = generateKeyPair();
//公钥
String publicKey = keys[0];
System.out.println("公钥" + publicKey);
//私钥
String privateKey = keys[1];
System.out.println("私钥" + privateKey);
String str = "测试使用SM2加密、解密";
//加密字符串
String encryptText = SM2Encryptor.encryptText(publicKey, str);
System.out.println(encryptText);
//解密字符串
String decryptText = SM2Encryptor.decryptText(privateKey, encryptText);
System.out.println(decryptText);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
前端纯js加密、以及后端java解密代码 js 实现国密sm2、sm3、sm4 加密解密demo
共24个文件
js:16个
zip:2个
html:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 112 浏览量
2023-09-19
09:35:48
上传
评论 1
收藏 5.8MB ZIP 举报
温馨提示
前端纯js加密、以及后端java解密代码。国密即国家密码局认定的国产密码算法。常用的主要有SM2,SM3,SM4。 SM2:椭圆曲线公钥密码算法是我国自主设计的公钥密码算法,为非对称加密,基于ECC。该算法已公开。由于该算法基于ECC,故其签名速度与秘钥生成速度都快于RSA。 SM3:消息摘要。可以用MD5作为对比理解。该算法已公开。校验结果为256位。 SM4:对称加密,密钥长度和分组长度均为128位。 由于国际环境(与美国的关系),我们在加密领域也基本切换为国密算法。 最近公司也是要求使用国密加密算法, 折腾了半天,也没有找到合适的资源,所以我这里统一提供了sm2\sm3\sm4 js 前端解解密的demo.需要的小伙伴自行下载
资源推荐
资源详情
资源评论
收起资源包目录
01 验证可行国密demo.zip (24个子文件)
01 验证可行国密demo
demo.zip 2.9MB
demo
sm2
前端
lib
jquery-3.6.0.js 282KB
crypto-js.js 47KB
sm2.js 55KB
sm2.html 2KB
后端
SM2Encryptor.java 3KB
sm-crypto-0.3.2.jar 29KB
sm3
前端
sm3Test.html 3KB
js
crypto
sm3-1.0.js 6KB
sm2-1.0.js 12KB
sm4-1.0.js 9KB
utils
byteUtil.js 2KB
jquery-3.3.1.min.js 85KB
core.js 20KB
hex.js 3KB
ext
rng.js 2KB
prng4.js 1KB
ec-patch.js 5KB
jsbn.js 15KB
jsbn2.js 18KB
ec.js 9KB
后端
bcprov-jdk15on-1.54.jar 3.13MB
Sm3Utils.java 3KB
sm4
国密SM4算法前后端加密解密完整代码.zip 35KB
共 24 条
- 1
资源评论
小风飞子
- 粉丝: 321
- 资源: 1496
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功