DES加密解密算法Java实现
生活随笔
收集整理的這篇文章主要介紹了
DES加密解密算法Java实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
DES 使用一個 56 位的密鑰以及附加的 8 位奇偶校驗位,產生最大 64 位的分組大小。這是一個迭代的分組密碼,使用稱為 Feistel 的技術,其中將加密的文本塊分成兩半。使用子密鑰對其中一半應用循環功能,然后將輸出與另一半進行“異或”運算;接著交換這兩半,這一過程會繼續下去,但最后一個循環不交換。DES 使用 16 個循環,使用異或,置換,代換,移位操作四種基本運算。
DES(Data Encryption Standard)是發明最早的最廣泛使用的分組對稱加密算法。DES算法的入口參數有三個:Key、Data、Mode。其中Key為8個字節共64位,是DES算法的工作密鑰;Data也為8個字節64位,是要被加密或被解密的數據;Mode為DES的工作方式,有兩種:加密或解密。
參考代碼如下:
package com.gddx.des;import java.io.IOException; import java.security.SecureRandom;import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec;/*** DES加密 解密算法* * @author Fangjs* @date 2017-03-16 */ public class DesUtil {private final static String DES = "DES";private final static String ENCODE = "UTF-8";private final static String defaultKey = "gddxbdhp";//8字節key長度/*** 使用 默認key加密*/public static String encrypt(String express) throws Exception {if (express == null) return null;byte[] bCiphertext = encrypt(express.getBytes(ENCODE), defaultKey.getBytes(ENCODE));String ciphertext = new sun.misc.BASE64Encoder().encode(bCiphertext);return ciphertext;//返回密文}/*** 使用 默認key 解密*/public static String decrypt(String ciphertext) throws IOException, Exception {if (ciphertext == null) return null;sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();byte[] buf = decoder.decodeBuffer(ciphertext);byte[] bExpress = decrypt(buf, defaultKey.getBytes(ENCODE));return new String(bExpress, ENCODE);//返回明文}/*** Description 根據鍵值進行加密* @param data* @param key 加密鍵byte數組* @return* @throws Exception*/public static String encrypt(String express, String key) throws Exception {if (express == null) return null;byte[] bCiphertext = encrypt(express.getBytes(ENCODE), key.getBytes(ENCODE));String ciphertext = new sun.misc.BASE64Encoder().encode(bCiphertext);return ciphertext;//返回密文}/*** Description 根據鍵值進行解密* @param data* @param key 加密鍵byte數組* @return* @throws IOException* @throws Exception*/public static String decrypt(String ciphertext, String key) throws IOException, Exception {if (ciphertext == null) return null;sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();byte[] buf = decoder.decodeBuffer(ciphertext);byte[] bExpress = decrypt(buf, key.getBytes(ENCODE));return new String(bExpress, ENCODE);}/*** @param data* @param key 加密鍵byte數組* @return* @throws Exception*/private static byte[] encrypt(byte[] data, byte[] key) throws Exception {// 生成一個可信任的隨機數源SecureRandom sr = new SecureRandom();// 從原始密鑰數據創建DESKeySpec對象DESKeySpec dks = new DESKeySpec(key);// 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks);// Cipher對象實際完成加密操作Cipher cipher = Cipher.getInstance(DES);// 用密鑰初始化Cipher對象cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);return cipher.doFinal(data);}/*** Description 根據鍵值進行解密* @param data* @param key 加密鍵byte數組* @return* @throws Exception*/private static byte[] decrypt(byte[] data, byte[] key) throws Exception {// 生成一個可信任的隨機數源SecureRandom sr = new SecureRandom();// 從原始密鑰數據創建DESKeySpec對象DESKeySpec dks = new DESKeySpec(key);// 創建一個密鑰工廠,然后用它把DESKeySpec轉換成SecretKey對象SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks);// Cipher對象實際完成解密操作Cipher cipher = Cipher.getInstance(DES);// 用密鑰初始化Cipher對象cipher.init(Cipher.DECRYPT_MODE, securekey, sr);return cipher.doFinal(data);}public static void main(String[] args) throws Exception {String express = "DES加密解密算法";String ciphertext=encrypt(express,"12345678");System.out.println(ciphertext);String rexpress=decrypt(ciphertext,"12345678");System.out.println(express);}}執行結果: 1IBx2S1YBzHb0TYdu93sDdwL+MZKsOYw DES加密解密算法
總結
以上是生活随笔為你收集整理的DES加密解密算法Java实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跨Hadoop平台Hive表export
- 下一篇: AES加密解密算法Java实现