實現一對金鑰組整個專案所有加密解密檔都適用的方法,採用先生成一對金鑰.保存到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演算法執行速度要非常非常慢;
全站熱搜
留言列表