suzixu

BlogJava 首页 新随笔 联系 聚合 管理
  4 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks

AES加密,代码如下:

import java.security.InvalidKeyException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;

/**
 * <p>标题: AESUtil</p>
 * <p>描述: AES加密工具类 实现对字符串(String)或文件的加密与解密 支持128、192、256位密匙</p>
 * <p>附: 若要使用192、256位密匙 需要到Sun下载jce_policy-6.zip 并解压安装</p>
 * 
@author 见习和尚
 * 
@version 1.0, 2010/09/10
 
*/

public class AESTool {
 
//密匙
 private byte[] key;
 
 
//加密的密匙位数(默认256位AES加密)
 private int keySize = 256;

 
//待加密源文件
 private String srcFile;

 
//加密后的文件
 private String desFile;

 
//解密后的文件
 private String decodeFile;

 
//调教后性价比很好的缓冲区大小(最好不要改动)
 private int blockSize = 61440;
 
 
//有效密匙位数集合
 private static List<Integer> KEYSIZELIST = new ArrayList<Integer>();
 
 
static{
  
//初始化有效密匙位数
  KEYSIZELIST.add(128);
  KEYSIZELIST.add(
192);
  KEYSIZELIST.add(
256);
 }

 
 
/**
  * 构造方法
  
*/

 
public AESTool(String srcFile,String desFile,String decodeFile,byte[] key){
  
this.srcFile = srcFile;
  
this.desFile = desFile;
  
this.decodeFile = decodeFile;
  
this.key = key;
 }

 
 
/**
  * 类入口方法,包含文件及字符串加解密调用示例
  * 
@param args
  * 
@return void
  
*/

 
public static void main(String[] args) throws Exception {
  System.out.println(
"*******************************************");
  System.out.println(
"***************AES加密工具*******************");
  System.out.println(
"*********操作指令:1加密  2解密  3退出**********");
  System.out.println(
"*******************************************");
  Scanner sc 
= new Scanner(System.in);
  String commond 
= null;
  
  
do{
   System.out.println(
"请输入操作指令:");
   
if(sc.hasNext()){
    commond 
= sc.nextLine();
   }

  }
while(!"1".equals(commond) && !"2".equals(commond) && !"3".equals(commond));
  
  
if("1".equals(commond)){
   System.out.println(
"请输入原始文件路径(如:c:/a.rar)");
   
   String srcFile 
= null;
   
if(sc.hasNext()){
    srcFile 
= sc.nextLine();
   }

   
   System.out.println(
"请输入加密文件存放路径(如:c:/b.uumap");
   String desFile 
= null;
   
if(sc.hasNext()){
    desFile 
= sc.nextLine();
   }

   
   System.out.println(
"请输入32位密匙(可由字母、数字、英文特殊字符组成):");
   String key 
= null;
   
if(sc.hasNext()){
    key 
= sc.nextLine();
   }

   
   sc.close();
   
   AESTool aesUtil 
= new AESTool(srcFile,desFile,null,key.getBytes());

   aesUtil.encode();
//文件加密
  }
else if("2".equals(commond)){
   System.out.println(
"请输入加密文件存放路径(如:c:/b.uumap");
   
   String desFile 
= null;
   
if(sc.hasNext()){
    desFile 
= sc.nextLine();
   }

   
   System.out.println(
"请输入解密后文件存放路径(如:c:/c.rar");
   String decodeFile 
= null;
   
if(sc.hasNext()){
    decodeFile 
= sc.nextLine();
   }

   
   System.out.println(
"请输入32位密匙(可由字母、数字、英文特殊字符组成):");
   String key 
= null;
   
if(sc.hasNext()){
    key 
= sc.nextLine();
   }

   
   sc.close();
   
   AESTool aesUtil 
= new AESTool(null,desFile,decodeFile,key.getBytes());
   
   
//文件加密解密示例
   aesUtil.decode();//文件解密*/
  }
else if("3".equals(commond)){
   System.out.println(
"******************退出**********************");
   System.exit(
0);
  }

 }

 
 
/**
  * 文件加密方法
  * 
@return void
  * 
@throws Exception
  
*/

 
public void encode()throws Exception{
  
  
//校验并创建文件目录
  validateFile(this.desFile);
  
  
//获取随机密匙
  byte[] raw = this.key;

  
//初始化SecretKeySpec对象
  SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

  
//初始化Cipher对象
  Cipher cipher = Cipher.getInstance("AES");

  
//用指定密匙对象初始化加密Cipher对象
  cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

  
//加密文件
  encodeFile(cipher);
 }

 
 
/**
  * 文件解密方法
  * 
@return void
  * 
@throws Exception
  
*/

 
public void decode()throws Exception{
  
//校验并创建文件目录
  validateFile(this.decodeFile);
  
  
//初始化SecretKeySpec对象
  SecretKeySpec skeySpec = new SecretKeySpec(this.key, "AES");

  
//初始化Cipher对象
  Cipher cipher = Cipher.getInstance("AES");

  
//用指定密匙对象初始化加密Cipher对象
  cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

  
//解密文件
  decodeFile(cipher,skeySpec);
 }

 
 
/**
  * 字符串加密方法
  * 
@return void
  * 
@throws Exception
  
*/

 
public byte[] encodeStr(String str,int keySize)throws Exception{
  
  
if(!validate(str,keySize)){
   System.out.println(
"字符串加密参数校验失败");
   
return null;
  }

  
  
//获取KeyGenerator对象
  KeyGenerator kgen = KeyGenerator.getInstance("AES");
  
  
//设置加密密匙位数,目前支持128、192、256位
  kgen.init(keySize);

  
//获取密匙对象
  SecretKey skey = kgen.generateKey();
  
  
//获取随机密匙
  byte[] raw = skey.getEncoded();

  
//初始化SecretKeySpec对象
  SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

  
//初始化Cipher对象
  Cipher cipher = Cipher.getInstance("AES");

  
//用指定密匙对象初始化加密Cipher对象
  cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

  
//加密字符串
  return cipher.doFinal(str.getBytes("utf-8"));
 }

 
 
/**
  * 字符串解密方法
  * 
@return void
  * 
@throws Exception
  
*/

 
public String decodeStr(byte[] date)throws Exception{

  
if(null == date || date.length%16 !=0 ){
   System.out.println(
"字符串解密参数校验失败");
   
return null;
  }

  
  
//初始化SecretKeySpec对象
  SecretKeySpec skeySpec = new SecretKeySpec(this.key, "AES");

  
//初始化Cipher对象
  Cipher cipher = Cipher.getInstance("AES");

  
//用指定密匙对象初始化加密Cipher对象
  cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  
  
//解密字符串
  byte[] decData = cipher.doFinal(date);
  
  
return new String(decData,"utf-8");
 }

 
 
/**
  * 文件加密具体逻辑实现方法
  * 
  * 
@param cipher
  * 
@return void
  
*/

 
public void encodeFile(Cipher cipher) {
  
  System.out.println(
"**********启动加密************");
  
  
try {
   
long readfilelen = 0;
         BufferedRandomAccessFile brafReadFile;
         BufferedRandomAccessFile brafWriteFile;
         brafReadFile 
= new BufferedRandomAccessFile(this.srcFile);
         readfilelen 
= brafReadFile.initfilelen;
         brafWriteFile 
= new BufferedRandomAccessFile(this.desFile, "rw"10);

         
byte buf[] = new byte[this.blockSize];
         
int readcount;

         
long start = System.currentTimeMillis();
         
double count = 0;
         
while((readcount = brafReadFile.read(buf)) != -1{
          count
+=readcount;
          
byte[] encData = cipher.doFinal(buf);
             brafWriteFile.write(encData);
             System.out.println(
"加密进度--->"+(int)((count/readfilelen)*100)+"%");
         }


         System.out.println(
"BufferedRandomAccessFile Copy & Write File: "
                            
+ brafReadFile.filename
                            
+ "    FileSize: "
                            
+ java.lang.Integer.toString((int)readfilelen >> 1024)
                            
+ " (KB)    "
                            
+ "Spend: "
                            
+(double)(System.currentTimeMillis()-start) / 1000
                            
+ "(s)");
   
         brafWriteFile.close();
         brafReadFile.close();
   System.out.println(
"**********加密完成************");
  }
 catch (FileNotFoundException e) {
   e.printStackTrace();
  }
catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  }
 catch (BadPaddingException e) {
   e.printStackTrace();
  }
catch (IOException e) {
   e.printStackTrace();
  }

 }


 
/**
  * 文件解密具体逻辑实现方法
  * 
  * 
@param cipher
  * 
@param skeySpec
  * 
@return void
  
*/

 
public void decodeFile(Cipher cipher,SecretKeySpec skeySpec){
  
  
try {
   cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  }
 catch (InvalidKeyException e) {
   e.printStackTrace();
  }

  
  System.out.println(
"**********启动解密************");
  
  
try {
   
long readfilelen = 0;
         BufferedRandomAccessFile brafReadFile;
         BufferedRandomAccessFile brafWriteFile;
         brafReadFile 
= new BufferedRandomAccessFile(this.desFile);
         readfilelen 
= brafReadFile.initfilelen;
         brafWriteFile 
= new BufferedRandomAccessFile(this.decodeFile, "rw"10);

         
byte buf[] = new byte[this.blockSize+16];
         
int readcount;

         
long start = System.currentTimeMillis();
         
double count = 0;
         
while((readcount = brafReadFile.read(buf)) != -1{
          count
+=readcount;
          
byte[] decData = cipher.doFinal(buf);
             brafWriteFile.write(decData);
             System.out.println(
"解密进度--->"+(int)((count/readfilelen)*100)+"%");
             
         }


         System.out.println(
"BufferedRandomAccessFile Copy & Write File: "
                            
+ brafReadFile.filename
                            
+ "    FileSize: "
                            
+ java.lang.Integer.toString((int)readfilelen >> 1024)
                            
+ " (KB)    "
                            
+ "Spend: "
                            
+(double)(System.currentTimeMillis()-start) / 1000
                            
+ "(s)");
   
         brafWriteFile.close();
         brafReadFile.close();
   System.out.println(
"**********解密完成************");
  }
 catch (FileNotFoundException e) {
   e.printStackTrace();
  }
catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  }
 catch (BadPaddingException e) {
   e.printStackTrace();
  }
catch (IOException e) {
   e.printStackTrace();
  }

 }

 
 
/**
  * 参数校验
  * 
  * 
@return boolean
  
*/

 
private static boolean validate(String str, int keySize) {
  
  
if(null==str){
   
return false;
  }

  
  
return KEYSIZELIST.contains(keySize);
 }

 
 
/**
  * 文件参数校验,若不存在改文件目录,则创建目录;若该文件存在则删除此文件
  * 
  * 
@return boolean
  
*/

 
private static boolean validateFile(String filePath) {
  
  
if(null==filePath || "".equals(filePath)){
   System.out.println(
"无效的文件路径");
   
return false;
  }

  
  String dir 
= filePath.substring(0, filePath.lastIndexOf("/")+1);
  
  File dirFile 
= new File(dir);
  
  
if(!dirFile.exists()){
   dirFile.mkdirs();
  }

  
  File file 
= new File(filePath);
  
if(file.exists()){
   file.delete();
  }

  
  
return true;
 }

}


希望各位给点意见~~~
posted on 2010-09-16 19:36 见习和尚 阅读(6495) 评论(5)  编辑  收藏

评论

# re: 闲来无事,写个AES(256位)加解密程序 2010-09-21 15:14 the_fire
我很想给个意见,前天刚学了DES加密原理。

不过老兄的代码太长了。

看晕了。呵呵  回复  更多评论
  

# re: 闲来无事,写个AES(256位)加解密程序 2012-04-06 00:11 en
这个是使用系统的 AES .... 一般没有 256位密钥 的实现可用!!!  回复  更多评论
  

# re: 闲来无事,写个AES(256位)加解密程序[未登录] 2012-04-06 09:11 见习和尚
@en
为什么要自己实现一个256位AES加密算法类?  回复  更多评论
  

# re: 闲来无事,写个AES(256位)加解密程序 2012-04-06 20:06 en
@见习和尚
当程序跑在别人机器时候.没办法叫别人都有 AES256运行的权力.   回复  更多评论
  

# re: 闲来无事,写个AES(256位)加解密程序 2014-06-23 16:52 莫凡
请问BufferRandomAccessFile那个类要怎么改啊@en
  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: