av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java 實(shí)現(xiàn)DES 加密解密的示例

瀏覽:2日期:2022-08-20 09:55:07

package com.cn.peitest;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvParameterSpec;/** * @功能說(shuō)明: <BR> * @創(chuàng)建日期:2016年9月21日<BR> * @變更記錄:<BR> * 1、2016年9月21日 LeoLu 更新 */public class DESUtil { /**用于建立大寫(xiě)的十六進(jìn)制字符的輸出*/ private static final char[] DIGITS_UPPER = { ’0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’A’, ’B’, ’C’, ’D’, ’E’, ’F’ }; /**DES向量*/ private static final byte[] iv = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef}; //private static final Logger log = LoggerFactory.getLogger(DESUtil.class); /** * @函數(shù)名稱:encodeHex<br> * @創(chuàng)建日期:2016年9月22日<br> * @功能說(shuō)明: 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字節(jié)數(shù)組 <br> * @參數(shù)說(shuō)明:data byte[] 字節(jié)數(shù)組<br> * @參數(shù)說(shuō)明:toDigits char[] 向量<br> * @返回說(shuō)明:十六進(jìn)制char[] */ private static char[] encodeHex(byte[] data, char[] toDigits) { int l = data.length; char[] out = new char[l << 1]; for (int i = 0, j = 0; i < l; i++) { out[j++] = toDigits[(0xF0 & data[i]) >>> 4]; out[j++] = toDigits[0x0F & data[i]]; } return out; } /** * @函數(shù)名稱:encodeHexStr<br> * @創(chuàng)建日期:2016年9月22日<br> * @功能說(shuō)明:將16進(jìn)制字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串 <br> * @參數(shù)說(shuō)明:data byte[] 16進(jìn)制字節(jié)數(shù)組 <br> * @參數(shù)說(shuō)明:toDigits char[] 向量 <br> * @返回說(shuō)明:String 返回16進(jìn)制字符串 */ private static String encodeHexStr(byte[] data, char[] toDigits) { return new String(encodeHex(data, toDigits)); } /** * @函數(shù)名稱:hexStringToString<br> * @創(chuàng)建日期:2016年9月21日<br> * @功能說(shuō)明:將16進(jìn)制字符串轉(zhuǎn)換為10進(jìn)制字符串 <br> * @參數(shù)說(shuō)明:str String 16進(jìn)制字符串 <br> * @返回說(shuō)明:String */ private static String hexStringToString(String str) { if (str == null || str.equals('')) { return null; } str = str.replace(' ', ''); byte[] baKeyword = new byte[str.length() / 2]; for (int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte) (0xff & Integer.parseInt( str.substring(i * 2, i * 2 + 2), 16)); } catch (Exception e) { e.printStackTrace(); } } try { str = new String(baKeyword, 'UTF-8'); new String(); } catch (Exception e1) { e1.printStackTrace(); } return str; } /** * @函數(shù)名稱:encrypt<br> * @創(chuàng)建日期:2016年9月22日<br> * @功能說(shuō)明:加密字節(jié)數(shù)組 <br> * @參數(shù)說(shuō)明:arrB byte[] 需要加密的字節(jié)數(shù)組 <br> * @參數(shù)說(shuō)明:key String 秘鑰 <br> * @返回說(shuō)明:byte[] */ private static byte[] encrypt(byte[] arrB, String key) throws Exception { return converCrypt(arrB, key, true); } /** * @函數(shù)名稱:encrypt<br> * @創(chuàng)建日期:2016年9月22日<br> * @功能說(shuō)明:加密字符串 <br> * @參數(shù)說(shuō)明:xml String 加密字符串 <br> * @參數(shù)說(shuō)明:key String 秘鑰 <br> * @返回說(shuō)明:String 返回加密后的16進(jìn)制字符串 */ public static String encrypt(String xml, String key) { try { return encodeHexStr(encrypt(xml.getBytes('UTF-8'), key), DIGITS_UPPER); } catch (Exception e) { System.out.println(e); return ''; } } /** * @函數(shù)名稱:decrypt<br> * @創(chuàng)建日期:2016年9月22日<br> * @功能說(shuō)明: 將16進(jìn)制字節(jié)數(shù)組進(jìn)行解密 <br> * @參數(shù)說(shuō)明: arrB byte[] 解密字節(jié)數(shù)組<br> * @參數(shù)說(shuō)明:key String 秘鑰 <br> * @返回說(shuō)明:byte[] 返回解密后 的16位字節(jié)數(shù)組 */ private static byte[] decrypt(byte[] arrB, String key) throws Exception { return converCrypt(arrB, key, false); } /** * @函數(shù)名稱:converCrypt<br> * @創(chuàng)建日期:2016年9月22日<br> * @功能說(shuō)明:將16位的字節(jié)數(shù)據(jù)進(jìn)行加密或解密 <br> * @參數(shù)說(shuō)明: arrB byte[] 需要加密的字節(jié)數(shù)組<br> * @參數(shù)說(shuō)明: key String 秘鑰<br> * @參數(shù)說(shuō)明:encrypt boolean是否加密,true加密,false解密 <br> * @返回說(shuō)明:byte[] 返回16進(jìn)制字節(jié)數(shù)組 */ private static byte[] converCrypt(byte[] arrB, String key, boolean encrypt) throws Exception{ String vikey = MD5.sign(key).substring(0, 8).toUpperCase(); DESKeySpec desKeySpec = new DESKeySpec(vikey.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance('DES'); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec ivp = new IvParameterSpec(vikey.getBytes()); Cipher cipher = Cipher.getInstance('DES/CBC/PKCS5Padding'); /**加密*/ if (encrypt == true) { cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivp); } else{ /**解密*/ cipher.init(Cipher.DECRYPT_MODE, secretKey, ivp); } return cipher.doFinal(arrB); } /** * @函數(shù)名稱:decrypt<br> * @創(chuàng)建日期:2016年9月22日<br> * @功能說(shuō)明:解密16進(jìn)制字符串 <br> * @參數(shù)說(shuō)明: desStr String 需要解密的16進(jìn)制字符串<br> * @參數(shù)說(shuō)明: key String 秘鑰<br> * @返回說(shuō)明:String 返回解密后的10進(jìn)制字符串 */ public static String decrypt(String desStr, String key) { try{ if (null == desStr || null == key) { return ''; } return hexStringToString(encodeHexStr(decrypt(hexStringToByte(new String(desStr.getBytes('UTF-8'))), key), DIGITS_UPPER)); } catch (Exception e) { System.out.println(); return ''; } } /** * @函數(shù)名稱:hexStringToByte<br> * @創(chuàng)建日期:2016年9月22日<br> * @功能說(shuō)明:將16進(jìn)制字符串轉(zhuǎn)換為16進(jìn)制字節(jié)數(shù)組 <br> * @參數(shù)說(shuō)明:hex String需要轉(zhuǎn)換的16進(jìn)制字符串 <br> * @返回說(shuō)明:byte[] 返回轉(zhuǎn)換后的16進(jìn)制字節(jié)數(shù)組 */ private static byte[] hexStringToByte(String hex) { int len = (hex.length() / 2); byte[] result = new byte[len]; char[] achar = hex.toCharArray(); for (int i = 0; i < len; i++) { int pos = i * 2; result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1])); } return result; } /** * @函數(shù)名稱:toByte<br> * @創(chuàng)建日期:2016年9月22日<br> * @功能說(shuō)明: 將字符轉(zhuǎn)換為字節(jié)<br> * @參數(shù)說(shuō)明: c char 需要轉(zhuǎn)換的字符<br> * @返回說(shuō)明:int 返回字符對(duì)應(yīng)的字節(jié)碼 */ private static int toByte(char c) { byte b = (byte) '0123456789ABCDEF'.indexOf(c); return b; } /** * @構(gòu)造函數(shù) */ public DESUtil() { // TODO Auto-generated constructor stub } /** * @函數(shù)名稱:main<br> * @創(chuàng)建日期:2016年9月21日<br> * @功能說(shuō)明: <br> * @參數(shù)說(shuō)明: <br> * @返回說(shuō)明:void */ public static void main(String[] args) { // TODO Auto-generated method stub try { System.out.println('-----------------------------'); String bStr = '123'; String key = 'SZAOA589'; String binSing = encrypt(bStr, key); System.out.println('加密前:'+bStr); System.out.println('加密后:'+binSing); System.out.println('解密后:'+decrypt(binSing, key)); System.out.println('-------------------------'); } catch (Exception e) { // TODO Auto-generated catch bloc e.printStackTrace(); } //3B976A2A2919A60B57DFF3518F65E1FF //3B976A2A2919A60B57DFF3518F65E1FF /* * C4A737D04D0D05E2 BD2DD4FC5050EBD0 */ }}//================================package com.cn.peitest;import java.security.MessageDigest; public class MD5 { // 生成MD5(截取16位長(zhǎng)度) public static String sign(String message) { String md5 = ''; try { MessageDigest md = MessageDigest.getInstance('MD5'); // 創(chuàng)建一個(gè)md5算法對(duì)象 byte[] messageByte = message.getBytes('UTF-8'); byte[] md5Byte = md.digest(messageByte); // 獲得MD5字節(jié)數(shù)組,16*8=128位 md5 = bytesToHex(md5Byte).substring(0, 16); // 轉(zhuǎn)換為16進(jìn)制字符串 } catch (Exception e) { e.printStackTrace(); } return md5; } // 二進(jìn)制轉(zhuǎn)十六進(jìn)制 public static String bytesToHex(byte[] bytes) { StringBuffer hexStr = new StringBuffer(); int num; for (int i = 0; i < bytes.length; i++) { num = bytes[i]; if (num < 0) { num += 256; } if (num < 16) { hexStr.append('0'); } hexStr.append(Integer.toHexString(num)); } return hexStr.toString().toUpperCase(); }}

