2023年java 加密解密實用

字號:

    在日常學習、工作或生活中,大家總少不了接觸作文或者范文吧,通過文章可以把我們那些零零散散的思想,聚集在一塊。寫范文的時候需要注意什么呢?有哪些格式需要注意呢?下面是小編幫大家整理的優(yōu)質(zhì)范文,僅供參考,大家一起來看看吧。
    java 加密解密篇一
     java很多時候要對秘要進行持久化加密,此時的加密采用md5。采用對稱加密的時候就采用des方法了,那么java證書的加密與解密代碼是什么呢?下面跟yjbys小編一起來學習一下吧!
     加密:t(password)
     解密:t(password)
    
     [java]
     package ;
     import ption;
     import ;
     import erator;
     import key;
     import 64;
     public class cryptutils {
     private static string algorithm = "des";
     private static byte[] default_key=new byte[] {-53, 122, -42, -88, -110, -123, -60, -74};
     private static string value_encoding="utf-8";
     /**
     * 生成密鑰
     *
     * @return byte[] 返回生成的密鑰
     * @throws exception
     * 扔出異常.
     */
     public static byte[] getsecretkey() throws exception {
     keygenerator keygen = tance(algorithm);
     secretkey deskey = tekey();
     // if (debug ) n ("生成密鑰:"+byte2hex (oded
     // ()));
     return oded();
     }
     /**
     * 將指定的數(shù)據(jù)根據(jù)提供的密鑰進行加密
     *
     * @param input
     * 需要加密的數(shù)據(jù)
     * @param key
     * 密鑰
     * @return byte[] 加密后的數(shù)據(jù)
     * @throws exception
     */
     public static byte[] encryptdata(byte[] input, byte[] key) throws exception {
     secretkey deskey = new keyspec(key, algorithm);
     // if (debug )
     // {
     // n ("加密前的二進串:"+byte2hex (input ));
     // n ("加密前的字符串:"+new string (input ));
     //
     // }
     cipher c1 = tance(algorithm);
     (t_mode, deskey);
     byte[] cipherbyte = l(input);
     // if (debug ) n ("加密后的二進串:"+byte2hex (cipherbyte ));
     return cipherbyte;
     }
     public static byte[] encryptdata(byte[] input) throws exception {
     return encryptdata(input, default_key);
     }
     /**
     * 將給定的已加密的數(shù)據(jù)通過指定的密鑰進行解密
     *
     * @param input
     * 待解密的數(shù)據(jù)
     * @param key
     * 密鑰
     * @return byte[] 解密后的數(shù)據(jù)
     * @throws exception
     */
     public static byte[] decryptdata(byte[] input, byte[] key) throws exception {
     secretkey deskey = new keyspec(key, algorithm);
     // if (debug ) n ("解密前的信息:"+byte2hex (input ));
     cipher c1 = tance(algorithm);
     (t_mode, deskey);
     byte[] clearbyte = l(input);
     // if (debug )
     // {
     // n ("解密后的二進串:"+byte2hex (clearbyte ));
     // n ("解密后的字符串:"+(new string (clearbyte )));
     //
     // }
     return clearbyte;
     }
     public static byte[] decryptdata(byte[] input) throws exception {
     return decryptdata(input, default_key);
     }
     /**
     * 字節(jié)碼轉(zhuǎn)換成16進制字符串
     *
     * @param byte[] b 輸入要轉(zhuǎn)換的字節(jié)碼
     * @return string 返回轉(zhuǎn)換后的16進制字符串
     */
     public static string byte2hex(byte[] bytes) {
     stringbuilder hs = new stringbuilder();
     for(byte b : bytes)
     (("%1$02x", b));
     return ng();
     }
     public static byte[] hex2byte(string content) {
     int l=()>>1;
     byte[] result=new byte[l];
     for(int i=0;i
     int j=i<<1;
     string s=ing(j, j+2);
     result[i]=f(s, 16).bytevalue();
     }
     return result;
     }
     /**
     * 將字節(jié)數(shù)組轉(zhuǎn)換為base64編碼字符串
     * @param buffer
     * @return
     */
     public static string bytestobase64(byte[] buffer) {