Java實(shí)現(xiàn)的DES加密解密工具類實(shí)例
本文實(shí)例講述了Java實(shí)現(xiàn)的DES加密解密工具類。分享給大家供大家參考,具體如下:
一個(gè)工具類,很常用,不做深入研究了,那來(lái)可直接用
DesUtil.java
package lsy;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DesUtil {
/**
* @param args
*/
public static void main(String[] args) {
//以下是加密方法algorithm="AES"的測(cè)試
System.out.println(DesUtil.getInstance("lushuaiyin").getEnCodeString("hello"));
//輸出 LDewGAZkmWHeYFjBz56ylw==
//將上面的密文解密:
System.out.println(DesUtil.getInstance("lushuaiyin").getDecodeString("LDewGAZkmWHeYFjBz56ylw=="));
//輸出 hello
//改變密鑰測(cè)試
System.out.println(DesUtil.getInstance("suolong").getEnCodeString("hello"));
//輸出 /RLowOJ+Fr3KdMcdJeNatg==
System.out.println(DesUtil.getInstance("suolong").getDecodeString("/RLowOJ+Fr3KdMcdJeNatg=="));
//輸出 hello
//如果使用不正確的密鑰解密,將會(huì):
System.out.println(DesUtil.getInstance("suolong").getDecodeString("LDewGAZkmWHeYFjBz56ylw=="));
}
private SecretKey key=null;//密鑰
//定義 加密算法,可用 DES,DESede,Blowfish,AES
//不同的加密方式結(jié)果會(huì)不同
private static String algorithm="AES";
private static DesUtil desUtil=null;
public DesUtil(){}
public static DesUtil getInstance(String strKey){
desUtil=new DesUtil();
desUtil.createKey(strKey);
return desUtil;
}
/**
* algorithm 算法
* @param strKey
*/
public void createKey(String strKey){
try{
KeyGenerator kg=KeyGenerator.getInstance(DesUtil.algorithm);
byte[] bt=strKey.getBytes("UTF-8");
SecureRandom sr=new SecureRandom(bt);
kg.init(sr);
this.setKey(kg.generateKey());
}catch(Exception e){
}
}
/**
* 加密方法,返回密文
* cipher 密碼
* @param dataStr
*/
public String getEnCodeString(String dataStr){
byte[] miwen=null;//密文
byte[] mingwen=null;//明文
Cipher cipher;
String result="";//密文字符串
try{
mingwen=dataStr.getBytes("UTF-8");
cipher=Cipher.getInstance(DesUtil.algorithm);
cipher.init(Cipher.ENCRYPT_MODE, this.getKey());
miwen=cipher.doFinal(mingwen);
BASE64Encoder base64en = new BASE64Encoder();
result=base64en.encodeBuffer(miwen);//或者可以用下面的方法得到密文,結(jié)果是不一樣的,都可以正常解密
// result=byte2hex(miwen);//密文結(jié)果類似2C:37:B0:18:06:64:99:61:DE:60:58:C1:CF:9E:B2:97
}catch(Exception e){
e.printStackTrace();
}
return result;
}
/**
* 解密方法,返回明文
* @param codeStr
* @return
*/
public String getDecodeString(String codeStr){
BASE64Decoder base64De = new BASE64Decoder();
byte[] miwen=null;
byte[] mingwen=null;
String resultData="";//返回的明文
Cipher cipher;
try{
miwen=base64De.decodeBuffer(codeStr);
cipher=Cipher.getInstance(DesUtil.algorithm);
cipher.init(Cipher.DECRYPT_MODE, this.getKey());
mingwen=cipher.doFinal(miwen);
resultData = new String(mingwen,"UTF-8");
}catch(Exception e){
return "密鑰不正確或其他原因?qū)е庐惓?,無(wú)法解密!";
}
return resultData;
}
//二行制轉(zhuǎn)字符串
public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
if (n < b.length - 1)
hs = hs + ":";
}
return hs.toUpperCase();
}
public SecretKey getKey() {
return key;
}
public void setKey(SecretKey key) {
this.key = key;
}
public static String getAlgorithm() {
return algorithm;
}
public static void setAlgorithm(String algorithm) {
algorithm = algorithm;
}
}
運(yùn)行結(jié)果:
LDewGAZkmWHeYFjBz56ylw== hello /RLowOJ+Fr3KdMcdJeNatg== hello 密鑰不正確或其他原因?qū)е庐惓?,無(wú)法解密!
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
在線DES加密/解密工具:
http://tools.jb51.net/password/des_encode
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
springboot+mybatis報(bào)錯(cuò)找不到實(shí)體類的問(wèn)題
這篇文章主要介紹了springboot+mybatis報(bào)錯(cuò)找不到實(shí)體類的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Spring.Net在MVC中實(shí)現(xiàn)注入的原理解析
這篇文章主要介紹了Spring.Net在MVC中實(shí)現(xiàn)注入的原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
java:無(wú)法訪問(wèn)org.springframework.boot.SpringApplication問(wèn)題
這篇文章主要介紹了java:無(wú)法訪問(wèn)org.springframework.boot.SpringApplication問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Springboot詳解整合SpringSecurity實(shí)現(xiàn)全過(guò)程
Spring Security基于Spring開(kāi)發(fā),項(xiàng)目中如果使用Springboot作為基礎(chǔ),配合Spring Security做權(quán)限更加方便,而Shiro需要和Spring進(jìn)行整合開(kāi)發(fā)。因此作為spring全家桶中的Spring Security在java領(lǐng)域很常用2022-07-07
javamail實(shí)現(xiàn)注冊(cè)激活郵件
這篇文章主要為大家詳細(xì)介紹了javamail實(shí)現(xiàn)注冊(cè)激活郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
利用Spring Validation實(shí)現(xiàn)輸入驗(yàn)證功能
這篇文章主要給大家介紹了如何利用Spring Validation完美的實(shí)現(xiàn)輸入驗(yàn)證功能,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,感興趣的朋友可以借鑒一下2023-06-06
java實(shí)現(xiàn)二叉樹(shù)的創(chuàng)建及5種遍歷方法(總結(jié))
下面小編就為大家?guī)?lái)一篇java實(shí)現(xiàn)二叉樹(shù)的創(chuàng)建及5種遍歷方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