以上就是java 實(shí)現(xiàn)DES 加密解密的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于java des加密解密的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 成人不卡| 亚洲欧美一区二区三区1000 | 日韩精品一区二区三区在线 | 福利网站导航 | 国产免费一区二区三区 | 一区二区成人 | 精品国产乱码久久久久久丨区2区 | 久久一区精品 | 波多野结衣一区二区 | 中文字幕亚洲区 | 日批日韩在线观看 | 嫩草最新网址 | 免费看国产a | 又黑又粗又长的欧美一区 | 欧美在线一区二区三区 | 亚洲天天干 | 一区二区久久 | 美女爽到呻吟久久久久 | 国产精品毛片一区二区在线看 | 美女在线观看国产 | 中文字幕日韩欧美一区二区三区 | 久草在线免费资源 | 色综合久久天天综合网 | 亚洲女人天堂成人av在线 | 国产精品av久久久久久久久久 | 四虎在线视频 | 欧美xxxx网站 | 三a毛片 | 伊人网站在线观看 | 在线观看亚洲一区二区 | 亚洲不卡 | 91视频在线 | 日韩欧美精品一区 | 精品欧美一区二区三区久久久 | 亚洲男人的天堂网站 | 中文字幕久久精品 | 亚洲 中文 欧美 日韩 在线观看 | 欧美午夜影院 | 日韩欧美电影在线 | 日本午夜精品一区二区三区 | 成人在线中文字幕 |