今天学了一点点简单的md5加密算法,具体是咋加密的俺暂时也不太清楚,下面的代码就是对象调加密的方法(学别人封装的),简单点就是把一段字符串作为参数传递进来,经过加密后返回给调用对象一个字符串对象!That's all,bye!
/** * Created by Kim on 2017/2/22.*/import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class Md5Util { /** * 按照md5算法对传过来的字符串进行加密 * @param PSD 传过来的字符串 加盐处理 * @return 加密后的字符串 */ public static String encoder(String psd){ try { //加盐处理 psd = psd + "mobilesafe"; //指定加密算法类型 MessageDigest digest = MessageDigest.getInstance("MD5"); //将需要加密的字符串转换成byte数组,然后进行随机哈希的过程 byte[] bs = digest.digest(psd.getBytes()); //循环遍历bs,然后让其生成32位字符串,固定写法 //创建StringBuffer对象 StringBuffer buffer = new StringBuffer(); for (byte b :bs){ //固定的格式 int i = b & 0xff; //Int类型的i需要转换成十六进制的字符 String haxString = Integer.toHexString(i); if (haxString.length()<2){ haxString = "0"+haxString; } buffer.append(haxString); } return buffer.toString(); } catch (NoSuchAlgorithmException e) {//没有这样的算法异常 e.PRintStackTrace(); } return ""; }}
新闻热点
疑难解答