加密解密工具

來源:互聯網
上載者:User

標籤:

package com.neusoft.mid.cloong.web.page.common;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class EndecryptUtil {

/**
* 進行MD5加密
*
* @param String
* 原始的SPKEY
* @return byte[] 指定加密方式為md5後的byte[]
*/

private byte[] md5(String strSrc) {
byte[] returnByte = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
returnByte = md5.digest(strSrc.getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
return returnByte;
}

/**
* 得到3-DES的密鑰匙 根據介面規範,密鑰匙為24個位元組,md5加密出來的是16個位元組,因此後面補8個位元組的0
*
* @param String
* 原始的SPKEY
* @return byte[] 指定加密方式為md5後的byte[]
*/

private byte[] getEnKey(String spKey) {
byte[] desKey = null;
try {
byte[] desKey1 = md5(spKey);
desKey = new byte[24];
int i = 0;
while (i < desKey1.length && i < 24) {
desKey[i] = desKey1[i];
i++;
}
if (i < 24) {
desKey[i] = 0;
i++;
}
} catch (Exception e) {
e.printStackTrace();
}

return desKey;
}

/**
* 3-DES加密
*
* @param byte[] src 要進行3-DES加密的byte[]
* @param byte[] enKey 3-DES加密金鑰
* @return byte[] 3-DES加密後的byte[]
*/

public byte[] Encrypt(byte[] src, byte[] enKey) {
byte[] encryptedData = null;
try {
DESedeKeySpec dks = new DESedeKeySpec(enKey);
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
encryptedData = cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedData;
}

/**
* 對字串進行Base64編碼
*
* @param byte[] src 要進行編碼的字元
*
* @return String 進行編碼後的字串
*/

public String getBase64Encode(byte[] src) {
String requestValue = "";
try {
BASE64Encoder base64en = new BASE64Encoder();
requestValue = base64en.encode(src);
// System.out.println(requestValue);
} catch (Exception e) {
e.printStackTrace();
}

return requestValue;
}

/**
* 去掉字串的分行符號號 base64編碼3-DES的資料時,得到的字串有分行符號號 ,一定要去掉,否則uni-wise平台解析票根不會成功,
* 提示“sp驗證失敗”。在開發的過程中,因為這個問題讓我束手無策, 一個朋友告訴我可以問聯通要一段加密後 的文字,然後去和自己產生的字串比較,
* 這是個不錯的調試方法。我最後比較發現我產生的字串唯一不同的 是多了換行。 我用c#語言也寫了票根請求程式,沒有發現這個問題。
*
*/

private String filter(String str) {
String output = null;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
int asc = str.charAt(i);
if (asc != 10 && asc != 13)
sb.append(str.subSequence(i, i + 1));
}
output = new String(sb);
return output;
}

/**
* 對字串進行URLDecoder.encode(strEncoding)編碼
*
* @param String
* src 要進行編碼的字串
*
* @return String 進行編碼後的字串
*/

public String getURLEncode(String src) {
String requestValue = "";
try {

requestValue = URLEncoder.encode(src);
} catch (Exception e) {
e.printStackTrace();
}

return requestValue;
}

/**
* 3-DES加密
*
* @param String
* src 要進行3-DES加密的String
* @param String
* spkey分配的SPKEY
* @return String 3-DES加密後的String
*/

public String get3DESEncrypt(String src, String spkey) {
String requestValue = "";
try {

// 得到3-DES的密鑰匙
byte[] enKey = getEnKey(spkey);
// 要進行3-DES加密的內容在進行/"UTF-16LE/"取位元組
byte[] src2 = src.getBytes("UTF-16LE");
// 進行3-DES加密後的內容的位元組
byte[] encryptedData = Encrypt(src2, enKey);

// 進行3-DES加密後的內容進行BASE64編碼
String base64String = getBase64Encode(encryptedData);
// BASE64編碼去除分行符號後
String base64Encrypt = filter(base64String);

// 對BASE64編碼中的HTML控制碼進行轉義的過程
requestValue = getURLEncode(base64Encrypt);
// System.out.println(requestValue);
} catch (Exception e) {
e.printStackTrace();
}

return requestValue;
}

/**
* 對字串進行URLDecoder.decode(strEncoding)解碼
*
* @param String
* src 要進行解碼的字串
*
* @return String 進行解碼後的字串
*/

public String getURLDecoderdecode(String src) {
String requestValue = "";
try {

requestValue = URLDecoder.decode(src);
} catch (Exception e) {
e.printStackTrace();
}

return requestValue;
}

/**
*
* 進行3-DES解密(密鑰匙等同於加密的密鑰匙)。
*
* @param byte[] src 要進行3-DES解密byte[]
* @param String
* spkey分配的SPKEY
* @return String 3-DES解密後的String
*/
public String deCrypt(byte[] debase64, String spKey) {
String strDe = null;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DESede");
byte[] key = getEnKey(spKey);
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance("DESede");
SecretKey sKey = keyFactory.generateSecret(dks);
cipher.init(Cipher.DECRYPT_MODE, sKey);
byte ciphertext[] = cipher.doFinal(debase64);
strDe = new String(ciphertext, "UTF-16LE");
} catch (Exception ex) {
strDe = "";
ex.printStackTrace();
}
return strDe;
}

/**
* 3-DES解密
*
* @param String
* src 要進行3-DES解密的String
* @param String
* spkey分配的SPKEY
* @return String 3-DES加密後的String
*/

public String get3DESDecrypt(String src, String spkey) {
String requestValue = "";
try {

// 得到3-DES的密鑰匙

// URLDecoder.decodeTML控制碼進行轉義的過程
String URLValue = getURLDecoderdecode(src);

// 進行3-DES加密後的內容進行BASE64編碼

BASE64Decoder base64Decode = new BASE64Decoder();
byte[] base64DValue = base64Decode.decodeBuffer(URLValue);

// 要進行3-DES加密的內容在進行/"UTF-16LE/"取位元組

requestValue = deCrypt(base64DValue, spkey);
} catch (Exception e) {
e.printStackTrace();
}
return requestValue;
}

public static void main(String[] args) {
EndecryptUtil test = new EndecryptUtil();
String oldString = "張哥威武";

String SPKEY = "neusoft";
System.out.println("1。分配的SPKEY為: " + SPKEY);
System.out.println("2。的內容為: " + oldString);
String reValue = test.get3DESEncrypt(oldString, SPKEY);
reValue = reValue.trim().intern();
System.out.println("進行3-DES加密後的內容: " + reValue);
String reValue2 = test.get3DESDecrypt(reValue, SPKEY);
System.out.println("進行3-DES解密後的內容: " + reValue2);
}
}

加密解密工具

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.