参考 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 加解密的更多相关文章

  1. java RSA加解密以及用途

    在公司当前版本的中间件通信框架中,为了防止非授权第三方和到期客户端的连接,我们通过AES和RSA两种方式的加解密策略进行认证.对于非对称RSA加解密,因为其性能耗费较大,一般仅用于认证连接,不会用于每 ...

  2. Rsa加解密Java、C#、php通用代码 密钥转换工具

    之前发了一篇"TripleDes的加解密Java.C#.php通用代码",后面又有项目用到了Rsa加解密,还是在不同系统之间进行交互,Rsa在不同语言的密钥格式不一样,所以过程中主 ...

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

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

  4. 与非java语言使用RSA加解密遇到的问题:algid parse error, not a sequence

    遇到的问题 在一个与Ruby语言对接的项目中,决定使用RSA算法来作为数据传输的加密与签名算法.但是,在使用Ruby生成后给我的私钥时,却发生了异常:IOException: algid parse ...

  5. 全面解决.Net与Java互通时的RSA加解密问题,使用PEM格式的密钥文件

    作者: zyl910 一.缘由 RSA是一种常用的非对称加密算法.所以有时需要在不用编程语言中分别使用RSA的加密.解密.例如用Java做后台服务端,用C#开发桌面的客户端软件时. 由于 .Net.J ...

  6. RSA加解密用途简介及java示例

    在公司当前版本的中间件通信框架中,为了防止非授权第三方和到期客户端的连接,我们通过AES和RSA两种方式的加解密策略进行认证.对于非对称RSA加解密,因为其性能耗费较大,一般仅用于认证连接,不会用于每 ...

  7. Java中的RSA加解密工具类:RSAUtils

    本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.utils.log.LogUtils; ...

  8. 【转】 Java 进行 RSA 加解密时不得不考虑到的那些事儿

    [转] Java 进行 RSA 加解密时不得不考虑到的那些事儿 1. 加密的系统不要具备解密的功能,否则 RSA 可能不太合适 公钥加密,私钥解密.加密的系统和解密的系统分开部署,加密的系统不应该同时 ...

  9. 前后端java+vue 实现rsa 加解密与摘要签名算法

    RSA 加密.解密.签名.验签.摘要,前后端java+vue联调测试通过 直接上代码 // 注意:加密密文与签名都是唯一的,不会变化.// 注意:vue 端密钥都要带pem格式.java 不要带pem ...

随机推荐

  1. CSS Grid布局入门

    相信大家都比较熟悉flex布局了,最近有空研究了波grid布局,感觉虽然兼容性还不是太高,应用不是太普遍,但是功能非常强大.未来应该是grid+flex为主流,grid是二维布局,很灵活,适合整体构架 ...

  2. C++ STL Set 集合

    前言 set是STL中的一种关联容器.集合具有无序性,互异性等特点.熟练使用STL中的set模板类,可以比较简单的解决一些编程问题. 关联容器:元素按照关键字来保存和访问,STL中的map,set就是 ...

  3. 【Spring学习笔记-MVC-11--】Spring MVC之表单标签

    一.使用方法 1.要使用Spring MVC提供的表单标签,首先需要在视图页面添加: <%@ taglib prefix="form" uri="http://ww ...

  4. 自定义动画animate()

    在上一节总结了一下3中类型的动画,其中show()和hide()方法会同时修改元素的多个属性,fadeOut()和fadeIn()方法只会修改元素的不透明度,而slideDown()和slideUp( ...

  5. R语言学习——欧拉计划(11)Largest product in a grid

    Problem 11 In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 0 ...

  6. COMMON INTERVIEW QUESTIONS

    1. What do you see yourself doing five years from now? 2. What motivates you to put forth your great ...

  7. Linux内存管理大图(第三稿)

    http://bbs.chinaunix.net/thread-2018659-2-1.html 描述讨论在http://bbs.chinaunix.net/thread-3760371-1-1.ht ...

  8. python写批量weblogic爆破脚本

    前言: 整理笔记的时候,发现了weblogic的攻击方法.心里打着算盘看看怎么写 个批量的弱口令爆破脚本.得出了以下思路 思路: 1.利用钟馗之眼采集weblogic的网站,将IP写入到txt 2.添 ...

  9. python-log-env

    logging.basicConfig(format="[%(asctime)s] %(filename)s[line:%(lineno)d] %(levelname)s: %(messag ...

  10. 2.Triangle (三角形)

    1.等腰直角三角形: https://www.cnblogs.com/FlyingLiao/p/9869040.html 2.1任意三角形: <!DOCTYPE html> <htm ...