码迷,mamicode.com
首页 > 编程语言 > 详细

java加密算法-AES

时间:2017-10-27 13:38:05      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:color   ret   aes   public   secure   actor   spec   try   rand   

public class AESUtil {
    public static void main(String[] args){
        jdkAES("12345623423432423S");
    }
    public static void jdkAES(String str){
        try {
        SecureRandom random = new SecureRandom();
        byte[] salt = random.generateSeed(8);
        PBEKeySpec pkey=new PBEKeySpec(str.toCharArray());
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
            Key key = factory.generateSecret(pkey);
            Key key2 = factory.generateSecret(pkey);
        
            System.out.println("KEY:"+key.serialVersionUID);
            System.out.println("KEY2:"+key2.serialVersionUID);
        
        PBEParameterSpec pbets = new PBEParameterSpec(salt,100);
        Cipher c = Cipher.getInstance("PBEWITHMD5andDES");
        c.init(Cipher.ENCRYPT_MODE, key,pbets);
        byte[] result = c.doFinal(str.getBytes());
        System.out.println(Base64.encodeBase64String(result));
        
        
        c.init(Cipher.DECRYPT_MODE, key,pbets);
        result = c.doFinal(result);
        System.out.println(new String(result));
        
        
        
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}

 

java加密算法-AES

标签:color   ret   aes   public   secure   actor   spec   try   rand   

原文地址:http://www.cnblogs.com/syscn/p/7742262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!