Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
z_stack
tools
提交
e81957e7
T
tools
项目概览
z_stack
/
tools
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
tools
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
e81957e7
编写于
4月 22, 2024
作者:
z_stack
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init
上级
45c6a1fc
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
0 addition
and
149 deletion
+0
-149
tools-common/tools-common-core/src/main/java/com/study/toolscore/thread/RSAUtil.java
...ore/src/main/java/com/study/toolscore/thread/RSAUtil.java
+0
-149
未找到文件。
tools-common/tools-common-core/src/main/java/com/study/toolscore/thread/RSAUtil.java
已删除
100644 → 0
浏览文件 @
45c6a1fc
//package com.study.toolscore.thread;
//
//
//import java.nio.charset.StandardCharsets;
//import java.security.*;
//import java.security.interfaces.RSAPrivateKey;
//import java.security.interfaces.RSAPublicKey;
//import javax.crypto.BadPaddingException;
//import javax.crypto.Cipher;
//import javax.crypto.IllegalBlockSizeException;
//import javax.crypto.NoSuchPaddingException;
//import java.security.spec.InvalidKeySpecException;
//import java.security.spec.PKCS8EncodedKeySpec;
//import java.security.spec.X509EncodedKeySpec;
//import java.util.Base64;
//import java.util.Map;
//import java.util.HashMap;
//
//public class RSAUtil {
//
//
// //公钥,可以写前端
// public static String public_key="MIGxxxxxx";
//
// //私钥,只能放后端
// public static String private_key="MIICxxxxxxxx";
//
//
// public static void main(String[] args) {
// //解密数据
// try {
// //生成公钥和私钥
// genKeyPair();
// String publicKey = keyMap.get(0);
// //打印出来自己记录下
// System.out.println("公钥:" + publicKey);
// String privateKey = keyMap.get(1);
// //打印出来自己记录下
// System.out.println("私钥:" + privateKey);
//
//
//
// //获取到后,可以放这里,测试下能不能正确加解密
// publicKey = public_key;
// privateKey = private_key;
//
//
//
// String orgData = "test";
// System.out.println("原数据:" + orgData);
//
// //加密
// String encryptStr =encrypt(orgData,publicKey);
// System.out.println("加密结果:" + encryptStr);
//
// //解密
// String decryptStr = decrypt(encryptStr,privateKey);
// System.out.println("解密结果:" + decryptStr);
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
// /**
// * RSA公钥加密
// *
// * @param str 加密字符串
// * @param publicKey 公钥
// * @return 密文
// * @throws Exception 加密过程中的异常信息
// */
// public static String encrypt(String str,String publicKey) throws Exception {
// //base64编码的公钥
// byte[] decoded = decryptBASE64(publicKey);
// Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
// RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
// //RSA加密
// Cipher cipher = Cipher.getInstance("RSA");
// cipher.init(Cipher.ENCRYPT_MODE, pubKey);
// String outStr = encryptBASE64(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
// return outStr;
// }
//
// /**
// * RSA私钥解密
// *
// * @param str 加密字符串
// * @param privateKey 私钥
// * @return 明文
// * @throws Exception 解密过程中的异常信息
// */
// public static String decrypt(String str, String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
// //64位解码加密后的字符串
// byte[] inputByte = decryptBASE64(str);
// //base64编码的私钥
// byte[] decoded = decryptBASE64(privateKey);
// Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
// RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
// //RSA解密
// Cipher cipher = Cipher.getInstance("RSA");
// cipher.init(Cipher.DECRYPT_MODE, priKey);
// return new String(cipher.doFinal(inputByte));
// }
//
// //编码返回字符串
// public static String encryptBASE64(byte[] key) {
// return Base64.getEncoder().encodeToString(key);
// }
//
// //解码返回byte
// public static byte[] decryptBASE64(String key) {
// return Base64.getDecoder().decode(key);
// }
//
// /**
// * 密钥长度 于原文长度对应 以及越长速度越慢
// */
// private static final int KEY_SIZE = 1024;
// /**
// * 用于封装随机产生的公钥与私钥
// */
// private static final Map<Integer, String> keyMap = new HashMap<>();
//
// /**
// * 随机生成密钥对
// * @throws Exception
// */
// public static void genKeyPair() throws Exception {
// // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
// KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// // 初始化密钥对生成器
// keyPairGen.initialize(KEY_SIZE, new SecureRandom());
// // 生成一个密钥对,保存在keyPair中
// KeyPair keyPair = keyPairGen.generateKeyPair();
// // 得到私钥
// RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// // 得到公钥
// RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// String publicKeyString = encryptBASE64(publicKey.getEncoded());
// // 得到私钥字符串
// String privateKeyString = encryptBASE64(privateKey.getEncoded());
// // 将公钥和私钥保存到Map
// //0表示公钥
// keyMap.put(0, publicKeyString);
// //1表示私钥
// keyMap.put(1, privateKeyString);
// }
//}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录