Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3423564
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: Java

2010-11-11 14:17:43

  1. /** 
  2.      * 加密文件infilename,输出加密后的文件outfilename 返回AES加密密钥 
  3.      */  
  4.     public static byte[] AesEnCrypt(String infilename, String outfilename)  
  5.             throws Exception {  
  6.         KeyGenerator keyGen = KeyGenerator.getInstance("AES");  
  7.         keyGen.init(128);  
  8.         SecretKey key = keyGen.generateKey();  
  9.   
  10.         Cipher cipher = Cipher.getInstance("AES");  
  11.   
  12.         InputStream in = new FileInputStream(infilename);  
  13.         DataOutputStream out = new DataOutputStream(new FileOutputStream(  
  14.                 outfilename));  
  15.   
  16.         cipher.init(Cipher.ENCRYPT_MODE, key);  
  17.         crypt(in, out, cipher);  
  18.   
  19.         in.close();  
  20.         out.close();  
  21.         return key.getEncoded();  
  22.     }  
  23.   
  24.     /** 
  25.      * 用密钥k解密文件infilename,输出明文文件outfilename 
  26.      *  
  27.      * @param infilename 
  28.      * @param outfilename 
  29.      * @param k 
  30.      */  
  31.     public static void AesDeCrypt(String infilename, String outfilename,  
  32.             byte[] k) {  
  33.   
  34.         try {  
  35.   
  36.             Cipher cipher = Cipher.getInstance("AES");  
  37.   
  38.             SecretKey key = new javax.crypto.spec.SecretKeySpec(k, "AES");  
  39.             OutputStream out = new FileOutputStream(outfilename);  
  40.             DataInputStream in = new DataInputStream(new FileInputStream(  
  41.                     infilename));  
  42.   
  43.             cipher.init(Cipher.DECRYPT_MODE, key);  
  44.             crypt(in, out, cipher);  
  45.   
  46.             in.close();  
  47.             out.close();  
  48.   
  49.         } catch (GeneralSecurityException exception) {  
  50.             exception.printStackTrace();  
  51.         } catch (IOException exception) {  
  52.             exception.printStackTrace();  
  53.         }  
  54.   
  55.     }  
  56.   
  57.     /** 
  58.      * 自己定义的加 密函数 
  59.      *  
  60.      * @param in 
  61.      * @param out 
  62.      * @param cipher 
  63.      * @throws IOException 
  64.      * @throws GeneralSecurityException 
  65.      */  
  66.     public static void crypt(InputStream in, OutputStream out, Cipher cipher)  
  67.             throws IOException, GeneralSecurityException {  
  68.         int blockSize = cipher.getBlockSize();  
  69.         int outputSize = cipher.getOutputSize(blockSize);  
  70.         byte[] inBytes = new byte[blockSize];  
  71.         byte[] outBytes = new byte[outputSize];  
  72.         int inLength = 0;  
  73.         boolean more = true;  
  74.         while (more) {  
  75.             inLength = in.read(inBytes);  
  76.             if (inLength == blockSize) {  
  77.                 int outLength = cipher.update(inBytes, 0, blockSize, outBytes);  
  78.                 out.write(outBytes, 0, outLength);  
  79.             } else {  
  80.                 more = false;  
  81.             }  
  82.         }  
  83.         if (inLength > 0)  
  84.             outBytes = cipher.doFinal(inBytes, 0, inLength);  
  85.         else  
  86.             outBytes = cipher.doFinal();  
  87.         out.write(outBytes);  
  88.     }  
  89.       
  90.       
  91.       
  92.     public static String crypt(String in,Cipher cipher)  
  93.     throws IOException, GeneralSecurityException {  
  94.         StringBuffer out = new StringBuffer();  
  95.         int blockSize = cipher.getBlockSize();  
  96.         int outputSize = cipher.getOutputSize(blockSize);  
  97.         byte[] inBytes = new byte[blockSize];  
  98.         byte[] outBytes = new byte[outputSize];  
  99.         int inLength = 0;  
  100.         boolean more = true;  
  101.         while (more) {  
  102.             for(int i = 0;i
  103.             {  
  104.                 System.out.println(in.getBytes().length*8);  
  105.                 if(in.getBytes().length>=i*blockSize)  
  106.                 {  
  107.                     System.arraycopy(in, 0*blockSize, inBytes, 0, blockSize);  
  108.                     cipher.update(inBytes, 0, blockSize, outBytes);  
  109.                     out.append(outBytes);  
  110.                 }  
  111.                 else  
  112.                 {  
  113.                     inLength = in.getBytes().length - i*blockSize;  
  114.                     more = false;  
  115.                 }  
  116.             }  
  117.         }  
  118.         if (inLength > 0)  
  119.             outBytes = cipher.doFinal(inBytes, 0, inLength);  
  120.         else  
  121.             outBytes = cipher.doFinal();  
  122.         out.append(outBytes);  
  123.         return out.toString();  
  124. }
阅读(1096) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~