java對稱加密,解密,還原原文
package com.suntek.web.hrserver.util;
/*
* DesEncrypt.java
*
*字符串對稱加密
*/
//思路: 因爲 任意一個字符串,都是由若干字節表示的,每個字節實質就是一個
// 有8位的進進制數,
// 又因爲 一個8位二進制數,可用兩位16進制字符串表示.
// 因此 任意一個字符串可以由兩位16進制字符串表示。
// 而 DES是對8位二進制數進行加密,解密。
// 所以 用DES加密解密時,可以把加密所得的8位進進制數,轉成
// 兩位16進制數進行保存,傳輸。
// 具體方法:1 把一個字符串轉成8位二進制數,用DES加密,得到8位二進制數的
// 密文
// 2 然後把(由1)所得的密文轉成兩位十六進制字符串
// 3 解密時,把(由2)所得的兩位十六進制字符串,轉換成8位二進制
// 數的密文
// 4 把子3所得的密文,用DES進行解密,得到8位二進制數形式的明文,
// 並強制轉換成字符串。
// 思考:爲什麼要通過兩位16進制數字符串保存密文呢?
// 原因是:一個字符串加密後所得的8位二進制數,通常不再時字符串了,如果
// 直接把這種密文所得的8位二進制數強制轉成字符串,有許多信息因爲異
// 常而丟失,導制解密失敗。因制要把這個8位二制數,直接以數的形式
// 保存下來,而通常是用兩位十六進制數表示。
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
/**
*
* 使用DES加密與解密,可對byte[],String類型進行加密與解密
* 密文可使用String,byte[]存儲.
*
* 方法:
* void getKey(String strKey)從strKey的字條生成一個Key
*
* String getEncString(String strMing)對strMing進行加密,返回String密文
* String getDesString(String strMi)對strMin進行解密,返回String明文
*
*byte[] getEncCode(byte[] byteS)byte[]型的加密
*byte[] getDesCode(byte[] byteD)byte[]型的解密
*/
public class EncryptionPsw {
Key key;
private EncryptionPsw()
{}
public static EncryptionPsw getInstance()
{
EncryptionPsw des=new EncryptionPsw();
des.getKey(Constants.PROCESSINGKEY);
return des;
}
/**
* 根據參數生成KEY
* @param strKey
*/
public void getKey(String strKey) {
try{
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator=null;
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 加密String明文輸入,String密文輸出
* @param strMing
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try {
return byte2hex(getEncCode (strMing.getBytes() ) );
// byteMing = strMing.getBytes("UTF8");
// byteMi = this.getEncCode(byteMing);
// strMi = new String( byteMi,"UTF8");
}
catch(Exception e){
e.printStackTrace();
}
finally {
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文輸入,String明文輸出
* @param strMi
* @return
*/
public String getDesString(String strMi) {
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
return new String(getDesCode(hex2byte(strMi.getBytes()) ));
// byteMing = this.getDesCode(byteMi);
// strMing = new String(byteMing,"UTF8");
}
catch(Exception e) {
e.printStackTrace();
}
finally {
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文輸入,byte[]密文輸出
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文輸入,以byte[]明文輸出
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina=null;
try{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
}catch(Exception e){
e.printStackTrace();
}finally{
cipher=null;
}
return byteFina;
}
/**
* 二行制轉字符串
* @param b
* @return
*/
public static String byte2hex(byte[] b) { //一個字節的數,
// 轉成16進制字符串
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;
}
return hs.toUpperCase(); //轉成大寫
}
public static byte[] hex2byte(byte[] b) {
if((b.length%2)!=0)
throw new IllegalArgumentException("長度不是偶數");
byte[] b2 = new byte[b.length/2];
for (int n = 0; n < b.length; n+=2) {
String item = new String(b,n,2);
// 兩位一組,表示一個字節,把這樣表示的16進制字符串,還原成一個進制字節
b2[n/2] = (byte)Integer.parseInt(item,16);
}
return b2;
}
public static void main(String[] args){
System.out.println("pa$$word");
EncryptionPsw des=EncryptionPsw.getInstance();
String strEnc = des.getEncString("pa$$word");//加密字符串,返回String的密文
System.out.println(strEnc);
String strDes = des.getDesString(strEnc);//把String 類型的密文解密
System.out.println(strDes);
new EncryptionPsw();
}
}
/*
* DesEncrypt.java
*
*字符串對稱加密
*/
//思路: 因爲 任意一個字符串,都是由若干字節表示的,每個字節實質就是一個
// 有8位的進進制數,
// 又因爲 一個8位二進制數,可用兩位16進制字符串表示.
// 因此 任意一個字符串可以由兩位16進制字符串表示。
// 而 DES是對8位二進制數進行加密,解密。
// 所以 用DES加密解密時,可以把加密所得的8位進進制數,轉成
// 兩位16進制數進行保存,傳輸。
// 具體方法:1 把一個字符串轉成8位二進制數,用DES加密,得到8位二進制數的
// 密文
// 2 然後把(由1)所得的密文轉成兩位十六進制字符串
// 3 解密時,把(由2)所得的兩位十六進制字符串,轉換成8位二進制
// 數的密文
// 4 把子3所得的密文,用DES進行解密,得到8位二進制數形式的明文,
// 並強制轉換成字符串。
// 思考:爲什麼要通過兩位16進制數字符串保存密文呢?
// 原因是:一個字符串加密後所得的8位二進制數,通常不再時字符串了,如果
// 直接把這種密文所得的8位二進制數強制轉成字符串,有許多信息因爲異
// 常而丟失,導制解密失敗。因制要把這個8位二制數,直接以數的形式
// 保存下來,而通常是用兩位十六進制數表示。
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
/**
*
* 使用DES加密與解密,可對byte[],String類型進行加密與解密
* 密文可使用String,byte[]存儲.
*
* 方法:
* void getKey(String strKey)從strKey的字條生成一個Key
*
* String getEncString(String strMing)對strMing進行加密,返回String密文
* String getDesString(String strMi)對strMin進行解密,返回String明文
*
*byte[] getEncCode(byte[] byteS)byte[]型的加密
*byte[] getDesCode(byte[] byteD)byte[]型的解密
*/
public class EncryptionPsw {
Key key;
private EncryptionPsw()
{}
public static EncryptionPsw getInstance()
{
EncryptionPsw des=new EncryptionPsw();
des.getKey(Constants.PROCESSINGKEY);
return des;
}
/**
* 根據參數生成KEY
* @param strKey
*/
public void getKey(String strKey) {
try{
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator=null;
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 加密String明文輸入,String密文輸出
* @param strMing
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
try {
return byte2hex(getEncCode (strMing.getBytes() ) );
// byteMing = strMing.getBytes("UTF8");
// byteMi = this.getEncCode(byteMing);
// strMi = new String( byteMi,"UTF8");
}
catch(Exception e){
e.printStackTrace();
}
finally {
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文輸入,String明文輸出
* @param strMi
* @return
*/
public String getDesString(String strMi) {
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
return new String(getDesCode(hex2byte(strMi.getBytes()) ));
// byteMing = this.getDesCode(byteMi);
// strMing = new String(byteMing,"UTF8");
}
catch(Exception e) {
e.printStackTrace();
}
finally {
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文輸入,byte[]密文輸出
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文輸入,以byte[]明文輸出
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina=null;
try{
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
}catch(Exception e){
e.printStackTrace();
}finally{
cipher=null;
}
return byteFina;
}
/**
* 二行制轉字符串
* @param b
* @return
*/
public static String byte2hex(byte[] b) { //一個字節的數,
// 轉成16進制字符串
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;
}
return hs.toUpperCase(); //轉成大寫
}
public static byte[] hex2byte(byte[] b) {
if((b.length%2)!=0)
throw new IllegalArgumentException("長度不是偶數");
byte[] b2 = new byte[b.length/2];
for (int n = 0; n < b.length; n+=2) {
String item = new String(b,n,2);
// 兩位一組,表示一個字節,把這樣表示的16進制字符串,還原成一個進制字節
b2[n/2] = (byte)Integer.parseInt(item,16);
}
return b2;
}
public static void main(String[] args){
System.out.println("pa$$word");
EncryptionPsw des=EncryptionPsw.getInstance();
String strEnc = des.getEncString("pa$$word");//加密字符串,返回String的密文
System.out.println(strEnc);
String strDes = des.getDesString(strEnc);//把String 類型的密文解密
System.out.println(strDes);
new EncryptionPsw();
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.