如下代码:
   public byte[] getEncrypt(byte[] key,byte[] session){
      // byte[] message=asBin(session);
      // System.out.println("session="+message.length);
       byte[] encrypted=null;
       try{   
          SecretKeySpec skeySpec = new SecretKeySpec(key,"AES");
  
          Cipher cipher = Cipher.getInstance("AES");
          
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
          
          encrypted = cipher.doFinal(session);       
         
         }catch (Exception e) {
          System.out.println("AES"+ "获取加密串出错," + e.getMessage());
      }    
         //System.out.println(encrypted.length);
        return encrypted;       
     }
   public static void main(String[] args)
    {
      byte[] key={(byte)0xCD,(byte)0xC5,...............};//key是一个128位的密钥。       byte[] session={(byte)0x6C,(byte)0xD4,.............};//session是一个十六个字节的明文。       byte[] testing=getEncrypted(key, session);//得到的密文testing竟然是32个字节。     }
     小弟刚刚接触AES,对其不熟悉,请各位大侠帮忙看一下代码有什么问题,该怎样改正。

解决方案 »

  1.   

    Using AES with Java Technology
    http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html
      

  2.   

    正好我的博客里面发表了AES算法的代码,给你转过来,你自己比照看看import java.io.*;
    import java.security.*;import javax.crypto.*;/**
     * AES算法生成密鑰和對文件加解密的實現。
     * @author Richard
     * AES--DES算法的后續版本,由于DES算法可以通過窮舉法破譯,因此不推薦使用。
     */
    public class AESImpl {
     //產生AES密鑰
     public void createKey() {
      try{
       KeyGenerator keygen = KeyGenerator.getInstance("AES");
       SecureRandom random = new SecureRandom();
       keygen.init(random);
       SecretKey key = keygen.generateKey();
       ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("aesKey.txt"));
       out.writeObject(key);
       out.close();
      }catch (NoSuchAlgorithmException e) {
       e.printStackTrace();
      }catch (IOException e) {
       e.printStackTrace();
      }
     }
     
     public void run(String code){
      int mode = Cipher.ENCRYPT_MODE;
      String inputFileName = "encode.txt";//要加密的文件名
      String outputFileName = "aesDecode.txt";//加密后的文件名
      if("DECODE".equals(code)){
       mode = Cipher.DECRYPT_MODE;
       inputFileName = "aesDecode.txt";//要解密的文件名
       outputFileName = "encode.txt";//解密后的文件名
      }
      try{
       //讀入AES密鑰文件
       ObjectInputStream keyin = new ObjectInputStream(new FileInputStream("aesKey.txt"));
       Key key = (Key)keyin.readObject();
       keyin.close();
       
       InputStream in = new FileInputStream(inputFileName);
       OutputStream out = new FileOutputStream(outputFileName);
       Cipher cipher = Cipher.getInstance("AES");
       cipher.init(mode, key);
       
       //加解密
       crypt(in,out,cipher);
       in.close();
       out.close();
      }catch (Exception e) {
       e.printStackTrace();
      }
     } public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, ShortBufferException, GeneralSecurityException {
      int blockSize = cipher.getBlockSize();
      int outputSize = cipher.getOutputSize(blockSize);
      byte[] inBytes = new byte[blockSize];
      byte[] outBytes = new byte[outputSize];
      
      int inLength = 0;
      boolean more = true;
      
      while(more){
       inLength = in.read(inBytes);
       if(inLength == blockSize){
        int outLength = cipher.update(inBytes, 0,blockSize,outBytes);
        out.write(outBytes,0,outLength);
       }
       else more = false;
      }
      if(inLength > 0){
       outBytes = cipher.doFinal(inBytes,0,inLength);
      }
      else{
       outBytes = cipher.doFinal();
      }
      out.write(outBytes);
     }
     
     public static void main(String[] args){
      AESImpl _aes = new AESImpl();
      //_aes.createKey();
      _aes.run("DECODE");
     }}
      

  3.   

    原文地址:http://blog.csdn.net/richard_2010/archive/2008/09/24/2974592.aspx
      

  4.   

    32个字节有什么不对?
    先看一下AES算法的介绍
      

  5.   

    import java.io.*;
    import java.security.*;import javax.crypto.*;/**
     * AES算法生成密鑰和對文件加解密的實現。
     * @author Richard
     * AES--DES算法的后續版本,由于DES算法可以通過窮舉法破譯,因此不推薦使用。
     */
    public class AESImpl {
     //產生AES密鑰
     public void createKey() {
      try{
       KeyGenerator keygen = KeyGenerator.getInstance("AES");
       SecureRandom random = new SecureRandom();
       keygen.init(random);
       SecretKey key = keygen.generateKey();
       ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("aesKey.txt"));
       out.writeObject(key);
       out.close();
      }catch (NoSuchAlgorithmException e) {
       e.printStackTrace();
      }catch (IOException e) {
       e.printStackTrace();
      }
     }
     
     public void run(String code){
      int mode = Cipher.ENCRYPT_MODE;
      String inputFileName = "encode.txt";//要加密的文件名
      String outputFileName = "aesDecode.txt";//加密后的文件名
      if("DECODE".equals(code)){
       mode = Cipher.DECRYPT_MODE;
       inputFileName = "aesDecode.txt";//要解密的文件名
       outputFileName = "encode.txt";//解密后的文件名
      }
      try{
       //讀入AES密鑰文件
       ObjectInputStream keyin = new ObjectInputStream(new FileInputStream("aesKey.txt"));
       Key key = (Key)keyin.readObject();
       keyin.close();
       
       InputStream in = new FileInputStream(inputFileName);
       OutputStream out = new FileOutputStream(outputFileName);
       Cipher cipher = Cipher.getInstance("AES");
       cipher.init(mode, key);
       
       //加解密
       crypt(in,out,cipher);
       in.close();
       out.close();
      }catch (Exception e) {
       e.printStackTrace();
      }
     } public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, ShortBufferException, GeneralSecurityException {
      int blockSize = cipher.getBlockSize();
      int outputSize = cipher.getOutputSize(blockSize);
      byte[] inBytes = new byte[blockSize];
      byte[] outBytes = new byte[outputSize];
      
      int inLength = 0;
      boolean more = true;
      
      while(more){
       inLength = in.read(inBytes);
       if(inLength == blockSize){
        int outLength = cipher.update(inBytes, 0,blockSize,outBytes);
        out.write(outBytes,0,outLength);
       }
       else more = false;
      }
      if(inLength > 0){
       outBytes = cipher.doFinal(inBytes,0,inLength);
      }
      else{
       outBytes = cipher.doFinal();
      }
      out.write(outBytes);
     }
     
     public static void main(String[] args){
      AESImpl _aes = new AESImpl();
      //_aes.createKey();
      _aes.run("DECODE");
     }}
     在学习中,......................
      

  6.   

    security 包中有这种加密方法...截取一段就好了呀