java 加密算法

package utils;



import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;



/**

 * 加密类

 *

 * @author Administrator

 *

 */

public class Encrypt {



 /**

  * MD5 16位

  * @param s

  * @return

  * @author Administrator

  * @since 2016年9月9日 上午11:16:09

  */

 public static String md516(String s) {

  if (s != null && s.trim().length() > 0) {

   return textToEncrypt(s, "MD5").substring(8, 24);

  }

  return "";

 }



 /**

  * MD5 32位

  *

  * @param s

  * @return

  * @author Administrator

  * @since 2016年9月9日 上午11:14:08

  */

 public static String md532(String s) {

  return textToEncrypt(s, "MD5");

 }



 /**

  * 加密

  *

  * @param plainText

  * @param type

  * @return

  * @author Administrator

  * @since 2016年9月9日 上午11:13:43

  */

 public static String textToEncrypt(String plainText, String type) {

  String result = "";

  // 首先判断是否为空

  if (null == plainText || "".equals(plainText.trim())) {

   return result;

  }

  try {

   // 首先进行实例化和初始化

   MessageDigest md = MessageDigest.getInstance(type);

   // 得到一个操作系统默认的字节编码格式的字节数组

   byte[] btInput = plainText.getBytes();

   // 对得到的字节数组进行处理

   md.update(btInput);

   // 进行哈希计算并返回结果

   byte[] btResult = md.digest();

   // 进行哈希计算后得到的数据的长度

   StringBuffer sb = new StringBuffer();

   for (byte b : btResult) {

    int bt = b & 0xff;

    if (bt < 16) {

     sb.append(0);

    }

    sb.append(Integer.toHexString(bt));

   }

   result = sb.toString();

  } catch (NoSuchAlgorithmException e) {

   e.printStackTrace();

  }

  return result;

 }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值