java加密算法-AES

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();
        }
    }
    
}

 

posted @ 2017-10-27 11:18  冬天不眠  阅读(222)  评论(0编辑  收藏  举报