关于RSA加密算法的工具类
关于RSA加密算法的工具类
最近在捣鼓SSO(单点登录),就是一个在应用(系统)登录之后,当切换其他应用(系统)的时候,可以省去登录,提高用户的使用的便捷。(具体有时间在写)
期间涉及的安全问题,发送数据涉及账户密码以及分布系统之间的信息安全问题。
- package test.rsa;
- import java.io.*;
- import java.math.BigInteger;
- import java.security.*;
- import java.security.interfaces.*;
- import java.security.spec.*;
- import javax.crypto.*;
- import org.bouncycastle.util.encoders.Hex;
- import sun.misc.*;
- public class TestRSA {
- /**
- * * 生成密钥对 *
- *
- * @return KeyPair *
- * @throws EncryptException
- */
- public static KeyPair generateKeyPair() throws Exception {
- try {
- KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",
- new org.bouncycastle.jce.provider.BouncyCastleProvider());
- final int KEY_SIZE = 1024;// 块加密的大小,是不要太大,否则效率会低
- keyPairGen.initialize(KEY_SIZE, new SecureRandom());
- KeyPair keyPair = keyPairGen.generateKeyPair();
- saveKeyPair(keyPair);
- return keyPair;
- } catch (Exception e) {
- throw new Exception(e.getMessage());
- }
- }
- public static KeyPair getKeyPair() throws Exception {
- FileInputStream fis = new FileInputStream(
- "D:/javasoft/TempTest/RSAKey.txt");
- ObjectInputStream oos = new ObjectInputStream(fis);
- KeyPair kp = (KeyPair) oos.readObject();
- oos.close();
- fis.close();
- return kp;
- }
- public static void saveKeyPair(KeyPair kp) throws Exception {
- FileOutputStream fos = new FileOutputStream(
- "D:/javasoft/TempTest/RSAKey.txt");
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- // 生成密钥
- oos.writeObject(kp);
- oos.close();
- fos.close();
- }
- /**
- * * 生成公钥 *
- *
- * @param modulus *
- * @param publicExponent *
- * @return RSAPublicKey *
- * @throws Exception
- */
- public static RSAPublicKey generateRSAPublicKey(byte[] modulus,
- byte[] publicExponent) throws Exception {
- KeyFactory keyFac = null;
- try {
- keyFac = KeyFactory.getInstance("RSA",
- new org.bouncycastle.jce.provider.BouncyCastleProvider());
- } catch (NoSuchAlgorithmException ex) {
- throw new Exception(ex.getMessage());
- }
- RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(
- modulus), new BigInteger(publicExponent));
- try {
- return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
- } catch (InvalidKeySpecException ex) {
- throw new Exception(ex.getMessage());
- }
- }
- /**
- * * 生成私钥 *
- *
- * @param modulus *
- * @param privateExponent *
- * @return RSAPrivateKey *
- * @throws Exception
- */
- public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus,
- byte[] privateExponent) throws Exception {
- KeyFactory keyFac = null;
- try {
- keyFac = KeyFactory.getInstance("RSA",
- new org.bouncycastle.jce.provider.BouncyCastleProvider());
- } catch (NoSuchAlgorithmException ex) {
- throw new Exception(ex.getMessage());
- }
- RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(
- modulus), new BigInteger(privateExponent));
- try {
- return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);
- } catch (InvalidKeySpecException ex) {
- throw new Exception(ex.getMessage());
- }
- }
- /**
- * 加密的方法
- * @throws IOException
- * @throws NoSuchPaddingException
- * @throws NoSuchAlgorithmException
- */
- private static String encrypt(PublicKey pk,String source) throws Exception{
- Cipher cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
- cipher.init(Cipher.ENCRYPT_MODE, pk);
- byte[] sbt = source.getBytes();
- byte[] epByte = cipher.doFinal(sbt);
- BASE64Encoder encoder = new BASE64Encoder();
- String epStr = encoder.encode(epByte);
- return epStr;
- }
- private static String encrypt1(PublicKey pk,String source) throws Exception{
- Cipher cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
- cipher.init(Cipher.ENCRYPT_MODE, pk);
- byte[] s = source.getBytes();
- byte[] en_s = cipher.doFinal(s);
- return new String(Hex.encode(en_s));
- }
- /**
- * 解密的方法
- * @throws Exception
- */
- public static String decrypt(PrivateKey pk,String source) throws Exception {
- /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
- Cipher cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
- cipher.init(Cipher.DECRYPT_MODE, pk);
- BASE64Decoder decoder = new BASE64Decoder();
- byte[] b1 = decoder.decodeBuffer(source);
- /** 执行解密操作 */
- byte[] b = cipher.doFinal(b1);
- return new String(b);
- }
- public static String decrypt1(PrivateKey pk,String source) throws Exception {
- /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
- Cipher cipher = Cipher.getInstance("RSA",new org.bouncycastle.jce.provider.BouncyCastleProvider());
- cipher.init(Cipher.DECRYPT_MODE, pk);
- /** 执行解密操作 */
- byte[] b = cipher.doFinal(Hex.decode(source));
- return new String(b);
- }
- public static void main(String[] args) throws Exception {
- String s = "1-Test-我";
- String en_s = encrypt1(getKeyPair().getPublic(), s);
- System.out.println("----------------分割线--------------------------");
- System.out.println("加密之后:");
- System.out.println(en_s);
- System.out.println("----------------分割线--------------------------");
- String de_s = decrypt1(getKeyPair().getPrivate(),en_s);
- System.out.println("还原密文:");
- System.out.println(de_s);
- }
- }
关于RSA加密算法的工具类的更多相关文章
- RSA加解密工具类RSAUtils.java,实现公钥加密私钥解密和私钥解密公钥解密
package com.geostar.gfstack.cas.util; import org.apache.commons.codec.binary.Base64; import javax.cr ...
- RSA和AES工具类
AESUtil import com.xxx.common.BssException; import com.xxx.common.constants.CommonConstants; import ...
- AES/DES 可逆性加密算法 -- java工具类
package com.lock.demo.service; import org.apache.tomcat.util.codec.binary.Base64; import javax.crypt ...
- Java中的RSA加解密工具类:RSAUtils
本人手写已测试,大家可以参考使用 package com.mirana.frame.utils.encrypt; import com.mirana.frame.utils.log.LogUtils; ...
- 可用的 .net core 支持 RSA 私钥加密工具类
首先说明 MS并不建议私钥加密,而且.net 于安全的考虑,RSACryptoServiceProvider类解密时只有同时拥有公钥和私钥才可以,原因是公钥是公开的,会被多人持有,这样的数据传输是不安 ...
- Android工具类整合
Android-JSONUtil工具类 常用的Json工具类,包含Json转换成实体.实体转json字符串.list集合转换成json.数组转换成json public class JSONUtil ...
- JAVA的RSA加密算法工具类
须要用到一个jar http://www.bouncycastle.org/latest_releases.html 须要注意的问题 JS用同一秘钥生成的密文用java解密出来是逆序的,即js加密12 ...
- Asp.Net 常用工具类之加密——非对称加密RSA算法
踏入程序员这个行业也有几年了,几年中有收获(技术加强),有付出(时间和亚健康状态).当然喏,并不后悔,代码路还长!!! On The Way,永不止步!!! 开发过程中也积累了一些自己的经验.代码块和 ...
- java 加密工具类(MD5、RSA、AES等加密方式)
1.加密工具类encryption MD5加密 import org.apache.commons.codec.digest.DigestUtils; /** * MD5加密组件 * * @autho ...
随机推荐
- npm出错的解决方案
npm show grpc # 返回版本号 # 安装旧版本: npm install grpc@1.2.0
- idea自动生成serialVersionUID , serialVersionUID的作用
Java的序列化的机制通过判断serialVersionUID来验证版本的一致性.在反序列化的时候与本地的类的serialVersionUID进行比较,一致则可以进行反序列化,不一致则会抛出异常Inv ...
- SQLSERVER系统视图 sql server系统表详细说明
参考 https://www.cnblogs.com/luluping/archive/2012/11/05/2754639.html https://www.cnblogs.com/litubin/ ...
- 如何使用chrome自带的Javascript调试工具 【转】
http://zhangyongbluesky.blog.163.com/blog/static/1831941620113155739840/ 将写好的Javascript代码用chrome打开. ...
- [leetcode]Plus One @ Python
原题地址:https://oj.leetcode.com/problems/plus-one/ 题意: Given a non-negative number represented as an ar ...
- JS中三目运算符和if else的区别,你弄得明白吗
今天写了一个图片轮播的小demo,用到了判断 先试了一下if else,代码如下: if(n >= count-1){ n =0; }else{ n ++; } 随后代码写完了,准备优化 ...
- 快速教你成为C#高手教程
C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言. C#看起来与Java有着惊人的相似:它包括了诸如单一继承.接口.与Java几乎同样的语法 和编译成中间代 ...
- SQLServer 数据库镜像+复制切换方案
目标: 主机做了Mirror和Replication,当主机出现问题时,Replication和Mirror实现自动的故障转移(Mirror 和Replication都切换到备机,而当主机 重新启动后 ...
- Android -- Drag&&Drop
Android3.0提供了drag/drop框架,利用此框架可以实现使用拖放手势将一个view拖放到当前布局中的另外一个view中. 实现拖放的步骤 首先,我们先了解一下拖放过程,从官方文档可以知道, ...
- (转)Unity3D研究院之Assetbundle的原理(六十一)
Assetbundle 是Unity Pro提供提供的功能,它可以把多个游戏对象或者资源二进制文件封装到Assetbundle中,提供了封装与解包的方法使用起来很便利. 1.预设 A ...