自动生成RSA密钥,并进行加密和解密2


package com.digican.books.a0102.b0101;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

/**
* 2010年9月1日
* @author 姚大庆
*/
public class DigicanRSA {

public static void main(String[] args) throws Exception {
//生成密钥
//getAutoCreateRSA("d:\\key\\rsapublickey","d:\\key\\rsaprivatekey");
//用公钥对AES密钥加密
/*byte[] b = getRSAPublicEncode("d:\\key\\rsapublickey","d:\\key\\aeskey");
FileOutputStream fos = new FileOutputStream("d:\\key\\rsajiamiaes");
fos.write(b);
fos.flush();
fos.close();*/
//用私钥来解AES密钥
byte[] b = getRSAPrivateDecode("d:\\key\\rsaprivatekey","d:\\key\\rsajiamiaes");
FileOutputStream fos = new FileOutputStream("d:\\key\\rsajiemiaes");
fos.write(b);
fos.flush();
fos.close();
}

/**
* 生成密钥
* 自动产生RSA1024位密钥;并保持到文件里
* rsaPublicKeyFilePath 公钥的文件路径名,例如:d:\publickey.txt
* rsaPrivateKeyFilePath 公钥的文件路径名,例如:d:\privatekey.txt
* @throws NoSuchAlgorithmException
* @throws IOException
*/
public static void getAutoCreateRSA(String rsaPublicKeyFilePath,String rsaPrivateKeyFilePath) throws NoSuchAlgorithmException, IOException{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
PublicKey puk = kp.getPublic();
PrivateKey prk = kp.getPrivate();
FileOutputStream pufos = new FileOutputStream(rsaPublicKeyFilePath);
ObjectOutputStream puoos = new ObjectOutputStream(pufos);
puoos.writeObject(puk);
puoos.flush();
puoos.close();
FileOutputStream prfos = new FileOutputStream(rsaPrivateKeyFilePath);
ObjectOutputStream proos = new ObjectOutputStream(prfos);
proos.writeObject(prk);
proos.flush();
proos.close();
}

/**
* 加密
* 使用非对称方式生成的公钥进行加密RSA1024
* 使用公钥把AES密钥进行加密
* rsaPublicKeyFilePath 公钥文件路径;例如:d:\\rsapublickey.txt
* aesKeyFilePath 对称密钥文件路径
* @throws IOException
* @throws ClassNotFoundException
*/
public static byte[] getRSAPublicEncode(String rsaPublicKeyFilePath,String aesKeyFilePath) throws IOException, ClassNotFoundException {
//读取公钥
FileInputStream fis = new FileInputStream(rsaPublicKeyFilePath);
ObjectInputStream ois = new ObjectInputStream(fis);
RSAPublicKey pbk = (RSAPublicKey)ois.readObject();
BigInteger e = pbk.getPublicExponent();
BigInteger n = pbk.getModulus();

//读取对称密钥
File file = new File(aesKeyFilePath);
byte[] baes = new byte[(int) file.length()];
FileInputStream fisaes = new FileInputStream(file);
fisaes.read(baes);

BigInteger m = new BigInteger(baes);
BigInteger c = m.modPow(e, n);//因为RSA算法要求整型数m的值必须小于n,所以需要计算
byte[] bb = c.toByteArray();
return bb;
}

/**
* 解密
* 通过非对称生成的公钥和私钥
* 用私钥解公钥加密数据RSA1024
* rsaPrivateKeyFilePath 私钥文件路径;例如:d:\\rsaprivatekey.txt
* publicEncodeFilePath 公钥加密后的数据;例如:d:\\rsapublickeyjiamihou.txt
*/
public static byte[] getRSAPrivateDecode(String rsaPrivateKeyFilePath,String publicEncodeFilePath) throws Exception{
/*BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(publicEncodeFilePath)));//加密后文件
String ctext = br.readLine();
BigInteger c = new BigInteger(ctext);*/

File file = new File(publicEncodeFilePath);
byte[] bjia = new byte[(int) file.length()];
FileInputStream fisaes = new FileInputStream(file);
fisaes.read(bjia);
BigInteger c = new BigInteger(bjia);

FileInputStream fis = new FileInputStream(rsaPrivateKeyFilePath);
ObjectInputStream ois = new ObjectInputStream(fis);
RSAPrivateKey prk = (RSAPrivateKey)ois.readObject();
BigInteger d = prk.getPrivateExponent();
BigInteger n = prk.getModulus();
BigInteger m = c.modPow(d, n);
byte[] mt = m.toByteArray();
return mt;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值