Java開發經驗分享之JAVA簡單實現DES加密與實現DES解密
阿新 • • 發佈:2019-01-28
exc final secret for 操作 好的 16進制 subst ace
前言:相信很多小夥伴在開發過程中都會加密問題。當然,小菜在開發中也遇到了,這裏呢,小菜想說的是JAVA簡單實現DES加密。
今天就簡單實現一下如何DES加密解密。話不多說,代碼如下。直接可用。希望能成為你項目中的一個很好的工具類。
public class DesEncrypt { // DES加密密鑰key public static String key = "sdfDA12r3JHV214IJrwerDSO892BK2345nrekk35oewr4wrwrenlklknsdlemifzkw8iiiifegJG7649UJNDFJSvgsfdjFGDFGj435jUhjhjbkajb12kj987gsjh9834tbAXiudhf9B3PM4bt98dyf9Q2m97jjyf417aliD";// DES加密明文plaintext @SuppressWarnings("static-access") public static String encryptDES(String plaintext) { try { // 首先,DES算法要求有一個可信任的隨機數源,可以通過 SecureRandom類,內置兩種隨機數算法,NativePRNG和SHA1PRNG SecureRandom random = new SecureRandom(); // 創建一個DESKeySpec對象 DESKeySpec desKey = newDESKeySpec(key.getBytes()); // 創建一個密匙工廠 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 將DESKeySpec對象轉換成SecretKey對象 SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher對象實際完成加密操作 Cipher cipher = Cipher.getInstance("DES");// 用密匙初始化Cipher對象 cipher.init(cipher.ENCRYPT_MODE, securekey, random); // 加密生成密文byte數組 byte[] cipherBytes = cipher.doFinal(plaintext.getBytes()); // 將密文byte數組轉化為16進制密文 String ciphertext = byteToHex(cipherBytes); return ciphertext; } catch (Throwable e) { e.printStackTrace(); } return null; } // DES解密 @SuppressWarnings("static-access") public static String decryptDES(String ciphertext) { try { // DES算法要求有一個可信任的隨機數源,SecureRandom內置兩種隨機數算法,NativePRNG和SHA1PRNG,
// 通過new來初始化,默認來說會使用NativePRNG算法生成隨機數,但是也可以配置-Djava.security參數來修改調用的算法,
// 如果是/dev/[u]random兩者之一就是NativePRNG,否則就是SHA1PRNG。 SecureRandom random = new SecureRandom(); // 創建一個DESKeySpec對象 DESKeySpec desKey = new DESKeySpec(key.getBytes()); // 創建一個密匙工廠 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 將DESKeySpec對象轉換成SecretKey對象 SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher對象實際完成解密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher對象 cipher.init(cipher.DECRYPT_MODE, securekey, random); // 將16進制密文轉化為密文byte數組 byte[] cipherBytes = hexToByte(ciphertext); // 真正開始解密操作 String plaintext = new String(cipher.doFinal(cipherBytes)); return plaintext; } catch (Throwable e) { e.printStackTrace(); } return null; } // 將byte轉化為16進制 public static String byteToHex(byte[] bs) { if (0 == bs.length) { return ""; } else { StringBuffer sb = new StringBuffer(); for (int i = 0; i < bs.length; i++) { String s = Integer.toHexString(bs[i] & 0xFF); if (1 == s.length()) { sb.append("0"); } sb = sb.append(s.toUpperCase()); } return sb.toString(); } } // 將16進制轉化為byte public static byte[] hexToByte(String ciphertext) { byte[] cipherBytes = ciphertext.getBytes(); if ((cipherBytes.length % 2) != 0) { throw new IllegalArgumentException("長度不為偶數"); } else { byte[] result = new byte[cipherBytes.length / 2]; for (int i = 0; i < cipherBytes.length; i += 2) { String item = new String(cipherBytes, i, 2); result[i / 2] = (byte) Integer.parseInt(item, 16); } return result; } }
// 進行測試 public static void main(String[] args) { // 當前時間戳 long timestamp = System.currentTimeMillis(); System.out.println(timestamp); // 待加密內容 String str = "測試內容:Mr.JimYi" + timestamp; // 密碼,長度要是8的倍數 String ciphertext = encryptDES(str); System.out.println("加密後:" + ciphertext); String plaintext = decryptDES(ciphertext); System.out.println("解密後:" + plaintext); String t = plaintext.substring(plaintext.length() - 13, plaintext.length()); System.out.println(t); } }
Java開發經驗分享之JAVA簡單實現DES加密與實現DES解密