//package
/*
運行本程序你需要下載JCE
網止是
配置如下
在WINDOWS中
一個在你安裝的JDK目錄中
另一個在你的JDK運行環境中
另外還要在對兩個java
我的在 C:\j
C:\Program Files\Java\j
在java
security
如果一切順利
該程序具有對你的文件加解密功能
比如說你指定了要加密的文件名
還有口令password如
注意口令是解密的鑰匙
其他解密過程自己參考
本程序利用會話密鑰加密
*/
import java
import java
import javax
import javax
import java
import java
public class FileEncryptorRSA {
private static final int ITERATIONS=
private static byte[] publicKeyBytes;//公鑰
private static byte[] privateKeyBytes;//私鑰
private static String SessionKey;//會話密鑰
public static String ENCRYPT_PRIVATEKEY_FILE=
private static String TEXT_FILE=
private static String ENCRPTOR_TEXT_FILE=
private static String DENCRYPTOR_TEXT_FILE=
private static String password=
public void setTEXT_FILE(String fileName){
TEXT_FILE=fileName;
}
public void setENCRYPT_PRIVATEKEY_FILE(String fileName){
ENCRYPT_PRIVATEKEY_FILE=fileName;
}
public String getENCRYPT_PRIVATEKEY_FILE(){
return ENCRYPT_PRIVATEKEY_FILE;
}
public void setENCRPTOR_TEXT_FILE(String fileName){
ENCRPTOR_TEXT_FILE=fileName;
}
public String getENCRPTOR_TEXT_FILE(){
return ENCRPTOR_TEXT_FILE;
}
public void setDENCRYPTOR_TEXT_FILE(String fileName){
DENCRYPTOR_TEXT_FILE=fileName;
}
public String getDENCRYPTOR_TEXT_FILE(){
return DENCRYPTOR_TEXT_FILE;
}
public void setPassword(String password){
this
}
//create a RSA secretKey
public static void createKey()throws Exception{
KeyPairGenerator keyPairGenerator=KeyPairGenerator
keyPairGenerator
KeyPair keyPair=keyPairGenerator
//得到公鑰的字節數組
publicKeyBytes=keyPair
//得到私鑰
byte[] privateKeyBytes=keyPair
byte[] encrytedPrivatekey=passwordEncrypt(password
FileOutputStream fos=new FileOutputStream(ENCRYPT_PRIVATEKEY_FILE);
fos
fos
}
//通過給的口令加密私鑰
private static byte[] passwordEncrypt(char[] password
throws Exception{
//create
byte[] salt=new byte[
Random random=new Random();
random
//create a PBE key and cipher
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactory
SecretKey key=keyFactory
PBEParameterSpec paramSpec=new PBEParameterSpec(salt
Cipher cipher=Cipher
cipher
//Encrypt the byte[]
byte[] cipherPriKey=cipher
//write out salt
ByteArrayOutputStream baos=new ByteArrayOutputStream();
baos
baos
return baos
}
//用會話密鑰加密給定的文件
//最後加密後的文件由密鑰長度+已加密的密鑰(會話密鑰)+密文
public static void encrypt()throws Exception{
//轉換成RSA密鑰
X
KeyFactory keyFactory=KeyFactory
PublicKey publickey=keyFactory
//打開存貯密文的文件
DataOutputStream output=new DataOutputStream(new FileOutputStream(ENCRPTOR_TEXT_FILE));
//創建RSA的CIpher
Cipher rsaCipher=Cipher
rsaCipher
//創建會話密鑰(Rijndael)
KeyGenerator rijndaelKeyGenerator=KeyGenerator
rijndaelKeyGenerator
Key rijndaelKey=rijndaelKeyGenerator
//公鑰加密會話密鑰
byte[] encodedKeyBytes=rsaCipher
output
output
//產生IV向量
SecureRandom random=new SecureRandom();
byte[] iv=new byte[
random
output
//加密正文
IvParameterSpec spec=new IvParameterSpec(iv);
Cipher symmetricCipher=Cipher
symmetricCipher
CipherOutputStream cos=new CipherOutputStream(output
FileInputStream input=new FileInputStream(TEXT_FILE);
int theByte=
while((theByte=input
cos
}
input
cos
return;
}
//得到私鑰
private static byte[] passwordDecrypt(char[] password
throws Exception{
byte[] salt=new byte[
ByteArrayInputStream bais=new ByteArrayInputStream(ciphertext);
bais
byte[] remainingCiphertext=new byte[ciphertext
bais
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactory
SecretKey key=keyFactory
PBEParameterSpec paramSpec=new PBEParameterSpec(salt
Cipher cipher=Cipher
cipher
return cipher
}
//解密加密的文件
public static void decrypt()
throws Exception{
FileInputStream fis=new FileInputStream(ENCRYPT_PRIVATEKEY_FILE);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int theByte=
while((theByte=fis
baos
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26781.html