RSA非对称加密,公钥加密/私钥解密
非对称加密
package test; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Base64;
import java.util.Enumeration;
import javax.crypto.Cipher; /**
* 公钥加密,私钥解密
* @author jinzhm
*
*/
public class RsaUtil {
public final static String CIPHER_ALGORITHM = "RSA/ECB/PKCS1Padding";
public final static String CHARSET_ENCODING = "UTF-8"; /**
* 加密
* @param publicKeyPath
* @param plainText
* @return
*/
private static byte[] encrypt(String publicKeyPath, String plainText) {
if(publicKeyPath==null || plainText==null){
return null;
}
try {
PublicKey key = readPublic(publicKeyPath);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plainText.getBytes(CHARSET_ENCODING));
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 解密
* @param privateKeyPath
* @param privateKeyPwd
* @param encryptedText
* @return
*/
private static String decrypt(String privateKeyPath, String privateKeyPwd, String encryptedText) {
if(privateKeyPath==null || privateKeyPwd==null || encryptedText==null){
return null;
}
try {
PrivateKey key = readPrivate(privateKeyPath, privateKeyPwd);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(Base64.getDecoder().decode(encryptedText)));
} catch (Exception e) {
e.printStackTrace();
}
return "";
} /**
* 读取公钥
* @param publicKeyPath
* @return
*/
private static PublicKey readPublic(String publicKeyPath){
if(publicKeyPath==null){
return null;
}
PublicKey pk = null;
FileInputStream bais = null;
try {
CertificateFactory certificatefactory = CertificateFactory.getInstance("X.509");
bais = new FileInputStream(publicKeyPath);
X509Certificate cert = (X509Certificate)certificatefactory.generateCertificate(bais);
pk = cert.getPublicKey();
} catch (CertificateException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally{
if(bais != null){
try {
bais.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return pk;
} /**
* 读取私钥
* @param path
* @return
*/
private static PrivateKey readPrivate(String privateKeyPath, String privateKeyPwd){
if(privateKeyPath==null || privateKeyPwd==null){
return null;
}
InputStream stream = null;
try {
// 获取JKS 服务器私有证书的私钥,取得标准的JKS的 KeyStore实例
KeyStore store = KeyStore.getInstance("JKS");
stream = new FileInputStream(new File(privateKeyPath));
// jks文件密码,根据实际情况修改
store.load(stream, privateKeyPwd.toCharArray());
// 获取jks证书别名
Enumeration en = store.aliases();
String pName = null;
while (en.hasMoreElements()) {
String n = (String) en.nextElement();
if (store.isKeyEntry(n)) {
pName = n;
}
}
// 获取证书的私钥
PrivateKey key = (PrivateKey) store.getKey(pName,
privateKeyPwd.toCharArray());
return key;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(stream != null){
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
RSA非对称加密,公钥加密/私钥解密的更多相关文章
- RSA 非对称加密,私钥转码为pkcs8 错误总结
RSA 非对称加密,私钥转码为pkcs8 错误总结 最近在和某上市公司对接金融方面的业务时,关于RSA对接过程中遇到了一个坑,特来分享下解决方案. 该上市公司简称为A公司,我们简称为B公司.A-B两家 ...
- RSA 加密算法 Java 公钥加密私钥解密 和 私钥加密公钥解密 的特点
package com.smt.cipher.unsymmetry; import org.apache.commons.codec.binary.Base64; import org.apache. ...
- RSA不对称加密,公钥加密私钥解密,私钥加密公钥解密
RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一 ...
- C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)
但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...
- CryptoAPI与openssl RSA非对称加密解密(PKCS1 PADDING)交互
(以下代码中都只做测试用,有些地方没有释放内存...这个自己解决下) 1.RSA非对称的,首先提供一个供测试用的证书和私钥的数据 1)pem格式的证书和私钥(公私钥是对应的)的base64编码 voi ...
- RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密
package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...
- javascript版前端页面RSA非对称加密解密
最近由于项目需要做一个url传参,并在页面显示参数内容的需求,这样就会遇到一个url地址可能会被假冒, 并传递非法内容显示在页面的尴尬情况 比如xxx.shtml?server=xxx是坏人& ...
- Atitit RSA非对称加密原理与解决方案
Atitit RSA非对称加密原理与解决方案 1.1. 一.一点历史 1 1.2. 八.加密和解密 2 1.3. 二.基于RSA的消息传递机制 3 1.4. 基于rsa的授权验证机器码 4 1.5. ...
- php使用openssl来实现RSA(非对称加密)
使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密. 1.安装openssl和PHP的openssl扩展 2.生成私钥:openssl genrsa 用于生成 ...
- RSA非对称加密Java实现
原文 加密基础方法类 import java.security.MessageDigest; import sun.misc.BASE64Decoder; import sun.misc.BASE64 ...
随机推荐
- laravel整合workerman做聊天室
测试工具 http://www.blue-zero.com/WebSocket/ 2018年8月6日17:28:24 <?php namespace App\Console\Commands; ...
- python hashlib模块 md5加密 sha256加密 sha1加密 sha512加密 sha384加密 MD5加盐
python hashlib模块 hashlib hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, ...
- 关于Java8:StreamAPI的一点记录
关于 Stream ,Functional Interface 的一点记录 stream对于集合操作的便捷度提升: import java.util.ArrayList; import java.ut ...
- [Day19]Collection接口中的子类(List集合、Set集合)
1.List接口 1.1API总结 (1)是一个元素存取有序的集合 (2)是一个带有索引的集合,通过索引可以精确的操作集合中的元素 (3)集合中有可以重复的元素,通过元素的equals方法,来比较是否 ...
- u-boot调试串口输出对应的系统函数
接上Debug串口,启动机器,u-boot哗啦啦地打印一行行的字符.刚接触u-boot的时候,对机器后台做了什么,几乎一无所知. 如果要很有信心地定制出一个简单并且可靠的系统,或者快速完成一项新的任务 ...
- SpringMVC Web项目升级为Springboot项目(一)
一.项目改为Springboot项目 1.将pom中所有spring相关依赖删除,添加spring-boot-starter及spring-boot-starter-web(项目中可能有其他sprin ...
- sed memo 2
配置文件注释过滤 示例文件 [user_00@txyun test]$ cat sed_test # comment aaaaaaaaaaa bbbb #comment cccc dddd fffo ...
- 白话skynet第三篇:通过队列解决多线程竞争资源
今天遇到一个问题,在大厅服务中,如果一个请求使用到了一个公共的变量,如何保证其一致性? 虽然请求是挨个运行的,但是skynet.call会阻塞. "同一个 skynet 服务中的一条消息处理 ...
- Xamarin Forms error MSB6006: “java.exe”已退出,代码为 2 解决办法
https://vicenteguzman.mx/2017/08/20/error-java-exe-exited-with-code-2-xamari-forms/
- IdeaJ 常见插件安装, 常用配置,常用快捷键
-- 系统是 Ubuntu 16.04 1, 插件: 2, 常见的设置: [1] 代码提示的修改: File --> settings --> Keymap --> MainMenu ...