java中DES加密解密
更新時(shí)間:2015年03月12日 14:27:24 投稿:hebedich
本文給大家分享的是一段java中實(shí)現(xiàn)des加密解密的代碼,非常的實(shí)用,基本每個(gè)項(xiàng)目都可以用到,推薦給大家。
廢話不多說(shuō),直接奉上代碼:
代碼一
package com.eabax.plugin.yundada.utils;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
public class DESEncryptHelper {
private final static String DES = "DES";
/**
* 生成密鑰
* @param employeeCode
*/
public static String getDESKey(String encryptStr){
if (!CacheManager.getCache().containsKey("encryptKey_"+encryptStr)) {
CacheManager.getCache().put("encryptKey_"+encryptStr, encryptStr+"tablemiyaokey");
}
String key = (String) CacheManager.getCache().get("encryptKey_"+encryptStr);
return key;
}
/**
* Description 根據(jù)鍵值進(jìn)行解密
* @param data
* @param key 加密鍵byte數(shù)組
* @return
* @throws IOException
* @throws Exception
*/
public static String decrypt(String data, String key) throws IOException,
Exception {
if (data == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] buf = decoder.decodeBuffer(data);
byte[] bt = decrypt(buf,key.getBytes());
return new String(bt);
}
/**
* 對(duì)字符串加密
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws InvalidKeySpecException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public static String getEncryptStr(String str,String encryptStr) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException,
InvalidKeySpecException, NoSuchAlgorithmException,
NoSuchPaddingException {
//獲取key
String key = getDESKey(encryptStr);
//獲取密鑰
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
DESKeySpec keyspec = new DESKeySpec(key.getBytes());
SecretKey deskey = factory.generateSecret(keyspec);
// Cipher負(fù)責(zé)完成加密或解密工作
Cipher c = Cipher.getInstance("DES");
// 根據(jù)密鑰,對(duì)Cipher對(duì)象進(jìn)行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, deskey);
byte[] src = str.getBytes();
// 該字節(jié)數(shù)組負(fù)責(zé)保存加密的結(jié)果
byte[] cipherByte = c.doFinal(src);
String enstr = new String(Base64.encodeBase64(cipherByte));
return enstr;
}
/**
* 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);
}
}
代碼二
package com.sinosoft.olyvem.common;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Encoder;
public class DES ...{
private byte[] desKey;
public DES(byte[] desKey) ...{
this.desKey = desKey;
}
public byte[] doEncrypt(byte[] plainText) throws Exception ...{
// DES算法要求有一個(gè)可信任的隨機(jī)數(shù)源
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey;/**//* 用某種方法獲得密匙數(shù)據(jù) */
// 從原始密匙數(shù)據(jù)創(chuàng)建DESKeySpec對(duì)象
DESKeySpec dks = new DESKeySpec(rawKeyData);
// 創(chuàng)建一個(gè)密匙工廠,然后用它把DESKeySpec轉(zhuǎn)換成
// 一個(gè)SecretKey對(duì)象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
// Cipher對(duì)象實(shí)際完成加密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher對(duì)象
cipher.init(Cipher.ENCRYPT_MODE, key, sr);
// 現(xiàn)在,獲取數(shù)據(jù)并加密
byte data[] = plainText;/**//* 用某種方法獲取數(shù)據(jù) */
// 正式執(zhí)行加密操作
byte encryptedData[] = cipher.doFinal(data);
return encryptedData;
}
public byte[] doDecrypt(byte[] encryptText) throws Exception ...{
// DES算法要求有一個(gè)可信任的隨機(jī)數(shù)源
SecureRandom sr = new SecureRandom();
byte rawKeyData[] = desKey; /**//* 用某種方法獲取原始密匙數(shù)據(jù) */
// 從原始密匙數(shù)據(jù)創(chuàng)建一個(gè)DESKeySpec對(duì)象
DESKeySpec dks = new DESKeySpec(rawKeyData);
// 創(chuàng)建一個(gè)密匙工廠,然后用它把DESKeySpec對(duì)象轉(zhuǎn)換成
// 一個(gè)SecretKey對(duì)象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
// Cipher對(duì)象實(shí)際完成解密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher對(duì)象
cipher.init(Cipher.DECRYPT_MODE, key, sr);
// 現(xiàn)在,獲取數(shù)據(jù)并解密
byte encryptedData[] = encryptText;/**//* 獲得經(jīng)過(guò)加密的數(shù)據(jù) */
// 正式執(zhí)行解密操作
byte decryptedData[] = cipher.doFinal(encryptedData);
return decryptedData;
}
public static void main(String[] args) throws Exception ...{
String key = "FtpXPass";
String value = "olympic";
BASE64Encoder base64Encoder = new BASE64Encoder();
DES desEncrypt = new DES(key.getBytes());
byte[] encryptText = desEncrypt.doEncrypt(value.getBytes());
//System.out.println("doEncrypt - " + toHexString(encryptText));
System.out.println("doEncrypt - "
+ base64Encoder.encode(encryptText));
byte[] decryptText = desEncrypt.doDecrypt("r9NGYcKAtdo=".getBytes());
System.out.println("doDecrypt - " + new String(decryptText));
//System.out.println("doDecrypt - " + toHexString(decryptText));
}
public static String toHexString(byte[] value) ...{
String newString = "";
for (int i = 0; i < value.length; i++) ...{
byte b = value[i];
String str = Integer.toHexString(b);
if (str.length() > 2) ...{
str = str.substring(str.length() - 2);
}
if (str.length() < 2) ...{
str = "0" + str;
}
newString += str;
}
return newString.toUpperCase();
}
}
以上就是本文關(guān)于DES加密解密的代碼了,希望對(duì)大家學(xué)習(xí)java有所幫助。
您可能感興趣的文章:
- java 實(shí)現(xiàn)DES 加密解密的示例
- Python和Java進(jìn)行DES加密和解密的實(shí)例
- Java實(shí)現(xiàn)的3des加密解密工具類示例
- Java實(shí)現(xiàn)的DES加密解密工具類實(shí)例
- Java使用Hutool實(shí)現(xiàn)AES、DES加密解密的方法
- java基于Des對(duì)稱加密算法實(shí)現(xiàn)的加密與解密功能詳解
- Java實(shí)現(xiàn)DES加密與解密,md5加密以及Java實(shí)現(xiàn)MD5加密解密類
- PHP、Java des加密解密實(shí)例
- java使用des加密解密示例分享
- [J2SE]Java中3DES加密解密調(diào)用示例
- plsql實(shí)現(xiàn)DES對(duì)稱加密 Java解密
相關(guān)文章
詳解Spring boot/Spring 統(tǒng)一錯(cuò)誤處理方案的使用
這篇文章主要介紹了詳解Spring boot/Spring 統(tǒng)一錯(cuò)誤處理方案的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
restemplate請(qǐng)求亂碼之content-encoding=“gzip“示例詳解
RestTemplate從Spring3.0開(kāi)始支持的一個(gè)HTTP請(qǐng)求工具,它提供了常見(jiàn)的REST請(qǐng)求方案的模板,及一些通用的請(qǐng)求執(zhí)行方法 exchange 以及 execute,接下來(lái)通過(guò)本文給大家介紹restemplate請(qǐng)求亂碼之content-encoding=“gzip“,需要的朋友可以參考下2024-03-03
基于maven使用IDEA創(chuàng)建多模塊項(xiàng)目
這篇文章主要介紹了基于maven使用IDEA創(chuàng)建多模塊項(xiàng)目,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
詳解JAVA 設(shè)計(jì)模式之狀態(tài)模式
這篇文章主要介紹了JAVA 狀態(tài)模式的的相關(guān)資料,文中講解的非常細(xì)致,幫助大家更好的學(xué)習(xí)理解JAVA 設(shè)計(jì)模式,感興趣的朋友可以了解下2020-06-06
java文字轉(zhuǎn)語(yǔ)音的實(shí)現(xiàn)示例
在Java中,我們可以使用第三方庫(kù)來(lái)實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音的功能,本文主要介紹了java文字轉(zhuǎn)語(yǔ)音的實(shí)現(xiàn)示例,選擇jacob技術(shù)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
解析Spring中@Controller@Service等線程安全問(wèn)題
這篇文章主要為大家介紹解析了Spring中@Controller@Service等線程的安全問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03

