JavaMD5加密工具类

import com.sun.crypto.provider.SunJCE;
import java.security.Key;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Hex;


public class CodeUtil
{
  public static final String MD5 = "MD5";
  private static final String DigestAlgorithm = "SHA1";
  private static final String KeyAlgorithm = "DESede";
  private static final String CryptAlgorithm = "DESede/CBC/PKCS5Padding";
  private static final String keyString = "3497C134D60D7F9E404916BA3252F8C85E8AEA1315626EC1";
  
  static
  {
    java.security.Security.addProvider(new SunJCE());
  }


  private static final byte[] IV = { 1, 2, 3, 4, 5, 6, 7, 8 };
  private static final String encoding = "UTF-8";
  
  public CodeUtil() {}
  
  public static byte[] base64Encode(byte[] b) { return Base64.encode(b); }


  public static byte[] base64Decode(byte[] b)
  {
    return Base64.decode(b);
  }


  public static byte[] base64Decode(String s)
  {
    return Base64.decode(s);
  }


  private static Key KeyGenerator(String KeyStr)
    throws Exception
  {
    byte[] input = Hex.decode(KeyStr);
    DESedeKeySpec KeySpec = new DESedeKeySpec(input);
    SecretKeyFactory KeyFactory = SecretKeyFactory.getInstance("DESede");
    return KeyFactory.generateSecret(KeySpec);
  }


  private static IvParameterSpec IvGenerator(byte[] b)
    throws Exception
  {
    IvParameterSpec IV = new IvParameterSpec(b);
    return IV;
  }


  public static String Encrypt(String strTobeEnCrypted)
    throws Exception
  {
    byte[] input = strTobeEnCrypted.getBytes("UTF-8");
    Key k = KeyGenerator("3497C134D60D7F9E404916BA3252F8C85E8AEA1315626EC1");
    IvParameterSpec IVSpec = IvGenerator(IV);
    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    c.init(1, k, IVSpec);
    byte[] output = c.doFinal(input);
    return new String(base64Encode(output), "UTF-8");
  }


  public static String Decrypt(String strTobeDeCrypted)
    throws Exception
  {
    byte[] input = base64Decode(strTobeDeCrypted);
    Key k = KeyGenerator("3497C134D60D7F9E404916BA3252F8C85E8AEA1315626EC1");
    IvParameterSpec IVSpec = IvGenerator(IV);
    Cipher c = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    c.init(2, k, IVSpec);
    byte[] output = c.doFinal(input);
    return new String(output, "UTF-8");
  }


  public static String encode(String password, String algorithm)
  {
    byte[] unencodedPassword = password.getBytes();
    MessageDigest md = null;
    try
    {
      md = MessageDigest.getInstance(algorithm);
    }
    catch (Exception e)
    {
      return password;
    }
    md.reset();
    md.update(unencodedPassword);
    byte[] encodedPassword = md.digest();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < encodedPassword.length; i++)
    {
      if ((encodedPassword[i] & 0xFF) < 16)
      {
        buf.append("0");
      }
      buf.append(Long.toString(encodedPassword[i] & 0xFF, 16));
    }
    return buf.toString();
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值