java利用des和md5进行加密

来源:互联网 发布:传奇h5源码论坛 编辑:程序博客网 时间:2024/05/22 15:34
package com.routon.utils;import java.security.InvalidKeyException;import java.security.Key;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import android.util.Log;/** * encryption and decryption tool * eg: *  (一、3des) *  byte[] encdata = utilSecurity.utilEncryptWith3DES("123456789abc","test".getBytes(),1); *  byte[] decdata = utilSecurity.utilEncryptWith3DES("123456789abc",encdata,2); *   *  (二、des) *  String encdata = utilSecurity.utilEncryptWithDESECB("123456789","data",1); *  String decdata = utilSecurity.utilEncryptWithDESECB("123456789",encdata,2); *    *  (三、32md5) *  String encdata = utilSecurity.utilEncryptWithMD5_32("data"); *   *  (四、16md5) *  String encdata = utilSecurity.utilEncryptWithMD5_16("data"); * @author xuweilin * */public class utilSecurity {private static String TAG = "utilSecurity";/** * use 3des Algorithm * @param a_pKey  secret * @param a_pInput input string * @param a_iMode 1:enc;2:dec * @return output */   public static byte[] utilEncryptWith3DES(String a_pKey, byte[] a_pInput, int a_iMode){   final String hexString="0123456789ABCDEF";   final String Algorithm = "DESede";   byte[] output = null;   byte[] bytes=a_pKey.getBytes();    StringBuilder sb=new StringBuilder(bytes.length*2);    for(int i=0;i<bytes.length;i++)    {    sb.append(hexString.charAt((bytes[i]&0xf0)>>4));    sb.append(hexString.charAt((bytes[i]&0x0f)>>0));    }        byte[] keyBytes =sb.toString().getBytes();    try {   SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);   Cipher c1 = Cipher.getInstance(Algorithm);if(a_iMode == 1){ c1.init(Cipher.ENCRYPT_MODE, deskey);}else if(a_iMode == 2){c1.init(Cipher.DECRYPT_MODE, deskey);}    output = c1.doFinal(a_pInput);}catch (java.lang.Exception e) {Log.e(TAG, "utilEncryptWith3DES exception:"+e.getMessage());}return output;   }   /**    * use des Algorithm    * @param a_pKey  secret    * @param a_pInput input string    * @param a_iMode 1:enc;2:dec    * @return output    */   public static String utilEncryptWithDESECB(String a_pKey, String a_pInput, int a_iMode){   Key key = getKey(a_pKey.getBytes());   String output = null;   if(a_iMode == 1){   output = getEncriptCode(key, a_pInput);   }else if(a_iMode == 2){   output = getDecriptCode(a_pInput, key);   }   return output;   }   /**   * define key   */   private static Key getKey(byte[] keyByte) {      Key key = null;      byte[] byteTemp = new byte[8];      for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {       byteTemp[i] = keyByte[i];      }      key = new SecretKeySpec(byteTemp, "DES");      return key;   }   /**   * @param key   * @return    */   private static String getEncriptCode(Key key, String srcCode) {      StringBuffer sb = null;      try {       Cipher encriptCipher = Cipher.getInstance("DES");       encriptCipher.init(Cipher.ENCRYPT_MODE, key);       byte[] desCode = encriptCipher.doFinal(srcCode.getBytes());       sb = new StringBuffer(desCode.length * 2);       for (int i = 0; i < desCode.length; i++) {        int temp = desCode[i];        if (temp < 0) {         temp = temp + 256;        }                if (temp < 16) {         sb.append("0");        }        sb.append(Integer.toString(temp, 16));       }      } catch (NoSuchAlgorithmException e) {      Log.e(TAG, "getEncriptCode exception:"+e.getMessage());      } catch (NoSuchPaddingException e) {      Log.e(TAG, "getEncriptCode exception:"+e.getMessage());      } catch (InvalidKeyException e) {      Log.e(TAG, "getEncriptCode exception:"+e.getMessage());      } catch (IllegalBlockSizeException e) {      Log.e(TAG, "getEncriptCode exception:"+e.getMessage());      } catch (BadPaddingException e) {      Log.e(TAG, "getEncriptCode exception:"+e.getMessage());      }      return sb.toString();   }   private static String getDecriptCode(String encriptCode, Key key) {      Cipher decriptCipher = null;      String decriptString = null;      byte[] encriptByte = encriptCode.getBytes();      byte[] decriptByte = new byte[encriptByte.length / 2];      for (int i = 0; i < encriptByte.length; i+=2) {       String strTmp = new String(encriptByte, i, 2);       decriptByte[i / 2] = (byte) Integer.parseInt(strTmp, 16);      }      try {       decriptCipher = Cipher.getInstance("DES");       decriptCipher.init(Cipher.DECRYPT_MODE, key);       byte[] outByte = decriptCipher.doFinal(decriptByte);       decriptString = new String(outByte);      } catch (NoSuchAlgorithmException e) {      Log.e(TAG, "getDecriptCode exception:"+e.getMessage());      } catch (NoSuchPaddingException e) {      Log.e(TAG, "getDecriptCode exception:"+e.getMessage());      } catch (InvalidKeyException e) {      Log.e(TAG, "getDecriptCode exception:"+e.getMessage());      } catch (IllegalBlockSizeException e) {      Log.e(TAG, "getDecriptCode exception:"+e.getMessage());      } catch (BadPaddingException e) {      Log.e(TAG, "getDecriptCode exception:"+e.getMessage());      }      return decriptString;   }   /**    * MD5    * @param a_pInput input string    * @return 32 length encrp string    */  public static String utilEncryptWithMD5_32(String a_pInput){  String output = null;try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(a_pInput.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if(i<0) i+= 256; if(i<16) buf.append("0"); buf.append(Integer.toHexString(i)); }    output = buf.toString();} catch (NoSuchAlgorithmException e) {  Log.e(TAG, "utilEncryptWithMD5_32 exception:"+e.getMessage());} return output;  }  /**   *  MD5   * @param a_pInput input string   * @return 16 length encrp string   */  public static String utilEncryptWithMD5_16(String a_pInput){  String output = null;try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(a_pInput.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if(i<0) i+= 256; if(i<16) buf.append("0"); buf.append(Integer.toHexString(i)); }    output = buf.toString().substring(8,24);} catch (NoSuchAlgorithmException e) { Log.e(TAG, "utilEncryptWithMD5_16 exception:"+e.getMessage());} return output;    }}