JAVA做一个简单的MD5加密...
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA做一个简单的MD5加密...相关的知识,希望对你有一定的参考价值。
老师出的题```
让用户输入一个小于8位的整数 首先要把这些数倒序 每个数加5 加5的结果再对10取模 之后再把第一个数和最后一个数换位 输出
不用麻烦的
就利用数组
还有Scanner的包
最后输出看结果是啥
高手赐教啊
自己弄出来了
说了哦 不要其他的 我都还没学到呢
package org.tool;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5_Encoding
private MessageDigest md = null;
private static MD5_Encoding md5 = null;
private static final char[] hexChars = '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ;
/**
* Constructor is private so you must use the getInstance method
*/
private MD5_Encoding() throws NoSuchAlgorithmException
md = MessageDigest.getInstance("MD5");
/**
* This returns the singleton instance
*/
public static MD5_Encoding getInstance() throws NoSuchAlgorithmException
if (md5 == null)
md5 = new MD5_Encoding();
return (md5);
/* 将字符串使用md5加密的方法*/
public static String hashCode(String dataToHash)
throws NoSuchAlgorithmException
return getInstance().hashData(dataToHash.getBytes());
public static String hashCode(byte[] dataToHash)
throws NoSuchAlgorithmException
return getInstance().hashData(dataToHash);
public String hashData(byte[] dataToHash)
return hexStringFromBytes((calculateHash(dataToHash))).toLowerCase();
private byte[] calculateHash(byte[] dataToHash)
md.update(dataToHash, 0, dataToHash.length);
return (md.digest());
public String hexStringFromBytes(byte[] b)
String hex = "";
int msb;
int lsb = 0;
int i;
// MSB maps to idx 0
for (i = 0; i < b.length; i++)
msb = ((int) b[i] & 0x000000FF) / 16;
lsb = ((int) b[i] & 0x000000FF) % 16;
hex = hex + hexChars[msb] + hexChars[lsb];
return (hex);
public static void main(String args[]) throws Exception
//举例对1进行加密
System.out.println(MD5_Encoding.hashCode("1"));
参考技术B public class MD5
public static String getMD5(String text)
StringBuffer md5 = new StringBuffer();
MessageDigest md = null;
try
md = MessageDigest.getInstance("MD5");
catch (NoSuchAlgorithmException e)
e.printStackTrace();
md.update(text.getBytes());
byte[] temp = md.digest();
int i;
for (int offset = 0; offset < temp.length; offset++)
i = temp[offset];
if (i < 0)
i += 256;
if (i < 16)
md5.append("0");
md5.append(Integer.toHexString(i));
return md5.toString();
JAVA中简单的MD5加密类(MD5Utils)
MD5加密分析:
![](https://image.cha138.com/20210611/d4268f7dbf6d4115a5872aba5e7f52ec.jpg)
JDK API:
![](https://image.cha138.com/20210611/4ab35e8347574b09a73bb13fa8e63def.jpg)
获取对象的API:
![](https://image.cha138.com/20210611/4d436916bb364c528c943222896fff30.jpg)
加密的API:
![](https://image.cha138.com/20210611/e34e7305048e48bdbf8ed977dc5e153f.jpg)
1 package cn.utils; 2 3 import java.security.MessageDigest; 4 import java.security.NoSuchAlgorithmException; 5 6 /** 7 * @author CQY13 MD5加密工具类 8 */ 9 public class MD5Utils { 10 11 /** 12 * 获取MD5加密 13 * 14 * @param pwd 15 * 需要加密的字符串 16 * @return String字符串 加密后的字符串 17 */ 18 public static String getPwd(String pwd) { 19 try { 20 // 创建加密对象 21 MessageDigest digest = MessageDigest.getInstance("md5"); 22 23 // 调用加密对象的方法,加密的动作已经完成 24 byte[] bs = digest.digest(pwd.getBytes()); 25 // 接下来,我们要对加密后的结果,进行优化,按照mysql的优化思路走 26 // mysql的优化思路: 27 // 第一步,将数据全部转换成正数: 28 String hexString = ""; 29 for (byte b : bs) { 30 // 第一步,将数据全部转换成正数: 31 // 解释:为什么采用b&255 32 /* 33 * b:它本来是一个byte类型的数据(1个字节) 255:是一个int类型的数据(4个字节) 34 * byte类型的数据与int类型的数据进行运算,会自动类型提升为int类型 eg: b: 1001 1100(原始数据) 35 * 运算时: b: 0000 0000 0000 0000 0000 0000 1001 1100 255: 0000 36 * 0000 0000 0000 0000 0000 1111 1111 结果:0000 0000 0000 0000 37 * 0000 0000 1001 1100 此时的temp是一个int类型的整数 38 */ 39 int temp = b & 255; 40 // 第二步,将所有的数据转换成16进制的形式 41 // 注意:转换的时候注意if正数>=0&&<16,那么如果使用Integer.toHexString(),可能会造成缺少位数 42 // 因此,需要对temp进行判断 43 if (temp < 16 && temp >= 0) { 44 // 手动补上一个“0” 45 hexString = hexString + "0" + Integer.toHexString(temp); 46 } else { 47 hexString = hexString + Integer.toHexString(temp); 48 } 49 } 50 return hexString; 51 } catch (NoSuchAlgorithmException e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } 55 return ""; 56 } 57 58 /** 59 * @param args 60 */ 61 public static void main(String[] args) { 62 String pwd = MD5Utils.getPwd("abc"); 63 System.out.println(pwd); 64 } 65 66 }
以上是关于JAVA做一个简单的MD5加密...的主要内容,如果未能解决你的问题,请参考以下文章