实现一对密钥对整个项目所有加密解密文件都适用的方法,采用先生成一对密钥.保存到xml文件中,以后获得私匙和公钥只需要从xml文件中取得就可以了.
/**
* 把成生的一对密钥保存到RSAKey.xml文件中
*/
public void saveRSAKey() {
try {
SecureRandom sr = new SecureRandom();
KeyPairGenerator kg = KeyPairGenerator.getInstance(\"RSA\",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
//注意密钥大小最好为1024,否则解密会有乱码情况.
kg.initialize(1024, sr);
FileOutputStream fos = new FileOutputStream(\"C:/RSAKey.xml\");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//生成密钥
oos.writeObject(kg.generateKeyPair());
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
注意:需要从http://www.bouncycastle.org下载bcprov-jdk14-137.jar包.
获取密钥方法如下:
/**
* 获得RSA加密的密钥。
* @return KeyPair返回对称密钥
*/
public static KeyPair getKeyPair() {
//产生新密钥对
KeyPair kp;
try {
String fileName = \"conf/RASKey.xml\";
InputStream is = FileUtils.class.getClassLoader()
.getResourceAsStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (KeyPair) oos.readObject();
oos.close();
} catch (Exception e) {
throw new EprasRuntimeException(\"读取加密文件出错.\", e);
}
return kp;
}
文件采用RSA算法加密文件
/**
* 文件file进行加密并保存目标文件destFile中
* @param srcFileName
* 要加密的文件 如c:/test/srcFile.txt
* @param destFileName
* 加密后存放的文件名 如c:/加密后文件.txt
*/
public static void encryptFile(String srcFileName,
String destFileName) throws Exception {
OutputStream outputWriter = null;
InputStream inputReader = null;
try {
Cipher cipher = Cipher.getInstance(\"RSA/ECB/PKCS1Padding\",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] buf = new byte[100];
int bufl;
cipher.init(Cipher.ENCRYPT_MODE, getKeyPair().getPublic());
outputWriter = new FileOutputStream(destFileName);
inputReader = new FileInputStream(srcFileName);
while ((bufl = inputReader.read(buf)) != -1) {
byte[] encText = null;
byte[] newArr = null;
if (buf.length == bufl) {
newArr = buf;
} else {
newArr = new byte[bufl];
for (int i = 0; i < bufl; i++) {
newArr = (byte) buf;
}
}
encText = cipher.doFinal(newArr);
outputWriter.write(encText);
}
outputWriter.flush();
} catch (Exception e) {
throw e;
} finally {
try {
if (outputWriter != null) {
outputWriter.close();
}
if (inputReader != null) {
inputReader.close();
}
} catch (Exception e) {
}
}
}
文件采用RSA算法解密文件
/**
* 文件file进行加密并保存目标文件destFile中
* @param srcFileName
* 已加密的文件 如c:/加密后文件.txt
* @param destFileName
* 解密后存放的文件名 如c:/ test/解密后文件.txt
*/
public static void decryptFile(String srcFileName,
String destFileName) throws Exception {
OutputStream outputWriter = null;
InputStream inputReader = null;
try {
Cipher cipher = Cipher.getInstance(\"RSA/ECB/PKCS1Padding\",
new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] buf = new byte[128];
int bufl;
cipher.init(Cipher.DECRYPT_MODE, getKeyPair().getPrivate());
outputWriter = new FileOutputStream(destFileName);
inputReader = new FileInputStream(srcFileName);
while ((bufl = inputReader.read(buf)) != -1) {
byte[] encText = null;
byte[] newArr = null;
if (buf.length == bufl) {
newArr = buf;
} else {
newArr = new byte[bufl];
for (int i = 0; i < bufl; i++) {
newArr = (byte) buf;
}
}
encText = cipher.doFinal(newArr);
outputWriter.write(encText);
}
outputWriter.flush();
} catch (Exception e) {
throw e;
} finally {
try {
if (outputWriter != null) {
outputWriter.close();
}
if (inputReader != null) {
inputReader.close();
}
} catch (Exception e) {
}
}
}
如果对于大文件加密采用RSA算法执行速度要非常非常慢。