Java RSA 公钥加密私钥解密
package com.lee.utils; import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Date; import javax.crypto.Cipher; public class RSAUtil { private final static String PUBLIC_KEY_PATH = "c:/publicKeyFile";
private final static String PRIVATE_KEY_PATH = "c:/privateKeyFile"; /**
*生成私钥 公钥
*/
private static void geration(){
KeyPairGenerator keyPairGenerator;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom secureRandom = new SecureRandom(new Date().toString().getBytes());
keyPairGenerator.initialize(1024, secureRandom);
KeyPair keyPair = keyPairGenerator.genKeyPair();
byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
FileOutputStream fos = new FileOutputStream(PUBLIC_KEY_PATH);
fos.write(publicKeyBytes);
fos.close();
byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
fos = new FileOutputStream(PRIVATE_KEY_PATH);
fos.write(privateKeyBytes);
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 获取公钥
* @param filename
* @return
* @throws Exception
*/
public static PublicKey getPublicKey(String filename) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int)f.length()];
dis.readFully(keyBytes);
dis.close();
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
} /**
* 获取私钥
* @param filename
* @return
* @throws Exception
*/
public static PrivateKey getPrivateKey(String filename)throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int)f.length()];
dis.readFully(keyBytes);
dis.close();
PKCS8EncodedKeySpec spec =new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);
} public static void main(String[] args) { geration(); String input = "!!!hello world!!!";
RSAPublicKey pubKey;
RSAPrivateKey privKey;
byte[] cipherText;
Cipher cipher;
try {
cipher = Cipher.getInstance("RSA");
pubKey = (RSAPublicKey) getPublicKey(PUBLIC_KEY_PATH);
privKey = (RSAPrivateKey) getPrivateKey(PRIVATE_KEY_PATH); cipher.init(Cipher.ENCRYPT_MODE, pubKey);
cipherText = cipher.doFinal(input.getBytes());
//加密后的东西
System.out.println("cipher: " + new String(cipherText));
//开始解密
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] plainText = cipher.doFinal(cipherText);
System.out.println("publickey: " + Base64.getEncoder().encode(cipherText));
System.out.println("plain : " + new String(plainText));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
Java RSA 公钥加密私钥解密的更多相关文章
- RSA公钥加密私钥解密
公司的项目需要电科院测评,必须保证数据的完整性和保密性,为这两个特性不得不搞个RSA+SHA1加密. 页面处理过程: 每次登录前,先向后端发送请求,由RSA生成一对公钥和私钥,获取公钥中的模modul ...
- RSA公钥加密-私钥解密/私钥加密-公钥解密
package com.tebon.ams.util;import org.apache.commons.codec.binary.Base64;import org.apache.log4j.Log ...
- RSA 公钥加密——私钥解密
作者:刘巍然-学酥链接:http://www.zhihu.com/question/25912483/answer/31653639来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密
package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...
- 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的算法实现.算法实现是基于网上提供的一个大整 ...
- C#中使用OpenSSL的公钥加密/私钥解密
在C#中进行公钥加密/私钥解密,需要用RSACryptoServiceProvider,但是它不支持由OpenSSL生成的公钥/私钥字符串. 比如这样的公钥/私钥对( 公私钥生成方法见 http:// ...
- Java RSA公钥加密,私钥解密算法的尝试
https://www.cnblogs.com/liemng/p/6699257.html 写这篇博客其实是有点意外的,来源最初也算是入职当前这家公司算吧,由于项目要求数据几乎都进行了加密(政府项目么 ...
随机推荐
- nginx/php的redis模块扩展
redis模块介绍 redis2-nginx-module 可以实现 Nginx 以非阻塞方式直接防问远方的 Redis 服务,可以启用强大的 Redis 连接池功能,进而实现更多的连接与更快速的访问 ...
- 安装Percona版本的MySQL主从复制
准备两台虚拟机,按顺序执行1.1节的公共部分 1.1 首先安装 cmake # yum –y install cmake //也需要安装gcc-c++,openssl openssl-deve ...
- 解决ajax 跨域请求问题
浏览器console报错: has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is 解决: 服务端加入代 ...
- 一键安装metasploit(linux,os x)
curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/templates/metasploit- ...
- windows系统 phpstudy2018 配置阿里云https最简单的流程!
一.从阿里去下载ssl文件放到C:\phpStudy\PHPTutorial\Apache\conf\cert 二.首先phpstudy开户php_openssl扩展,具体如下图 一般网上的教程,都要 ...
- 取消Fragment切换时间,Fragment+ViewPager静止滑动,去掉默认的滑动效果
import android.content.Context; import android.support.v4.view.ViewPager; import android.util.Attrib ...
- c语言二级指针内存模型
第一种: 指针数组作为输入参数 char *myArray[] = {"aaaaaa", "ccccc", "bbbbbb", " ...
- 浅谈 R_S触发器
1.. 1.要点 就是S R端谁是0谁就掌握了绝对权力,因为与门的缘故. 对于上图 S在左边,所以只要S是0 ,Q必定是1.另外对于R 只要R是0,Q非才能是1,q非又是S端的关键与信号,所以只有R为 ...
- Flutter学习(一)之MaterialApp和Scaffold组件使用详解
一,前言: MaterialApp和Scaffold是Flutter提供的两个Widget,其中: MaterialApp是一个方便的Widget,它封装了应用程序实现Material Design所 ...
- promise 的学习
promise 是为了解决异步操作的顺序问题而产生的 特性 promise 的实例一旦创建就会执行里面的异步操作 promise 的实例状态一旦改变就变成凝固的了, 无法再对其作出修改, (不明白为 ...