當(dāng)前位置:
首頁(yè) >
DES加密解密算法Java实现
發(fā)布時(shí)間:2025/4/16
42
豆豆
生活随笔
收集整理的這篇文章主要介紹了
DES加密解密算法Java实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
DES 使用一個(gè) 56 位的密鑰以及附加的 8 位奇偶校驗(yàn)位,產(chǎn)生最大 64 位的分組大小。這是一個(gè)迭代的分組密碼,使用稱為 Feistel 的技術(shù),其中將加密的文本塊分成兩半。使用子密鑰對(duì)其中一半應(yīng)用循環(huán)功能,然后將輸出與另一半進(jìn)行“異或”運(yùn)算;接著交換這兩半,這一過(guò)程會(huì)繼續(xù)下去,但最后一個(gè)循環(huán)不交換。DES 使用 16 個(gè)循環(huán),使用異或,置換,代換,移位操作四種基本運(yùn)算。
DES(Data Encryption Standard)是發(fā)明最早的最廣泛使用的分組對(duì)稱加密算法。DES算法的入口參數(shù)有三個(gè):Key、Data、Mode。其中Key為8個(gè)字節(jié)共64位,是DES算法的工作密鑰;Data也為8個(gè)字節(jié)64位,是要被加密或被解密的數(shù)據(jù);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字節(jié)key長(zhǎng)度/*** 使用 默認(rèn)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;//返回密文}/*** 使用 默認(rèn)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 根據(jù)鍵值進(jìn)行加密* @param data* @param key 加密鍵byte數(shù)組* @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 根據(jù)鍵值進(jìn)行解密* @param data* @param key 加密鍵byte數(shù)組* @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數(shù)組* @return* @throws Exception*/private static byte[] encrypt(byte[] data, byte[] key) throws Exception {// 生成一個(gè)可信任的隨機(jī)數(shù)源SecureRandom sr = new SecureRandom();// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象DESKeySpec dks = new DESKeySpec(key);// 創(chuàng)建一個(gè)密鑰工廠,然后用它把DESKeySpec轉(zhuǎn)換成SecretKey對(duì)象SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks);// Cipher對(duì)象實(shí)際完成加密操作Cipher cipher = Cipher.getInstance(DES);// 用密鑰初始化Cipher對(duì)象cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);return cipher.doFinal(data);}/*** Description 根據(jù)鍵值進(jìn)行解密* @param data* @param key 加密鍵byte數(shù)組* @return* @throws Exception*/private static byte[] decrypt(byte[] data, byte[] key) throws Exception {// 生成一個(gè)可信任的隨機(jī)數(shù)源SecureRandom sr = new SecureRandom();// 從原始密鑰數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象DESKeySpec dks = new DESKeySpec(key);// 創(chuàng)建一個(gè)密鑰工廠,然后用它把DESKeySpec轉(zhuǎn)換成SecretKey對(duì)象SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);SecretKey securekey = keyFactory.generateSecret(dks);// Cipher對(duì)象實(shí)際完成解密操作Cipher cipher = Cipher.getInstance(DES);// 用密鑰初始化Cipher對(duì)象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);}}執(zhí)行結(jié)果: 1IBx2S1YBzHb0TYdu93sDdwL+MZKsOYw DES加密解密算法
總結(jié)
以上是生活随笔為你收集整理的DES加密解密算法Java实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 跨Hadoop平台Hive表export
- 下一篇: AES加密解密算法Java实现