Java最簡(jiǎn)單的DES加密算法實(shí)現(xiàn)案例
更新時(shí)間:2017年06月12日 08:57:26 投稿:jingxian
下面小編就為大家?guī)?lái)一篇Java最簡(jiǎn)單的DES加密算法實(shí)現(xiàn)案例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
Base64.java
package com.mstf.des; import java.io.UnsupportedEncodingException; /** * base64編碼/解碼 * @author ceet * */ public class Base64 { public static String encode(String data) { return new String(encode(data.getBytes())); } public static String decode(String data) { try { return new String(decode(data.toCharArray()),"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } private static char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" .toCharArray(); private static byte[] codes = new byte[256]; static { for (int i = 0; i < 256; i++) { codes[i] = -1; } for (int i = 'A'; i <= 'Z'; i++) { codes[i] = (byte) (i - 'A'); } for (int i = 'a'; i <= 'z'; i++) { codes[i] = (byte) (26 + i - 'a'); } for (int i = '0'; i <= '9'; i++) { codes[i] = (byte) (52 + i - '0'); } codes['+'] = 62; codes['/'] = 63; } public static char[] encode(byte[] data) { char[] out = new char[((data.length + 2) / 3) * 4]; for (int i = 0, index = 0; i < data.length; i += 3, index += 4) { boolean quad = false; boolean trip = false; int val = (0xFF & (int) data[i]); val <<= 8; if ((i + 1) < data.length) { val |= (0xFF & (int) data[i + 1]); trip = true; } val <<= 8; if ((i + 2) < data.length) { val |= (0xFF & (int) data[i + 2]); quad = true; } out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)]; val >>= 6; out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)]; val >>= 6; out[index + 1] = alphabet[val & 0x3F]; val >>= 6; out[index + 0] = alphabet[val & 0x3F]; } return out; } public static byte[] decode(char[] data) { int tempLen = data.length; for (int ix = 0; ix < data.length; ix++) { if ((data[ix] > 255) || codes[data[ix]] < 0) { --tempLen; } } int len = (tempLen / 4) * 3; if ((tempLen % 4) == 3) { len += 2; } if ((tempLen % 4) == 2) { len += 1; } byte[] out = new byte[len]; int shift = 0; int accum = 0; int index = 0; for (int ix = 0; ix < data.length; ix++) { int value = (data[ix] > 255) ? -1 : codes[data[ix]]; if (value >= 0) { accum <<= 6; shift += 6; accum |= value; if (shift >= 8) { shift -= 8; out[index++] = (byte) ((accum >> shift) & 0xff); } } } if (index != out.length) { throw new Error("Miscalculated data length (wrote " + index + " instead of " + out.length + ")"); } return out; } }
DESUtil.java
package com.mstf.des; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; /** * DES對(duì)稱算法(加密/解密) * * @author ceet * */ public class DESUtil { private Key key; public DESUtil(String strKey) { setKey(strKey); } public void setKey(String strKey) { try { KeyGenerator generator = KeyGenerator.getInstance("DES"); generator.init(new SecureRandom(strKey.getBytes())); // 根據(jù)參數(shù)生成key this.key = generator.generateKey(); } catch (Exception e) { e.printStackTrace(); } } public String encrypt(String source) { return encrypt(source, "utf-8"); } public String decrypt(String encryptedData) { return decrypt(encryptedData, "utf-8"); } public String encrypt(String source, String charSet) { String encrypt = null; try { byte[] ret = encrypt(source.getBytes(charSet)); encrypt = new String(Base64.encode(ret)); } catch (Exception e) { e.printStackTrace(); encrypt = null; } return encrypt; } public String decrypt(String encryptedData, String charSet) { String descryptedData = null; try { byte[] ret = descrypt(Base64.decode(encryptedData.toCharArray())); descryptedData = new String(ret, charSet); } catch (Exception e) { e.printStackTrace(); descryptedData = null; } return descryptedData; } private byte[] encrypt(byte[] primaryData) { try { Cipher cipher = Cipher.getInstance("DES"); // Cipher對(duì)象實(shí)際完成加密操作 cipher.init(Cipher.ENCRYPT_MODE, this.key); // 用密鑰初始化Cipher對(duì)象(加密) return cipher.doFinal(primaryData); } catch (Exception e) { e.printStackTrace(); return null; } } private byte[] descrypt(byte[] encryptedData) { try { Cipher cipher = Cipher.getInstance("DES"); // Cipher對(duì)象實(shí)際完成解密操作 cipher.init(Cipher.DECRYPT_MODE, this.key); // 用密鑰初始化Cipher對(duì)象(解密) return cipher.doFinal(encryptedData); } catch (Exception e) { e.printStackTrace(); return null; } } public static void main(String[] args) { String code = "ceet"; DESUtil desUtil = new DESUtil("key"); String encrypt = desUtil.encrypt(code); String decrypt = desUtil.decrypt(encrypt); System.out.println("原內(nèi)容:" + code); System.out.println("加密:" + encrypt); System.out.println("解密:" + decrypt); } }
以上這篇Java最簡(jiǎn)單的DES加密算法實(shí)現(xiàn)案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中的JPA(Java?Persistence?API)詳解
這篇文章主要介紹了SpringBoot中的JPA(Java?Persistence?API)詳解,JPA用于將?Java?對(duì)象映射到關(guān)系型數(shù)據(jù)庫(kù)中,它提供了一種面向?qū)ο蟮姆绞絹?lái)操作數(shù)據(jù)庫(kù),使得開發(fā)者可以更加方便地進(jìn)行數(shù)據(jù)持久化操作,需要的朋友可以參考下2023-07-07簡(jiǎn)單談?wù)刯ava中匿名內(nèi)部類構(gòu)造函數(shù)
這篇文章主要簡(jiǎn)單給我們介紹了java中匿名內(nèi)部類構(gòu)造函數(shù),并附上了簡(jiǎn)單的示例,有需要的小伙伴可以參考下。2015-11-11Java利用沙箱支付實(shí)現(xiàn)電腦掃碼支付教程
當(dāng)我們制作的項(xiàng)目需要實(shí)現(xiàn)電腦掃碼支付功能時(shí),我們往往會(huì)采用沙箱支付來(lái)模擬實(shí)現(xiàn)。本文將主要介紹如何在Java中利用沙箱支付實(shí)現(xiàn)這一功能,需要的可以參考一下2022-01-01Java數(shù)據(jù)結(jié)構(gòu)之單鏈表的實(shí)現(xiàn)與面試題匯總
由于順序表的插入刪除操作需要移動(dòng)大量的元素,影響了運(yùn)行效率,因此引入了線性表的鏈?zhǔn)酱鎯?chǔ)——單鏈表。本文為大家介紹了單鏈表的實(shí)現(xiàn)與面試題匯總,感興趣的可以了解一下2022-10-10