参考 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. JVM插码之五:Java agent+ASM实战--监控所有方法执行时间

    本文建立在对instrumentation和agent有初步的了解的前提下阅读,关于这2个类的讲解在其它文章中. 这是一个maven项目,pom中需要的配置,lib中有asm的jar包 pom.xml ...

  2. java-appium-527 WebDriver协议&针对控件的操作

    1.WebDriver协议 https://www.w3.org/TR/webdriver/#list-of-endpoints 1.1查看当前所有的session情况 http://127.0.0. ...

  3. Fix-Dell iDRAC 7 error: RAC0218: The maximum number of user sessions is reached

    Hi Everyone, We came across the following error while performing some preventative maintenance check ...

  4. 【Linux_Unix系统编程】Chapter10 时间

    chapter10 时间 1:真实时间:度量这一时间的起点有二:(1)某个标准点:(2)进程生命周期内的某个固定时点(通常为程序启动) 2:进程时间:一个进程所使用的CPU时间总量,适用于对程序,算法 ...

  5. OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo)

    OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo) 1.问题描述 [oracle@dou_ra ...

  6. python 命名元组(namedtuple)

      我们知道c/c++语言中,有结构体这种数据类型: struct{ string name; int age; char sex; }student; 在对结构体对象进行赋值或者取值时可以使用.运算 ...

  7. Noip往年题目整理

    Noip往年题目整理 张炳琪 一.历年题目 按时间倒序排序 年份 T1知识点 T2知识点 T3知识点 得分 总体 2016day1 模拟 Lca,树上差分 期望dp 144 挺难的一套题目,偏思维难度 ...

  8. Angular5 UI post 请求 输出 文件下载

    this.httpClient.post(url1, JSON.parse(data1) , {responseType: 'blob'}).subscribe(data => { const ...

  9. spring 注解 @NotBlank and BingResult

    @NotEmpty用在集合类上面 @NotBlank 用在String上面 @NotNull 用在基本类型上 在 user对象中需要

  10. C# ADO.NET 封装的增删改查

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...