梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks

 

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil
{
    
public static String encrypt(String content, String password)
    
{
        String encryptResult 
= null;
        
try
        
{
            Cipher cipher 
= AESUtil.initCipher(Cipher.ENCRYPT_MODE, password);
            
byte[] byteContent = content.getBytes("UTF8");
            
byte[] result = cipher.doFinal(byteContent);
            encryptResult 
= parseByte2HexStr(result);
        }

        
catch (Exception e)
        
{
            e.printStackTrace();
        }

        
return encryptResult;
    }

    
    
public static String decrypt(String content, String password)
    
{
        String decryptResult 
= null;
        
try
        
{
            Cipher cipher 
= AESUtil.initCipher(Cipher.DECRYPT_MODE, password);
            
byte[] result = cipher.doFinal(parseHexStr2Byte(content));
            
return decryptResult = new String(result, "UTF8");
        }

        
catch (Exception ex)
        
{
            ex.printStackTrace();
        }

        
return decryptResult;
    }

    
    
private static String parseByte2HexStr(byte buf[])
    
{
        StringBuffer sb 
= new StringBuffer();
        
for (int i = 0; i < buf.length; i++)
        
{
            String hex 
= Integer.toHexString(buf[i] & 0xFF);
            
if (hex.length() == 1)
            
{
                hex 
= '0' + hex;
            }

            sb.append(hex.toUpperCase());
        }

        
return sb.toString();
    }

    
    
private static byte[] parseHexStr2Byte(String hexStr)
    
{
        
if (hexStr.length() < 1)
            
return null;
        
byte[] result = new byte[hexStr.length() / 2];
        
for (int i = 0; i < hexStr.length() / 2; i++)
        
{
            
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                    
16);
            result[i] 
= (byte) (high * 16 + low);
        }

        
return result;
    }

    
    
private static Cipher initCipher(int model, String password)
            
throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException
    
{
        KeyGenerator kgen 
= KeyGenerator.getInstance("AES");
        kgen.init(
128new SecureRandom(password.getBytes()));
        SecretKey secretKey 
= kgen.generateKey();
        
byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key 
= new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher 
= Cipher.getInstance("AES");
        cipher.init(model, key);
        
return cipher;
    }

    
    
public static void main(String[] args)
    
{
        String content 
= "123456惠万鹏!@#$%^&*()_+|";
        String password 
= "";
        
// 加密
        System.out.println("加密前:" + content);
        String result 
= AESUtil.encrypt(content, password);
        System.out.println(
"加密后:" + result);
        
// 解密
        System.out.println("解密后:" + AESUtil.decrypt(result, password));
    }

}

posted on 2010-05-28 15:37 HUIKK 阅读(192) 评论(0)  编辑  收藏 所属分类: Java

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


网站导航: