
<!--Google 468*60橫幅廣告開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "image"; //2007-07-26: CSDN google_ad_channel = "6063905817"; google_color_border = "6699CC"; google_color_bg = "E6E6E6"; google_color_link = "FFFFFF"; google_color_text = "333333"; google_color_url = "AECCEB"; google_ui_features = "rc:6"; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--Google 468*60橫幅廣告結束-->
RSA壓縮加密/解壓縮解密
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.security.Key;
- import java.security.KeyPair;
- import java.security.KeyPairGenerator;
- import java.security.PrivateKey;
- import java.security.PublicKey;
- import java.security.SecureRandom;
- import java.util.Properties;
- import java.util.UUID;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import java.util.zip.ZipOutputStream;
-
- import javax.crypto.Cipher;
-
- /**
- *對文件壓縮加密/解密解壓縮對象類
- *
- */
- public class ZipEncrypt{
- private static PrivateKeyprivateKey;
- private static PublicKeypublicKey;
- private static void directoryZip(ZipOutputStreamout,Filef,Stringbase)
- throws Exception{
- //如果傳入的是目錄
- if (f.isDirectory()){
- File[]fl=f.listFiles();
- //創(chuàng)建壓縮的子目錄
- out.putNextEntry( new ZipEntry(base+ "/" ));
- if (base.length()== 0 ){
- base= "" ;
- } else {
- base=base+ "/" ;
- }
- for ( int i= 0 ;i<fl.length;i++){
- directoryZip(out,fl[i],base+fl[i].getName());
- }
- } else {
- //把壓縮文件加入rar中
- out.putNextEntry( new ZipEntry(base));
- FileInputStreamin= new FileInputStream(f);
- byte []bb= new byte [ 2048 ];
- int aa= 0 ;
- while ((aa=in.read(bb))!=- 1 ){
- out.write(bb, 0 ,aa);
- }
- in.close();
- }
- }
-
- /**
- *壓縮文件
- *@paramzos
- *@paramfile
- *@throwsException
- */
- private static void fileZip(ZipOutputStreamzos,Filefile)
- throws Exception{
- if (file.isFile()){
- zos.putNextEntry( new ZipEntry(file.getName()));
- FileInputStreamfis= new FileInputStream(file);
- byte []bb= new byte [ 2048 ];
- int aa= 0 ;
- while ((aa=fis.read(bb))!=- 1 ){
- zos.write(bb, 0 ,aa);
- }
- fis.close();
- System.out.println(file.getName());
- } else {
- directoryZip(zos,file, "" );
- }
- }
-
- /**
- *解壓縮文件
- *
- *@paramzis
- *@paramfile
- *@throwsException
- */
- private static void fileUnZip(ZipInputStreamzis,Filefile)
- throws Exception{
- ZipEntryzip=zis.getNextEntry();
- if (zip== null )
- return ;
- Stringname=zip.getName();
- Filef= new File(file.getAbsolutePath()+ "/" +name);
- if (zip.isDirectory()){
- f.mkdirs();
- fileUnZip(zis,file);
- } else {
- f.createNewFile();
- FileOutputStreamfos= new FileOutputStream(f);
- byte b[]= new byte [ 2048 ];
- int aa= 0 ;
- while ((aa=zis.read(b))!=- 1 ){
- fos.write(b, 0 ,aa);
- }
- fos.close();
- fileUnZip(zis,file);
- }
- }
-
- /**
- *對directory目錄下的文件壓縮,保存為指定的文件zipFile
- *
- *@paramdirectory
- *@paramzipFile
- */
- private static void zip(Stringdirectory,StringzipFile){
- try {
- ZipOutputStreamzos= new ZipOutputStream( new FileOutputStream(
- zipFile));
- fileZip(zos, new File(directory));
- zos.close();
- } catch (Exceptione){
- e.printStackTrace();
- }
- }
-
- /**
- *解壓縮文件zipFile保存在directory目錄下
- *
- *@paramdirectory
- *@paramzipFile
- */
- private static void unZip(Stringdirectory,StringzipFile){
- try {
- ZipInputStreamzis= new ZipInputStream(
- new FileInputStream(zipFile));
- Filef= new File(directory);
- f.mkdirs();
- fileUnZip(zis,f);
- zis.close();
- } catch (Exceptione){
- e.printStackTrace();
- }
- }
-
- /**
- *根據key的路徑文件獲得持久化成文件的key
- *<P>
- *例子:RsaEncrypt.getKey("c:/systemkey/private.key");
- *
- *@paramkeyPath
- *@return
- */
- public static KeygetKey(StringkeyPath) throws Exception{
- Keykey= null ;
- FileInputStreamfis= new FileInputStream(keyPath);
- ObjectInputStreamofs= new ObjectInputStream(fis);
- key=(Key)ofs.readObject();
- return key;
- }
-
- /**
- *把文件srcFile加密后存儲為destFile
- *
- *@paramsrcFile
- *@paramdestFile
- */
- private static void encrypt(StringsrcFile,StringdestFile,KeyprivateKey)
- throws Exception{
- Ciphercipher=Cipher.getInstance( "RSA" );
- cipher.init(Cipher.ENCRYPT_MODE,privateKey);
- FileInputStreamfis= new FileInputStream(srcFile);
- FileOutputStreamfos= new FileOutputStream(destFile);
- byte []b= new byte [ 53 ];
- while (fis.read(b)!=- 1 ){
- fos.write(cipher.doFinal(b));
- }
- fos.close();
- fis.close();
- }
-
- /**
- *把文件srcFile解密后存儲為destFile
- *
- *@paramsrcFile
- *@paramdestFile
- *@paramprivateKey
- *@throwsException
- */
- private static void decrypt(StringsrcFile,StringdestFile,KeyprivateKey)
- throws Exception{
- Ciphercipher=Cipher.getInstance( "RSA" );
- cipher.init(Cipher.DECRYPT_MODE,privateKey);
- FileInputStreamfis= new FileInputStream(srcFile);
- FileOutputStreamfos= new FileOutputStream(destFile);
- byte []b= new byte [ 64 ];
- while (fis.read(b)!=- 1 ){
- fos.write(cipher.doFinal(b));
- }
- fos.close();
- fis.close();
- }
-
- /**
- *對目錄srcFile下的所有文件目錄進行先壓縮后操作,然后保存為destfile
- *
- *@paramsrcFile
- *要操作的目錄如c:/test/test
- *@paramdestfile
- *壓縮加密后存放的文件名如c:/加密壓縮文件.zip
- *@paramkeyfile
- *公鑰存放地點
- */
- public static void encryptZip(StringsrcFile,Stringdestfile,Stringkeyfile) throws Exception{
- SecureRandomsr= new SecureRandom();
- KeyPairGeneratorkg=KeyPairGenerator.getInstance( "RSA" );
- kg.initialize( 512 ,sr);
- //產生新密鑰對
- KeyPairkp=kg.generateKeyPair();
- //獲得私匙
- ZipEncrypt.privateKey=kp.getPrivate();
- //獲得公鑰
- ZipEncrypt.publicKey=kp.getPublic();
- Filef= new File(keyfile);
- f.createNewFile();
- FileOutputStreamfos= new FileOutputStream(f);
- ObjectOutputStreamdos= new ObjectOutputStream(fos);
- dos.writeObject(ZipEncrypt.publicKey);
- Filetemp= new File(UUID.randomUUID().toString()+ ".zip" );
- temp.deleteOnExit();
- //先壓縮文件
- zip(srcFile,temp.getAbsolutePath());
- //對文件加密
- encrypt(temp.getAbsolutePath(),destfile,privateKey);
- temp.delete();
- }
-
- /**
- *對文件srcfile進行先解密后解壓縮,然后解壓縮到目錄destfile下
- *
- *@paramsrcfile
- *要解密和解壓縮的文件名如c:/目標.zip
- *@paramdestfile
- *解壓縮后的目錄如c:/abc
- *@parampublicKey
- *公鑰
- */
- public static void decryptUnzip(Stringsrcfile,Stringdestfile,
- KeypublicKey) throws Exception{
- //先對文件解密
- Filetemp= new File(UUID.randomUUID().toString()+ ".zip" );
- temp.deleteOnExit();
- decrypt(srcfile,temp.getAbsolutePath(),publicKey);
- //解壓縮
- unZip(destfile,temp.getAbsolutePath());
- temp.delete();
- }
-
- public static void main(Stringargs[]) throws Exception{
- Filef= new File( "." );
- Propertiesprop= new Properties();;
- FileInputStreamfis= new FileInputStream( "./conf.properties" );
- prop.load(fis);
- //要壓縮的目錄
- StringsrcPath=prop.getProperty( "SRC_PATH" );
- //壓縮后的存放文件
- StringdestZip=prop.getProperty( "DEST_FILE" );
- //壓縮加密后的publickey
- Stringkeyfile=prop.getProperty( "KEY_FILE" );
- ZipEncrypt.encryptZip(srcPath,destZip,keyfile);
- /*解密
- ZipEncrypt.decryptUnzip("e:/comXXX/comxxxx.zip","d:/comxxx",ZipEncrypt
- .getKey("e:/comXXX/public.key"));
- */
- }
- }
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.ObjectInputStream;
- import java.security.Key;
- import java.security.SecureRandom;
- import java.util.UUID;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import java.util.zip.ZipOutputStream;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- /**
- *對文件加密/解密和壓縮/解壓縮對象類
- *@author趙成明
- */
- public class ZipEncrypt{
- private void directoryZip(ZipOutputStreamout,Filef,Stringbase)
- throws Exception{
- //如果傳入的是目錄
- if (f.isDirectory()){
- File[]fl=f.listFiles();
- //創(chuàng)建壓縮的子目錄
- out.putNextEntry( new ZipEntry(base+ "/" ));
- if (base.length()== 0 ){
- base= "" ;
- } else {
- base=base+ "/" ;
- }
- for ( int i= 0 ;i<fl.length;i++){
- directoryZip(out,fl[i],base+fl[i].getName());
- }
- } else {
- //把壓縮文件加入rar中
- out.putNextEntry( new ZipEntry(base));
- FileInputStreamin= new FileInputStream(f);
- byte []bb= new byte [ 2048 ];
- int aa= 0 ;
- while ((aa=in.read(bb))!=- 1 ){
- out.write(bb, 0 ,aa);
- }
- in.close();
- }
- }
- /**
- *壓縮文件
- *@paramzos
- *@paramfile
- *@throwsException
- */
- private void fileZip(ZipOutputStreamzos,Filefile)
- throws Exception{
- if (file.isFile()){
- zos.putNextEntry( new ZipEntry(file.getName()));
- FileInputStreamfis= new FileInputStream(file);
- byte []bb= new byte [ 2048 ];
- int aa= 0 ;
- while ((aa=fis.read(bb))!=- 1 ){
- zos.write(bb, 0 ,aa);
- }
- fis.close();
- System.out.println(file.getName());
- } else {
- directoryZip(zos,file, "" );
- }
- }
- /**
- *解壓縮文件
- *
- *@paramzis
- *@paramfile
- *@throwsException
- */
- private void fileUnZip(ZipInputStreamzis,Filefile)
- throws Exception{
- ZipEntryzip=zis.getNextEntry();
- if (zip== null )
- return ;
- Stringname=zip.getName();
- Filef= new File(file.getAbsolutePath()+ "/" +name);
- if (zip.isDirectory()){
- f.mkdirs();
- fileUnZip(zis,file);
- } else {
- f.createNewFile();
- FileOutputStreamfos= new FileOutputStream(f);
- byte b[]= new byte [ 2048 ];
- int aa= 0 ;
- while ((aa=zis.read(b))!=- 1 ){
- fos.write(b, 0 ,aa);
- }
- fos.close();
- fileUnZip(zis,file);
- }
- }
- /**
- *對directory目錄下的文件壓縮,保存為指定的文件zipFile
- *
- *@paramdirectory
- *@paramzipFile
- */
- private void zip(Stringdirectory,StringzipFile){
- try {
- ZipOutputStreamzos= new ZipOutputStream( new FileOutputStream(
- zipFile));
- fileZip(zos, new File(directory));
- zos.close();
- } catch (Exceptione){
- e.printStackTrace();
- }
- }
- /**
- *解壓縮文件zipFile保存在directory目錄下
- *
- *@paramdirectory
- *@paramzipFile
- */
- private void unZip(Stringdirectory,StringzipFile){
- try {
- ZipInputStreamzis= new ZipInputStream( new FileInputStream(zipFile));
- Filef= new File(directory);
- f.mkdirs();
- fileUnZip(zis,f);
- zis.close();
- } catch (Exceptione){
- e.printStackTrace();
- }
- }
- /**
- *根據key的路徑文件獲得持久化成文件的key
- *<P>
- *例子:RsaEncrypt.getKey("c:/systemkey/private.key");
- *
- *@paramkeyPath
- *@return
- */
- private KeygetKey(StringkeyPath) throws Exception{
- FileInputStreamfis= new FileInputStream(keyPath);
- byte []b= new byte [ 16 ];
- fis.read(b);
- SecretKeySpecdks= new SecretKeySpec(b, "AES" );
- fis.close();
- return dks;
- }
- /**
- *把文件srcFile加密后存儲為destFile
- *
- *@paramsrcFile
- *@paramdestFile
- */
- private void encrypt(StringsrcFile,StringdestFile,KeyprivateKey)
- throws Exception{
- SecureRandomsr= new SecureRandom();
- Ciphercipher=Cipher.getInstance( "AES/CBC/PKCS5Padding" );
- IvParameterSpecspec= new IvParameterSpec(privateKey.getEncoded());
- cipher.init(Cipher.ENCRYPT_MODE,privateKey,spec,sr);
- FileInputStreamfis= new FileInputStream(srcFile);
- FileOutputStreamfos= new FileOutputStream(destFile);
- byte []b= new byte [ 2048 ];
- while (fis.read(b)!=- 1 ){
- fos.write(cipher.doFinal(b));
- }
- fos.close();
- fis.close();
- }
- /**
- *把文件srcFile解密后存儲為destFile
- *
- *@paramsrcFile
- *@paramdestFile
- *@paramprivateKey
- *@throwsException
- */
- private void decrypt(StringsrcFile,StringdestFile,KeyprivateKey)
- throws Exception{
- SecureRandomsr= new SecureRandom();
- Cipherciphers=Cipher.getInstance( "AES/CBC/PKCS5Padding" );
- IvParameterSpecspec= new IvParameterSpec(privateKey.getEncoded());
- ciphers.init(Cipher.DECRYPT_MODE,privateKey,spec,sr);
- FileInputStreamfis= new FileInputStream(srcFile);
- FileOutputStreamfos= new FileOutputStream(destFile);
- byte []b= new byte [ 2064 ];
- while (fis.read(b)!=- 1 ){
- fos.write(ciphers.doFinal(b));
- }
- fos.close();
- fis.close();
- }
- /**
- *對目錄srcFile下的所有文件目錄進行先壓縮后操作,然后保存為destfile
- *
- *@paramsrcFile
- *要操作的目錄如c:/test/test
- *@paramdestfile
- *壓縮加密后存放的文件名如c:/加密壓縮文件.zip
- *@paramkeyfile
- *公鑰存放地點
- */
- public void encryptZip(StringsrcFile,Stringdestfile,Stringkeyfile) throws Exception{
- SecureRandomsr= new SecureRandom();
- KeyGeneratorkg=KeyGenerator.getInstance( "AES" );
- kg.init( 128 ,sr);
- SecretKeykey=kg.generateKey();
- Filef= new File(keyfile);
- if (!f.getParentFile().exists())
- f.getParentFile().mkdirs();
- f.createNewFile();
- FileOutputStreamfos= new FileOutputStream(f);
- fos.write(key.getEncoded());
- Filetemp= new File(UUID.randomUUID().toString()+ ".zip" );
- temp.deleteOnExit();
- //先壓縮文件
- zip(srcFile,temp.getAbsolutePath());
- //對文件加密
- encrypt(temp.getAbsolutePath(),destfile,key);
- temp.delete();
- }
- /**
- *對文件srcfile進行先解密后解壓縮,然后解壓縮到目錄destfile下
- *
- *@paramsrcfile
- *要解密和解壓縮的文件名如c:/目標.zip
- *@paramdestfile
- *解壓縮后的目錄如c:/abc
- *@parampublicKey
- *公鑰
- */
- public void decryptUnzip(Stringsrcfile,Stringdestfile,
- Stringkeyfile) throws Exception{
- //先對文件解密
- Filetemp= new File(UUID.randomUUID().toString()+ ".zip" );
- temp.deleteOnExit();
- decrypt(srcfile,temp.getAbsolutePath(), this .getKey(keyfile));
- //解壓縮
- unZip(destfile,temp.getAbsolutePath());
- temp.delete();
- }
- public static void main(Stringargs[]) throws Exception{
- long a=System.currentTimeMillis();
- new ZipEncrypt().encryptZip( "e:/com" , "e:/comXXX/page.zip" , "e:/comXXX/public.key" );
- System.out.println(System.currentTimeMillis()-a);
- a=System.currentTimeMillis();
- new ZipEncrypt().decryptUnzip( "e:/comXXX/page.zip" , "e:/comxxx" , "e:/comXXX/public.key" );
- System.out.println(System.currentTimeMillis()-a);
- }
- }
通過路徑找到Key:getKey
解密:decryptUnzip
轉自【http://topic.csdn.net/u/20081215/09/6ad763bd-a158-488a-8e5e-b34cec2a0424.html及http://www.blogjava.net/zhaochengming/archive/2007/09/03/142396.html】
<!--新Google 468*60橫幅廣告開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x60, 創(chuàng)建于 08-8-6 */ google_ad_slot = "7368701459"; google_ad_width = 468; google_ad_height = 60; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468*60橫幅廣告結束-->
<!--新Google 468x15 橫鏈接單元開始--><script type="text/javascript"><!-- google_ad_client = "pub-7343546549496470"; /* 468x15 橫鏈接單元 */ google_ad_slot = "5785741422"; google_ad_width = 468; google_ad_height = 15; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script><!--新Google 468x15 橫鏈接單元結束-->
<!-- Google Reader shared發(fā)布代碼開始 --><script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script><script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&callback=GRC_p(%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D)%3Bnew%20GRC"></script><!-- Google Reader shared發(fā)布代碼結束 -->
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
