//package
/*
运行本程序你需要下载JCEBouncy Castle的JCE with Provider and Lightweight API
网止是
配置如下
在WINDOWS中你需要把下载的bcprovjdkjar文件拷贝到两个地方
一个在你安装的JDK目录中比如说我的是C:\jsdkrc\jre\lib\ext
另一个在你的JDK运行环境中我的是在C:\Program Files\Java\jrerc\lib\ext
另外还要在对两个javasecurity进行修改
我的在 C:\jsdkrc\jre\lib\security\javasecurity
C:\Program Files\Java\jrerc\lib\security\javasecurity;
在javasecurity中加入
securityprovider=orgbouncycastlejceproviderBouncyCastleProvider
如果一切顺利你就可以运行本程序了
该程序具有对你的文件加解密功能需要你指定的数据程序中已给了接口
比如说你指定了要加密的文件名txt加密后的文件存放位置txt
还有口令password如liufeng后运行该程序那么txt 中将是txt的密文
注意口令是解密的钥匙不要忘记
其他解密过程自己参考
本程序利用会话密钥加密提供很多接口如果你项目中需要加密过程可以稍加改进为你所用
*/
import javasecurity*;
import javasecurityspec*;
import javaxcrypto*;
import javaxcryptospec*;
import javaio*;
import javautil*;
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=txt;//该文件放置加密的私钥
private static String TEXT_FILE=txt;//要加密的文件
private static String ENCRPTOR_TEXT_FILE=txt;//被加密后的文件
private static String DENCRYPTOR_TEXT_FILE=txt;//解密后的文件
private static String password=liufeng;//口令用于加密私钥
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){
thispassword=password;
}
//create a RSA secretKey
public static void createKey()throws Exception{
KeyPairGenerator keyPairGenerator=KeyPairGeneratorgetInstance(RSA);
keyPairGeneratorinitialize();
KeyPair keyPair=keyPairGeneratorgenKeyPair();
//得到公钥的字节数组
publicKeyBytes=keyPairgetPublic()getEncoded();
//得到私钥
byte[] privateKeyBytes=keyPairgetPrivate()getEncoded();
byte[] encrytedPrivatekey=passwordEncrypt(passwordtoCharArray()privateKeyBytes);
FileOutputStream fos=new FileOutputStream(ENCRYPT_PRIVATEKEY_FILE);
foswrite(encrytedPrivatekey);
fosclose();
}
//通过给的口令加密私钥
private static byte[] passwordEncrypt(char[] passwordbyte[] privateKeyBytes)
throws Exception{
//create byte salt
byte[] salt=new byte[];
Random random=new Random();
randomnextBytes(salt);
//create a PBE key and cipher
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactorygetInstance(PBEWithSHAAndTwofishCBC);
SecretKey key=keyFactorygenerateSecret(keySpec);
PBEParameterSpec paramSpec=new PBEParameterSpec(saltITERATIONS);
Cipher cipher=CiphergetInstance(PBEWithSHAAndTwofishCBC);
cipherinit(CipherENCRYPT_MODEkeyparamSpec);
//Encrypt the byte[]
byte[] cipherPriKey=cipherdoFinal(privateKeyBytes);
//write out salt and then the cipherPriKey
ByteArrayOutputStream baos=new ByteArrayOutputStream();
baoswrite(salt);
baoswrite(cipherPriKey);
return baostoByteArray();
}
//用会话密钥加密给定的文件然后用公钥加密会话密钥并存入文件中
//最后加密后的文件由密钥长度+已加密的密钥(会话密钥)+密文
public static void encrypt()throws Exception{
//转换成RSA密钥
XEncodedKeySpec keySpec=new XEncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory=KeyFactorygetInstance(RSA);
PublicKey publickey=keyFactorygeneratePublic(keySpec);
//打开存贮密文的文件
DataOutputStream output=new DataOutputStream(new FileOutputStream(ENCRPTOR_TEXT_FILE));
//创建RSA的CIpher
Cipher rsaCipher=CiphergetInstance(RSA/ECB/PKCSPadding);
rsaCipherinit(CipherENCRYPT_MODEpublickey);
//创建会话密钥(Rijndael)
KeyGenerator rijndaelKeyGenerator=KeyGeneratorgetInstance(Rijndael);
rijndaelKeyGeneratorinit();
Key rijndaelKey=rijndaelKeyGeneratorgenerateKey();
//公钥加密会话密钥
byte[] encodedKeyBytes=rsaCipherdoFinal(rijndaelKeygetEncoded());
outputwriteInt(encodedKeyByteslength);
outputwrite(encodedKeyBytes);
//产生IV向量
SecureRandom random=new SecureRandom();
byte[] iv=new byte[];
randomnextBytes(iv);
outputwrite(iv);
//加密正文
IvParameterSpec spec=new IvParameterSpec(iv);
Cipher symmetricCipher=CiphergetInstance(Rijndael/CBC/PKCSPadding);
symmetricCipherinit(CipherENCRYPT_MODErijndaelKeyspec);
CipherOutputStream cos=new CipherOutputStream(outputsymmetricCipher);
FileInputStream input=new FileInputStream(TEXT_FILE);
int theByte=;
while((theByte=inputread())!=){
coswrite(theByte);
}
inputclose();
cosclose();
return;
}
//得到私钥
private static byte[] passwordDecrypt(char[] passwordbyte[] ciphertext)
throws Exception{
byte[] salt=new byte[];
ByteArrayInputStream bais=new ByteArrayInputStream(ciphertext);
baisread(salt);
byte[] remainingCiphertext=new byte[ciphertextlength];
baisread(remainingCiphertextciphertextlength);
PBEKeySpec keySpec=new PBEKeySpec(password);
SecretKeyFactory keyFactory=SecretKeyFactorygetInstance(PBEWithSHAAndTwofishCBC);
SecretKey key=keyFactorygenerateSecret(keySpec);
PBEParameterSpec paramSpec=new PBEParameterSpec(saltITERATIONS);
Cipher cipher=CiphergetInstance(PBEWithSHAAndTwofishCBC);
cipherinit(CipherDECRYPT_MODEkeyparamSpec);
return cipherdoFinal(remainingCiphertext);
}
//解密加密的文件
public static void decrypt()
throws Exception{
FileInputStream fis=new FileInputStream(ENCRYPT_PRIVATEKEY_FILE);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int theByte=;
while((theByte=fisread())!=){
baoswrit