java易懂易用的MD5加密(可直接运行) (1)

  1、md5加密,该加密算法是单向加密,即加密的数据不能再通过解密还原。相关类包含在java.security.MessageDigest包中。

  2、3-DES加密,该加密算法是可逆的,解密方可以通过与加密方约定的密钥匙进行解密。相关类包含在javax.crypto.*包中。

  3、base64编码,是用于传输8bit字节代码最常用的编码方式。相关类在sun.misc.BASE64Decoder 和sun.misc.BASE64Encoder 中。

  4、URLEncoder编码,是一种字符编码,保证被传送的参数由遵循规范的文本组成。相关类在java.net.URLEncoder包中。

  细节:

  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的密钥匙

  * 根据根据需要,如密钥匙为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、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、根据需要可以去掉字符串的换行符号

  

复制代码 代码如下:

  /**

  * 去掉字符串的换行符号

  * base64编码3-DES的数据时,得到的字符串有换行符号,根据需要可以去掉

  */

  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;

经过以上步骤就可以完成MD5加密,3-DES加密、base64编码传输、base64解码、3-DES解密得到原文。