DESede/ECB/PKCS5Padding 加密

public static void main(String[] args) throws Exception {
        //密钥,24位 下面需要字节数组,记住utf-8
        byte[] keybyte = "abcdefghizklmnopqrstuvwx".getBytes("utf-8");
        //用到javax下的SecreKey,传入密钥和加密方式  
        SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
        //需要字节数组类型    utf-8
        byte[] input = "今天是个好日子".getBytes("UTF-8");
        //emmm....获得一个Cipher实例
        Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        //加载这个加密算法 
        c1.init(Cipher.ENCRYPT_MODE, deskey);
        //执行,获得字节数组(密文)
        byte[] str1 = c1.doFinal(input);
        String str =Base64.encode(str1);
        System.out.println("加密后base64:"+str);
        //下面是解密 ------------------------------------------------------                
        SecretKey deskey2 = new SecretKeySpec(keybyte, "DESede");
        Cipher c2 = Cipher.getInstance("DESede");
        c2.init(Cipher.DECRYPT_MODE, deskey2);//加载解密算法
        byte[] str2 = c2.doFinal(str1);//获得解密后的数据
        String string = new String(str2, "utf-8");
        System.out.println(bytes2Hex(str2) + "   16进制解密后:" + string);

 

你可能感兴趣的:(java编程)