import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;


/**
* 非对称加密工具
*
* @author lKF71480
*/
public class RSAUtils {
/**
* PublicKey4UP:用户中心公钥匙
*/
public static Map<String, Object> keyMap = new HashMap<String, Object>(); static {
try {
Security.addProvider(new BouncyCastleProvider());
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 生成公钥和私钥
*
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
*
*/
public static HashMap<String, Object> getKeys() throws NoSuchAlgorithmException, NoSuchProviderException {
HashMap<String, Object> map = new HashMap<String, Object>();
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); map.put("public", publicKey);
map.put("private", privateKey);
return map;
} /**
* 使用模和指数生成RSA公钥
* 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
* /None/NoPadding】
*
* @param modulus
* 模
* @param exponent
* 指数
* @return
*/
public static RSAPublicKey getPublicKey(String modulus, String exponent) {
try {
BigInteger b1 = new BigInteger(modulus);
BigInteger b2 = new BigInteger(exponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} public static RSAPublicKey getPublicKey(X509EncodedKeySpec publicKey) {
try {
KeyFactory keyfactoryNew = KeyFactory.getInstance("RSA", "BC");
return (RSAPublicKey) keyfactoryNew.generatePublic(publicKey);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 使用模和指数生成RSA私钥
* 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
* /None/NoPadding】
* @param modulus 模
* @param exponent 指数
* @return
*/
public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {
try {
BigInteger b1 = new BigInteger(modulus);
BigInteger b2 = new BigInteger(exponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* 公钥加密
*
* @param data
* @param publicKey
* @return
* @throws Exception
*/
public static String encryptByPublicKey(String data, RSAPublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 模长
int key_len = publicKey.getModulus().bitLength() / 8;
// 加密数据长度 <= 模长-11
String[] datas = splitString(data, key_len - 11);
String mi = "";
// 如果明文长度大于模长-11则要分组加密
for (String s : datas) {
mi += bcd2Str(cipher.doFinal(s.getBytes()));
}
return mi;
} /**
* 私钥解密
*
* @param data
* @param privateKey
* @return
* @throws Exception
*/
public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// 模长
int key_len = privateKey.getModulus().bitLength() / 8;
byte[] bytes = data.getBytes();
byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
System.err.println(bcd.length);
// 如果密文长度大于模长则要分组解密
String ming = "";
byte[][] arrays = splitArray(bcd, key_len);
for (byte[] arr : arrays) {
ming += new String(cipher.doFinal(arr));
}
return ming;
} /**
* 公钥解密
*
* @param data
* @param privateKey
* @return
* @throws Exception
*/
public static String decryptByPublicKey(String data, RSAPublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
// 模长
int key_len = publicKey.getModulus().bitLength() / 8;
byte[] bytes = data.getBytes();
byte[] bcd = ASCII_To_BCD(bytes, bytes.length);
System.err.println(bcd.length);
// 如果密文长度大于模长则要分组解密
String ming = "";
byte[][] arrays = splitArray(bcd, key_len);
for (byte[] arr : arrays) {
ming += new String(cipher.doFinal(arr));
}
return ming;
}
/**
* <p>
* 解密
* </P>
* <功能详细描述>
*
* @return 解密后的明文
* @see [类、类#方法、类#成员]
*/
public static String decrypt(String cryptStr) {
return null;
} /**
* ASCII码转BCD码
*
*/
public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {
byte[] bcd = new byte[asc_len / 2];
int j = 0;
for (int i = 0; i < (asc_len + 1) / 2; i++) {
bcd[i] = asc_to_bcd(ascii[j++]);
bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4));
}
return bcd;
} public static byte asc_to_bcd(byte asc) {
byte bcd; if ((asc >= '0') && (asc <= '9'))
bcd = (byte) (asc - '0');
else if ((asc >= 'A') && (asc <= 'F'))
bcd = (byte) (asc - 'A' + 10);
else if ((asc >= 'a') && (asc <= 'f'))
bcd = (byte) (asc - 'a' + 10);
else
bcd = (byte) (asc - 48);
return bcd;
} /**
* BCD转字符串
*/
public static String bcd2Str(byte[] bytes) {
char temp[] = new char[bytes.length * 2], val; for (int i = 0; i < bytes.length; i++) {
val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); val = (char) (bytes[i] & 0x0f);
temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
}
return new String(temp);
} /**
* 拆分字符串
*/
public static String[] splitString(String string, int len) {
int x = string.length() / len;
int y = string.length() % len;
int z = 0;
if (y != 0) {
z = 1;
}
String[] strings = new String[x + z];
String str = "";
for (int i = 0; i < x + z; i++) {
if (i == x + z - 1 && y != 0) {
str = string.substring(i * len, i * len + y);
} else {
str = string.substring(i * len, i * len + len);
}
strings[i] = str;
}
return strings;
} /**
* 拆分数组
*/
public static byte[][] splitArray(byte[] data, int len) {
int x = data.length / len;
int y = data.length % len;
int z = 0;
if (y != 0) {
z = 1;
}
byte[][] arrays = new byte[x + z][];
byte[] arr;
for (int i = 0; i < x + z; i++) {
arr = new byte[len];
if (i == x + z - 1 && y != 0) {
System.arraycopy(data, i * len, arr, 0, y);
} else {
System.arraycopy(data, i * len, arr, 0, len);
}
arrays[i] = arr;
}
return arrays;
} public static String getUpPrivateEncodeStr (String encodeStr) {
String result = "";
try {
RSAPrivateKey priKey = null;
if (keyMap.get("PrivateKey4UP") == null) {
String rootPath = StringUtils.remove(Thread.currentThread().getContextClassLoader().getResource("").getPath(), "classes/");
String localPath = rootPath + File.separator + "keystore";
File privateKeyFile = new File(localPath + File.separator + "private.key");
FileInputStream privateKeyInputStream = new FileInputStream(privateKeyFile);
byte[] privateKeyByte = new byte[(int) privateKeyFile.length()];
// 将私钥文件读入内存
privateKeyInputStream.read(privateKeyByte);
privateKeyInputStream.close();
// 生成私钥钥对象privateKey
PKCS8EncodedKeySpec priPKCS8new = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyByte));
KeyFactory keyfactoryNew = KeyFactory.getInstance("RSA", "BC");
priKey = (RSAPrivateKey) keyfactoryNew.generatePrivate(priPKCS8new);
keyMap.put("PrivateKey4UP", priKey);
} else {
priKey = (RSAPrivateKey) keyMap.get("PrivateKey4UP");
}
// 私钥解密
result = RSAUtils.decryptByPrivateKey(encodeStr, priKey);
} catch (Exception e) {
e.printStackTrace();
}
return result;
} public static String getUpPublicEncodeStr(String str) {
// 公钥加密
String result = "";
try {
RSAPublicKey pubKey = null;
if (keyMap.get("PublicKey4UP") == null){
// 公钥文件
String rootPath = StringUtils.remove(Thread.currentThread().getContextClassLoader().getResource("").getPath(), "classes/");
String localPath = rootPath + File.separator + "keystore";
File publicKeyFile = new File(localPath + File.separator + "publicUp.key");
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
FileInputStream publicKeyInputStream = new FileInputStream(publicKeyFile);
byte[] publicKeyByte = new byte[(int) publicKeyFile.length()];
// 将公钥文件读入内存
publicKeyInputStream.read(publicKeyByte);
publicKeyInputStream.close();
// 生成公钥对象PublicKey
X509EncodedKeySpec pubX509new = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyByte));
KeyFactory keyfactoryNew = KeyFactory.getInstance("RSA", "BC");
pubKey = (RSAPublicKey) keyfactoryNew.generatePublic(pubX509new);
keyMap.put("PublicKey4UP", pubKey);
} else {
pubKey = (RSAPublicKey)keyMap.get("PublicKey4UP");
}
result = RSAUtils.encryptByPublicKey(str, pubKey);
} catch (Exception e) {
e.printStackTrace();
DEBUGGER.debug(e.getMessage());
}
return result;
} /**
* 获取秘钥对文件
* @param args
* @throws Exception
*/
public static void makeThePairFile(String[] args) throws Exception { /*HashMap<String, Object> map = RSAUtils.getKeys();
// 生成公钥和私钥
RSAPublicKey publicKey = (RSAPublicKey) map.get("public");
RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");
// 生成公钥文件
File publicKeyFile = new File("D:" + File.separator + "public.key");
FileOutputStream publicKeyStream = new FileOutputStream(publicKeyFile);
// 获取公钥标准编码并进行base64加密
String publicKeyBase = new String(Base64.encodeBase64(publicKey.getEncoded()));
System.out.println("公钥为:" + new String(publicKey.getEncoded()));
System.out.println("BASE64公钥为:" + publicKeyBase);
publicKeyStream.write(publicKeyBase.getBytes());
publicKeyStream.close();
//
// // // 生成私钥文件
File privateKeyFile = new File("D:" + File.separator + "private.key");
FileOutputStream privateKeyStream = new FileOutputStream(privateKeyFile);
// 获取私钥标准编码并进行base64加密
String privateKeyBase = new String(Base64.encodeBase64(privateKey.getEncoded()));
System.out.println("私钥为:" + new String(privateKey.getEncoded()));
System.out.println("BASE64私钥为:" + privateKeyBase);
privateKeyStream.write(privateKeyBase.getBytes());
privateKeyStream.close(); X509EncodedKeySpec pubX509 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyBase.getBytes()));
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyBase.getBytes())); KeyFactory keyfactory = KeyFactory.getInstance("RSA");
RSAPublicKey newPublicKey = (RSAPublicKey) keyfactory.generatePublic(pubX509);
RSAPrivateKey newPrivateKey = (RSAPrivateKey) keyfactory.generatePrivate(priPKCS8);
System.out.println(publicKey.equals(newPublicKey));
System.out.println(privateKey.equals(newPrivateKey));*/
}
}

