<!-- Feedsky FEED发布代码开始 -->
如果您喜欢这些文章,欢迎点击此处订阅本Blog
<!-- FEED自动发现标记开始 --> <link title="RSS 2.0" type="application/rss+xml" href="http://feed.feedsky.com/softwave" rel="alternate"> <!-- FEED自动发现标记结束 -->
<!--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压缩加密/解压缩解密
AES压缩加密/解压缩解密,网上一般用base64来对byte[]编码,其实不需要,指定AES/CBC/PKCS5Padding来指定加密解密时候位数不对的情况下,用pkcs5padding来附加位数,不过这个时候读解密的文件的时候,要多读16位的验证位就不会报异常
加密:encryptZip
通过路径找到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, 创建于 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发布代码开始 --><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发布代码结束 -->
<!--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();
- //创建压缩的子目录
- 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();
- //创建压缩的子目录
- 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, 创建于 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发布代码开始 --><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发布代码结束 -->