java rsa 加解密
参考 http://blog.csdn.net/a394268045/article/details/52232120
package rsa; import org.apache.commons.codec.binary.Hex; import javax.crypto.Cipher;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom; /**
* Created by Administrator on 2018/3/12.
*/
public class Main {
public static void main(String[] args) throws Exception {
String source = "hhh测试";
String cryptograph = encrypt(source);
System.out.println("生成的密文-->" + cryptograph); String target = decrypt(cryptograph);
System.out.println("解密密文-->" + target);
} public static void generateKeyPair() throws Exception {
SecureRandom sr = new SecureRandom(); KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(512, sr); KeyPair kp = kpg.generateKeyPair(); Key publicKey = kp.getPublic();
Key privateKey = kp.getPrivate(); ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream("e:/publicKey.keystore"));
ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream("e:/privateKey.keystore"));
oos1.writeObject(publicKey);
oos2.writeObject(privateKey); oos1.close();
oos2.close();
} public static String encrypt(String source) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:/publicKey.keystore"));
Key key = (Key) ois.readObject();
ois.close(); Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] b = source.getBytes("utf-8"); byte[] b1 = cipher.doFinal(b);
// return Base64.encodeBase64String(b1);
return Hex.encodeHexString(b1);
} public static String decrypt(String cryptograph) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:/privateKey.keystore"));
Key key = (Key) ois.readObject(); Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key); // byte[] b1 = Base64.decodeBase64(cryptograph);
//
// byte[] b = cipher.doFinal(b1);
//
// return new String(b, "utf-8"); byte[] b1 = Hex.decodeHex(cryptograph); byte[] b = cipher.doFinal(b1); return new String(b, "utf-8");
}
}
java rsa 加解密的更多相关文章
- java RSA加解密以及用途
在公司当前版本的中间件通信框架中,为了防止非授权第三方和到期客户端的连接,我们通过AES和RSA两种方式的加解密策略进行认证.对于非对称RSA加解密,因为其性能耗费较大,一般仅用于认证连接,不会用于每 ...
- Rsa加解密Java、C#、php通用代码 密钥转换工具
之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...
- RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密
package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...
- 与非java语言使用RSA加解密遇到的问题:algid parse error, not a sequence
遇到的问题 在一个与Ruby语言对接的项目中,决定使用RSA算法来作为数据传输的加密与签名算法.但是,在使用Ruby生成后给我的私钥时,却发生了异常:IOException: algid parse ...
- 全面解决.Net与Java互通时的RSA加解密问题,使用PEM格式的密钥文件
作者: zyl910 一.缘由 RSA是一种常用的非对称加密算法.所以有时需要在不用编程语言中分别使用RSA的加密.解密.例如用Java做后台服务端,用C#开发桌面的客户端软件时. 由于 .Net.J ...
- RSA加解密用途简介及java示例
在公司当前版本的中间件通信框架中,为了防止非授权第三方和到期客户端的连接,我们通过AES和RSA两种方式的加解密策略进行认证.对于非对称RSA加解密,因为其性能耗费较大,一般仅用于认证连接,不会用于每 ...
- Java中的RSA加解密工具类:RSAUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.utils.log.LogUtils; ...
- 【转】 Java 进行 RSA 加解密时不得不考虑到的那些事儿
[转] Java 进行 RSA 加解密时不得不考虑到的那些事儿 1. 加密的系统不要具备解密的功能,否则 RSA 可能不太合适 公钥加密,私钥解密.加密的系统和解密的系统分开部署,加密的系统不应该同时 ...
- 前后端java+vue 实现rsa 加解密与摘要签名算法
RSA 加密.解密.签名.验签.摘要,前后端java+vue联调测试通过 直接上代码 // 注意:加密密文与签名都是唯一的,不会变化.// 注意:vue 端密钥都要带pem格式.java 不要带pem ...
随机推荐
- FPGA配置方式
FPGA有多种配置/加载方式.粗略可以分为主动和被动两种.主动加载是指由FPGA控制配置流程,被动加载是指FPGA仅仅被动接收配置数据. 最常见的被动配置模式就是JTAG下载bit文件.此模式下,主动 ...
- chrome和Firefox对p标签中单词换行的渲染(强制换行)
谷歌和火狐对p标签单词的渲染: 今天在p标签展示url链接中,由于有几个下划线拼接的单词特别长, 所以总有那么几行老是超出p标签的范围,然后设置了强制 换行,才得以解决. word-wrap : br ...
- Parquet列式存储格式
Parquet是面向分析型业务的列式存储格式,由Twitter和Cloudera合作开发,2015年5月从Apache的孵化器里毕业成为Apache顶级项目,最新的版本是1.8.0. 列式存储 列式存 ...
- Linux Shell脚本编程--Linux特殊符号大全
Linux Shell脚本编程--Linux特殊符号大全 linux_shell 特殊符号的介绍 2011
- jquery的相关应用
1.jQuery获取鼠标事件源(万能) 1 //任意位置 2 3 $(document).ready(function(){ 4 5 $(document).click(function(){ 6 $ ...
- Jquery_artDialog对话框弹出
artDialog是一个基于javascript编写的对话框组件,它拥有精致的界面与友好的接口l 自适应内容artDialog的特殊UI框架能够适应内容变化,甚至连外部程序动态插入的内容它仍然能自适 ...
- Ubuntu12.10下Vsftpd的安装
安装Vsftpd sudo apt-get install vsftpd 配置 sudo vim /etc/vsftpd.conf 取消以下两行前面的注释 local_enable=YES write ...
- C++多线程同步之临界区(CriticalSection)
原文链接:http://blog.csdn.net/olansefengye1/article/details/53262917 一.Win32平台 1.相关头文件和接口 #include <w ...
- 常用模块:re ,shelve与xml模块
一 shelve模块: shelve模块比pickle模块简单,只有一个open函数,所以使用完之后要使用f.close关闭文件.返回类似字典的对象,可读可写;key必须为字符串,而值可以是pytho ...
- SpringBoot 返回json 字符串(jackson 及 fast json)
一.jackson 1.Controller 类加注解@RestController 这个注解相当于@Controller 这个注解加 @ResponseBody 2.springBoot 默认使 ...