java中DES加密解密
廢話不多說(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)文章
使用@PathVariable接收兩個(gè)參數(shù)
Swift洗牌動(dòng)畫效果的實(shí)現(xiàn)方法
ResultSet如何動(dòng)態(tài)獲取列名和值
Springboot在有鎖的情況下正確使用事務(wù)的實(shí)現(xiàn)代碼
SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)url攔截(基于rbac模型)
SpringBoot整合mybatis-plus實(shí)現(xiàn)分頁(yè)查詢功能

