說明bcprov-ext-jdk16-141.jar包在 http://www.bouncycastle.org/ 上下載
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package 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.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class GenKey {
public static void main(String[] args){//建立密鑰
try {
//建立金鑰組產生器
KeyPairGenerator KPG = KeyPairGenerator.getInstance("RSA");
//初始化金鑰產生器
KPG.initialize(1024);
//產生金鑰組
KeyPair KP=KPG.genKeyPair();
//擷取公開金鑰和密鑰
PublicKey pbKey=KP.getPublic();
PrivateKey prKey=KP.getPrivate();
//儲存公開金鑰到檔案
FileOutputStream out=new FileOutputStream("RSAPublic.dat");
ObjectOutputStream fileOut=new ObjectOutputStream(out);
fileOut.writeObject(pbKey);
//儲存密鑰到檔案
FileOutputStream outPrivate=new FileOutputStream("RSAPrivate.dat");
ObjectOutputStream privateOut=new ObjectOutputStream(outPrivate);
privateOut.writeObject(prKey);
} catch (Exception ex) {
Logger.getLogger(GenKey.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rsatest;
import cipherinputstream.EncryptAndDecrypt;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.security.Key;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
*
* @author Administrator
*/
public class RRSSA {
public static void main(String[] args) {
//要用作加密或解密的檔案名稱
String dataFileName = args[0];
//是區分是加密還是解密,加密為encrypt,解密為decrypt
String opMode = args[1];
String keyFileName = null;
//密鑰存放的檔案名稱
if (opMode.equalsIgnoreCase("encrypt")) {
keyFileName = "RSAPublic.dat";
} else {
keyFileName = "RSAPrivate.dat";
}
try {
//產生密鑰
FileInputStream keyFIS = new FileInputStream(keyFileName);
ObjectInputStream OIS = new ObjectInputStream(keyFIS);
Key key = (Key) OIS.readObject();
//建立並初始化密碼器
Cipher cp = Cipher.getInstance("RSA", new BouncyCastleProvider());//此處不能少
//BouncyCastLeProvider類在bcprov-ext-jdk16-141.jar中
if (opMode.equalsIgnoreCase("encrypt")) {
cp.init(Cipher.ENCRYPT_MODE, key);
} else if (opMode.equalsIgnoreCase("decrypt")) {
cp.init(Cipher.DECRYPT_MODE, key);
} else {
return;
}
FileInputStream dataFIS = new FileInputStream(dataFileName);
//取得要加密的資料
int size = dataFIS.available();
byte[] encryptByte = new byte[size];
dataFIS.read(encryptByte);
//如果是加密操作
if (opMode.equalsIgnoreCase("encrypt")) {
//建立檔案輸出資料流
FileOutputStream FOS = new FileOutputStream("mi.txt");
//RSA演算法必須採用分塊加密
//取得RSA加密的塊的大小
int blockSize = cp.getBlockSize();
//根據給定的輸入長度 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();
} else {//如果是解密操作
FileOutputStream FOS = new FileOutputStream("jiemi.txt");
int blockSize = cp.getBlockSize();
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);
}
}
}