JAVA 非对称加密工具的更多相关文章

  1. JProtector java应用加密工具

    JProtector    专业的java项目加密工具 JProtector简介: JProtector 专业的java项目加密工具.目前java开发的项目发布的时候需要将项目发布到用户手中,但由于一 ...

  2. Java 非对称加密

    package test; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.Object ...

  3. Java AES 加密工具类

    package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.S ...

  4. java MD5Utils 加密工具类

    package com.sicdt.library.core.utils; import java.io.File; import java.io.FileInputStream; import ja ...

  5. Java MD5加密工具类

    public final static String MD5(String s) { char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', ' ...

  6. Java安全之对称加密、非对称加密、数字签名

    原文地址: http://blog.csdn.net/furongkang/article/details/6882039 Java中加密分为两种方式一个是对称加密,另一个是非对称加密.对称加密是因为 ...

  7. RSA非对称加密简析-java

    1 非对称加密算法 1.1 概述 1976年,美国学者Dime和Henman为解决信息公开传送和密钥管理问题,提出一种新的密钥交换协议,允许在不安全的媒体上的通讯双方交换信息,安全地达成一致的密钥,这 ...

  8. Java加密与解密笔记(三) 非对称加密

    非对称的特点是加密和解密时使用的是不同的钥匙.密钥分为公钥和私钥,用公钥加密的数据只能用私钥进行解密,反之亦然. 另外,密钥还可以用于数字签名.数字签名跟上文说的消息摘要是一个道理,通过一定方法对数据 ...

  9. java 加密工具类(MD5、RSA、AES等加密方式)

    1.加密工具类encryption MD5加密 import org.apache.commons.codec.digest.DigestUtils; /** * MD5加密组件 * * @autho ...

随机推荐

  1. C#属性升级版--自动属性-chapter 3 P34-36

    使用C#属性,能够通过将数据与它的设置和检索方法分离的方式公开类中的一段数据.   例如:   namespace LanguageFeatures { public class Product { ...

  2. PostgreSQL psql中如何查看快捷功能的对应函数

    在psql中,我们可以通过一系列的的快捷命令查看数据库元素,如:\d 查看当前搜索路径下的表,那么内部用到的SQL语句是什么呢,可以通过命令来设置是否打印出来: apple=# \set ECHO_H ...

  3. keras_基本网络层结构(1)_常用层

    参考文献: https://blog.csdn.net/sinat_26917383/article/details/72857454 http://keras-cn.readthedocs.io/e ...

  4. CMake与Make

    大家都知道,写程序大体步骤为: 1.用编辑器编写源代码,如.c文件. 2.用编译器编译代码生成目标文件,如.o. 3.用链接器连接目标代码生成可执行文件,如.exe. 但如果源文件太多,一个一个编译时 ...

  5. Locust 关联

    关联的概念 用户登入后,服务器通常会给用户返回一个sessionID, 用户每次登陆,服务器返回的都会不同. 那么在自动化测试中,让系统自动登入账号就会被中断.那么我们可以通过取得服务器每次返回的se ...

  6. redis安装全过程

    1. 从官网上下载redis. 2.安装gcc 3.进入./redis/src目录下make MALLOC =libc 4.遇到的问题 Redis简介: Redis是一个开源的使用ANSI C语言编写 ...

  7. C#最受欢迎功能 -- C#1至C#7

    不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: http://www.dotnetcurry.com/csharp/1 ...

  8. 移植wpa_supplicant2.5及界面配置wifi(原创)

    JP5G开发机上需要图形界面配置 wifi网络,为此移植了wpa_supplicant2.5. 1.参考wpa_supplicant-2.5移植与使用l http://blog.csdn.net/hk ...

  9. bzoj 3144 [Hnoi2013]切糕——最小割

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3144 一根纵轴上切一个点,可以把一根纵轴上的点连成一串来体现.自己的写法是每个点连向前一个点 ...

  10. Spring 部署Tomcat 404 错误解决方案

    将Spring项目部署到tomcat后,访问网页出现404错误 HTTP Status 404 – Not Found The origin server did not find a current ...