java 實(shí)現(xiàn)DES 加密解密的示例
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)文章!
相關(guān)文章:
1. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享2. 使用Hangfire+.NET 6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)3. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. jsp文件下載功能實(shí)現(xiàn)代碼5. 詳解瀏覽器的緩存機(jī)制6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器8. xml中的空格之完全解說(shuō)9. 如何在jsp界面中插入圖片10. phpstudy apache開(kāi)啟ssi使用詳解
