1.生产双钥
/**
?*
?*/
package com.sinaapp.zhangziyou.rsa;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
/**
?* 产生公钥和私钥对,并且保存在文件中,公钥 pk.dat,私钥 sk.dat
?*
?* @author Administrator
?*
?*/
public class KeyGen {
/**
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 加密的种子信息
String keyInfo = “ASDFSDFNUGD__TYTY”;
KeyGen kg = new KeyGen();
kg.genKeys(keyInfo);
}
/**
* 根据keyInfo产生公钥和私钥,并且保存到pk.dat和sk.dat文件中
*
* @param keyInfo
* @throws Exception
*/
public void genKeys(String keyInfo) throws Exception {
KeyPairGenerator keygen = KeyPairGenerator.getInstance(“RSA”);
SecureRandom random = new SecureRandom();
long value = random.nextLong();
System.out.println(value);
random.setSeed(keyInfo.getBytes());
// 初始加密,长度为512,必须是大于512才可以的
keygen.initialize(1024, random);
// 取得密钥对
KeyPair kp = keygen.generateKeyPair();
// 取得公钥
PublicKey publicKey = kp.getPublic();
System.out.println(publicKey);
saveFile(publicKey, “pk.txt”);
// 取得私钥
PrivateKey privateKey = kp.getPrivate();
System.out.println(privateKey);
saveFile(privateKey, “sk.txt”);
}
/**
* 保存对象到文件
*
* @param obj
* @param fileName
* @throws Exception
*/
private void saveFile(Object obj, String fileName) throws Exception {
ObjectOutputStream output = new ObjectOutputStream(
new FileOutputStream(fileName));
output.writeObject(obj);
output.close();
}
}
2加密 文件
package com.sinaapp.zhangziyou.rsa;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
//http://polydistortion.net/bc/index.html,这个包,可以从这里下载
//加密算法
public class encryptway {
public static void main(String[] args) throws Exception {
encryptway rsa = new encryptway();
RSAPublicKey publickKey = (RSAPublicKey) rsa.readFromFile(“pk.txt”);
try {
// 生成密钥
// 创建并初始化密码器
Cipher cp = Cipher.getInstance(“RSA”, new BouncyCastleProvider());// 此处不能少
// BouncyCastLeProvider类在bcprov-ext-jdk16-141.jar中
FileOutputStream FOS = new FileOutputStream(“mi.txt”);
FileInputStream dataFIS = new FileInputStream(“原文.txt”);
// 取得要加密的数据
int size = dataFIS.available();
byte[] encryptByte = new byte[size];
dataFIS.read(encryptByte);
// 如果是加密操作
// 建立文件输出流
cp.init(Cipher.ENCRYPT_MODE, publickKey);
// RSA算法必须采用分块加密
// 取得RSA加密的块的大小
int blockSize = cp.getBlockSize();
System.out.println(blockSize);
// 根据给定的输入长度 inputLen(以字节为单位),返回保存下一个 update 或 doFinal
// 操作结果所需的输出缓冲区长度(以字节为单位)。
int outputBlockSize = cp.getOutputSize(encryptByte.length);
// 确定要加密的次数(加密块的个数)
int leavedSize = encryptByte.length % blockSize;
int blocksNum = leavedSize == 0 ? encryptByte.length / blockSize
: encryptByte.length / blockSize + 1;
byte[] cipherData = new byte[blocksNum * outputBlockSize];
// 对每块数据分别加密
for (int i = 0; i < blocksNum; i++) {
if ((encryptByte.length – i * blockSize) > blockSize) {
cp.doFinal(encryptByte, i * blockSize, blockSize,
cipherData, i * outputBlockSize);
} else {
cp.doFinal(encryptByte, i * blockSize, encryptByte.length
– i * blockSize, cipherData, i * outputBlockSize);
}
}
FOS.write(cipherData);
FOS.close();
} catch (Exception ex) {
// Logger.getLogger(EncryptAndDecrypt.class.getName()).log(Level.SEVERE,
// null, ex);
System.out.println(ex);
}
}
private Object readFromFile(String fileName) throws Exception {
ObjectInputStream input = new ObjectInputStream(new FileInputStream(
fileName));
Object obj = input.readObject();
input.close();
return obj;
}
}
3.解密算法
package com.sinaapp.zhangziyou.rsa;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.security.interfaces.RSAPrivateKey;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
//解密算法
public class decryptway {
public static void main(String[] args) throws Exception {
decryptway rsa = new decryptway();
RSAPrivateKey privateKey = (RSAPrivateKey) rsa.readFromFile(“sk.txt”);
try {
// 生成密钥
// 创建并初始化密码器
Cipher cp = Cipher.getInstance(“RSA”, new BouncyCastleProvider());// 此处不能少
// BouncyCastLeProvider类在bcprov-ext-jdk16-141.jar中
FileInputStream dataFIS = new FileInputStream(“mi.txt”);
// 取得要加密的数据
int size = dataFIS.available();
byte[] encryptByte = new byte[size];
dataFIS.read(encryptByte);
// 如果是加密操作
// 建立文件输出流
FileOutputStream FOS = new FileOutputStream(“jiemi.txt”);
cp.init(Cipher.DECRYPT_MODE, privateKey);
// RSA算法必须采用分块加密
// 取得RSA加密的块的大小
int blockSize = cp.getBlockSize();
System.out.println(blockSize);
// 根据给定的输入长度 inputLen(以字节为单位),返回保存下一个 update 或 doFinal
// 操作结果所需的输出缓冲区长度(以字节为单位)。
int j = 0;
// 分别对各块数据进行解密
while ((encryptByte.length – j * blockSize) > 0) {
FOS.write(cp.doFinal(encryptByte, j * blockSize, blockSize));
j++;
}
FOS.close();
} catch (Exception ex) {
// Logger.getLogger(EncryptAndDecrypt.class.getName()).log(Level.SEVERE,
// null, ex);
}
}
private Object readFromFile(String fileName) throws Exception {
ObjectInputStream input = new ObjectInputStream(new FileInputStream(
fileName));
Object obj = input.readObject();
input.close();
return obj;
}
}
4.我弄这个居然弄了2天,郁闷啊
发表回复