<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    夢幻之旅

    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 閱讀(202) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 亚洲高清在线观看| 亚洲AV伊人久久青青草原| 亚洲AV无码国产丝袜在线观看| 亚洲成a人无码亚洲成www牛牛 | youjizz亚洲| 国产成在线观看免费视频| 亚洲午夜久久久久久尤物| 99久久免费国产精品特黄| 亚洲性无码AV中文字幕| 拔擦拔擦8x华人免费久久| 亚洲日韩在线中文字幕综合| 国产亚洲精品免费| 一级成人生活片免费看| 亚洲第一AV网站| 亚洲成人在线免费观看| 一本色道久久综合亚洲精品蜜桃冫| 一区二区无码免费视频网站| 午夜亚洲WWW湿好爽| 亚洲欧洲精品成人久久奇米网| 国产高清视频免费在线观看 | 亚洲 暴爽 AV人人爽日日碰| 国产免费黄色大片| www成人免费视频| 亚洲av无码潮喷在线观看| 亚洲一级毛片免费看| 亚洲kkk4444在线观看| 亚洲成a人片在线播放| 日韩免费高清播放器| 亚洲精品动漫在线| 青青草国产免费久久久下载| a级毛片免费观看网站| 久久久久亚洲av无码专区喷水 | 夜夜嘿视频免费看| 久久久久国色AV免费观看| 亚洲日韩区在线电影| 四虎成人免费网站在线| 国产精品福利在线观看免费不卡| 亚洲美女大bbbbbbbbb| 国产精品极品美女免费观看| 最新久久免费视频| 亚洲私人无码综合久久网|