免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
MD5加密
概述:
出于安全考慮,網(wǎng)絡的傳輸中經(jīng)常對傳輸數(shù)據(jù)做加密和編碼處理,其中涉及以下幾種: 

1、md5加密,該加密算法是單向加密,即加密的數(shù)據(jù)不能再通過解密還原。相關類包含在java.security.MessageDigest包中。 

2、3-DES加密,該加密算法是可逆的,解密方可以通過與加密方約定的密鑰匙進行解密。相關類包含在javax.crypto.*包中。 

3、base64編碼,是用于傳輸8bit字節(jié)代碼最常用的編碼方式。相關類在sun.misc.BASE64Decoder 和sun.misc.BASE64Encoder 中。 

4、URLEncoder編碼,是一種字符編碼,保證被傳送的參數(shù)由遵循規(guī)范的文本組成。相關類在java.net.URLEncoder包中。 

細節(jié):
1、進行MD5加密,得到byte[]
/**
  * 進行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("GBK"));
}
catch(Exception e)
{
e.printStackTrace();
}
return returnByte;


2、得到3-DES的密鑰匙
/**
 * 得到3-DES的密鑰匙
 * 根據(jù)根據(jù)需要,如密鑰匙為24個字節(jié),md5加密出來的是16個字節(jié),因此后面補8個字節(jié)的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、3-DES加密
/**
  * 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;
 }
4、對字符串進行Base64編碼
/**
  * 對字符串進行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;
 }

5、根據(jù)需要可以去掉字符串的換行符號
/**
 * 去掉字符串的換行符號
 * base64編碼3-DES的數(shù)據(jù)時,得到的字符串有換行符號,根據(jù)需要可以去掉
     */

 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;
 }

6、對字符串進行URLDecoder.encode(strEncoding)編碼
/**
  * 對字符串進行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;
 }

7、對字符串進行URLDecoder.decode(strEncoding)解碼
/**
  * 對字符串進行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;
 }

8、進行3-DES解密(密鑰匙等同于加密的密鑰匙)
/**
  * 
  *進行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;

經(jīng)過以上步驟就可以完成MD5加密,3-DES加密、base64編碼傳輸、base64解碼、3-DES解密得到原文。



程序全文如下:
package com.neusoft.test.util.crypt;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;


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;

/**
 * <p>Title:加密解密測試</p>
 *
 * <p>Description: 加密解密</p>
 *
 *<p>Date       : 2005-08-11</p>
 *
 * <p>Copyright: Copyright (c) 2005 neusoft</p>
 *
 * <p>Company: neusoft</p>
 *
 * @author mengk
 * @version 1.00
 *
 * <p>------------------------------------------------------------</p>
 * <p> 修改歷史 </p>
 * <p>  序號    日期    修改人      修改原因</p>
 * <p>   1                           </p>
 */

public class Endecrypt {


/**
 * 進行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("GBK"));
}
catch(Exception e)
{
e.printStackTrace();
}
return returnByte;



/**
 * 得到3-DES的密鑰匙
 * 根據(jù)接口規(guī)范,密鑰匙為24個字節(jié),md5加密出來的是16個字節(jié),因此后面補8個字節(jié)的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的數(shù)據(jù)時,得到的字符串有換行符號
 * ,一定要去掉,否則uni-wise平臺解析票根不會成功, 
 * 提示“sp驗證失敗”。在開發(fā)的過程中,因為這個問題讓我束手無策,
 * 一個朋友告訴我可以問聯(lián)通要一段加密后 的文字,然后去和自己生成的字符串比較,
 * 這是個不錯的調試方法。我最后比較發(fā)現(xiàn)我生成的字符串唯一不同的 是多了換行。
 * 我用c#語言也寫了票根請求程序,沒有發(fā)現(xiàn)這個問題。 
 * 
     */

 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加密的內容在進行\(zhòng)"UTF-16LE\"取字節(jié)
    byte[] src2 = src.getBytes("UTF-16LE");
    //進行3-DES加密后的內容的字節(jié)
    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加密的內容在進行\(zhòng)"UTF-16LE\"取字節(jié)

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

 
 
public static void main(String[] args) {
Endecrypt test = new Endecrypt();
String oldString = "毒素發(fā)";

String SPKEY = "1234";
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);


}


}
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java與Delphi交叉DES加解密的問題
RSA加密解密及數(shù)字簽名Java實現(xiàn)
主題:java 加密解密簡單實現(xiàn)
JAVA實現(xiàn)DES加密
使用RSA進行信息加密解密的WebService示例
DES加密、解密字符串算法(java版)
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服