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 公钥加密私钥解密的更多相关文章

  1. RSA公钥加密私钥解密

    公司的项目需要电科院测评,必须保证数据的完整性和保密性,为这两个特性不得不搞个RSA+SHA1加密. 页面处理过程: 每次登录前,先向后端发送请求,由RSA生成一对公钥和私钥,获取公钥中的模modul ...

  2. RSA公钥加密-私钥解密/私钥加密-公钥解密

    package com.tebon.ams.util;import org.apache.commons.codec.binary.Base64;import org.apache.log4j.Log ...

  3. RSA 公钥加密——私钥解密

    作者:刘巍然-学酥链接:http://www.zhihu.com/question/25912483/answer/31653639来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  4. RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密

    package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...

  5. RSA 加密算法 Java 公钥加密私钥解密 和 私钥加密公钥解密 的特点

    package com.smt.cipher.unsymmetry; import org.apache.commons.codec.binary.Base64; import org.apache. ...

  6. RSA不对称加密,公钥加密私钥解密,私钥加密公钥解密

    RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一 ...

  7. C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)

    但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...

  8. C#中使用OpenSSL的公钥加密/私钥解密

    在C#中进行公钥加密/私钥解密,需要用RSACryptoServiceProvider,但是它不支持由OpenSSL生成的公钥/私钥字符串. 比如这样的公钥/私钥对( 公私钥生成方法见 http:// ...

  9. Java RSA公钥加密,私钥解密算法的尝试

    https://www.cnblogs.com/liemng/p/6699257.html 写这篇博客其实是有点意外的,来源最初也算是入职当前这家公司算吧,由于项目要求数据几乎都进行了加密(政府项目么 ...

随机推荐

  1. Java for Android 学习第一周

    前言 专业Java程序员所必需掌握的3个主题: 1. Java编程语言 2. 使用Java的面向对象编程(OOP) 3. Java核心库 JDK.JRE和JVM 1. javac编译java源代码为字 ...

  2. git-format-patch

    使用方法: git diff ${old-commit} ${new-commit} > commit-operation.patch OR git format- b1af44f > c ...

  3. .NET Core开发日志——GraphQL

    GraphQL是什么 GraphQL既是一种用于API的查询语言也是一种通过使用对应数据的类型系统,执行数据查询的服务端运行时.GraphQL没有局限于任何数据库或存储引擎,而是通过既有代码及数据获得 ...

  4. Visual Studio Code for mac 设置中文

    1,mac系统VScode设置中文 macOS 快捷键:command + shift + p 输入搜索 configure language       1.Ctrl+Shift+P 打开命令 2. ...

  5. Excel文件导入导出(基于Nodejs、exceljs)

    Excel导入.导出是大多数项目的管理后台必备功能.几年来使用过多个该功能的实现包,最近一次开发该功能,突然发现一个人气极高(3000+)的包,这里记录一下使用方法. 大凡厉害的技术的文档咋一看都想字 ...

  6. vue 子组件调用父组件的方法

    vue中 父子组件的通信: 子组件通过 props: { //子组件中写的. childMsg: { //字段名 type: Array,//类型 default: [0,0,0] //这样可以指定默 ...

  7. How to use draggable attribute?怎样使用拖拽属性代码分享

    6.7 Drag and dropSupport: dragndropChrome for Android NoneChrome 4+iOS Safari 11.0+UC Browser for An ...

  8. SQL Server脚本

    -- 清楚缓冲区 DBCC DROPCLEANBUFFERS -- 删除计划高速缓存中的元素 DBCC FREEPROCCACHE -- 执行时间 SET STATISTICS TIME ON -- ...

  9. 接口自动化框架(java)--4.接口Token传递

    这套框架的报告是自己封装的 一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值 packa ...

  10. selenium各种定位方法(转)

    selenium使用 Xpath CSS JavaScript jQuery的定位方法 (治疗selenium各种定位不到,点击不了的并发症) 2017年07月28日 22:47:36 阅读数:369 ...