RSA非对称加密

package com.test.security;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.Cipher;

public class RSAUtil {

/***
* 创建密钥
*
* @param savepath
* @return
*/
public static KeyPair createKeyPair(String savepath) {

KeyPairGenerator kg = null;
try {
kg = KeyPairGenerator.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
// 注意密钥大小最好为1024,否则解密会有乱码情况.
SecureRandom sr = new SecureRandom();
kg.initialize(1024, sr);
KeyPair kp = kg.generateKeyPair();
if (null != savepath) {
FileOutputStream fos = new FileOutputStream(savepath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(kp);
}
return kp;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

/****
* 创建公钥
*
* @param mode
* @param pubex
* @return
* @throws Exception
*/
public static RSAPublicKey createRSAPublicKey(String mode, String pubex)
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(mode), new BigInteger(pubex));

return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
}

public static RSAPrivateKey createRSAPrivateKey(String mod, String pubex)
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 privatekeyspec = new RSAPrivateKeySpec(
new BigInteger(mod), new BigInteger(pubex));
return (RSAPrivateKey) keyFac.generatePrivate(privatekeyspec);
}

/***
* 加密
*
* @param key
* @param data
* @return
* @throws Exception
*/
public static byte[] encrypt(Key key, byte[] data) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, key);
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(data.length);
int leavedSize = data.length % blockSize;
int blocksSize = leavedSize != 0 ? data.length / blockSize + 1
: data.length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (data.length - i * blockSize > 0) {
if (data.length - i * blockSize > blockSize)
cipher.doFinal(data, i * blockSize, blockSize, raw, i
* outputSize);
else
cipher.doFinal(data, i * blockSize, data.length - i
* blockSize, raw, i * outputSize);
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}

/***
* 解密
*
* @param key
* @param raw
* @return
* @throws Exception
*/
public static byte[] decrypt(Key key, byte[] raw) throws Exception {
try {
Cipher cipher = Cipher.getInstance("RSA",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(Cipher.DECRYPT_MODE, key);
int blockSize = cipher.getBlockSize();
ByteArrayOutputStream bout = new ByteArrayOutputStream(64);
int j = 0;
while (raw.length - j * blockSize > 0) {
bout.write(cipher.doFinal(raw, j * blockSize, blockSize));
j++;
}
return bout.toByteArray();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}

/**
* HEX编码 将形如0x12 0x2A 0x01 转换为122A01
*
* @param data
* @return
*/
public static String hexEncode(byte[] data) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < data.length; i++) {
String tmp = Integer.toHexString(data[i] & 0xff);
if (tmp.length() < 2) {
buffer.append('0');
}
buffer.append(tmp);
}
String retStr = buffer.toString().toUpperCase();
return retStr;
}

/**
* HEX解码 将形如122A01 转换为0x12 0x2A 0x01
*
* @param data
* @return
*/
public static byte[] hexDecode(String data) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int i = 0; i < data.length(); i += 2) {
String onebyte = data.substring(i, i + 2);
int b = Integer.parseInt(onebyte, 16) & 0xff;
out.write(b);
}
return out.toByteArray();
}

public static void main(String [] b) throws Exception{



// KeyPair kp=RSAUtil.createKeyPair(null);
// RSAPublicKey rsapu=(RSAPublicKey)kp.getPublic();
// String pu_mode=rsapu.getModulus().toString();
// String pu_e=rsapu.getPublicExponent().toString();
// System.out.println("1==="+pu_mode);
// System.out.println("1===---="+pu_e);
//
// RSAPrivateKey rsapr=(RSAPrivateKey)kp.getPrivate();
// String pr_mode=rsapr.getModulus().toString();
// String pr_e=rsapr.getPrivateExponent().toString();
// System.out.println("1==="+pr_mode);
// System.out.println("1===---="+pr_e);

String p_mode="103190509448526906675184354225772346071429888143058908524801320734882636647794976703442201427451980154742515355242923272405486638063926806046954606187430474469959480371551827585540584042060956419779924840041960580937115749057780905135508742149286660980732571577959483114185713853564532704043098107986880520621";
String p_e="65537";
String r_mode="103190509448526906675184354225772346071429888143058908524801320734882636647794976703442201427451980154742515355242923272405486638063926806046954606187430474469959480371551827585540584042060956419779924840041960580937115749057780905135508742149286660980732571577959483114185713853564532704043098107986880520621";
String r_e="57110074739574887956659775579031520581607847061002024369486987569997529851109626623289929169402181549241890450432825274477309090272925052750798640783439736345203812293923880602549365460815681279861017814602597215150254395324235620932778059499956426017769871307287955163294147786931713281819809460060173604129";

String abbb="快接啊第三方空间卡斯达克反抗拉萨的asdjfkjaksjdfalsdfkl5455865655568568adsfasdef";
RSAPublicKey rsap=createRSAPublicKey(p_mode, p_e);

byte [] aaaa=encrypt(rsap,abbb.getBytes("UTF-8"));
String aa_s=hexEncode(aaaa);
System.out.println(aa_s);

RSAPrivateKey rep=createRSAPrivateKey(r_mode, r_e);
byte[] accc=decrypt(rep, aaaa);
System.out.println(new String(accc,"UTF-8"));


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值