gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
用java 编写个 md5 加密
- User
用java 编写个 md5 加密
- Assistant
下面是java代码实现MD5加密:
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Encrypt { public static String encrypt(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(input.getBytes()); byte[] bytes = md.digest(); StringBuilder sb = new StringBuilder(); for(int i=0; i<bytes.length; i++) { sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } public static void main(String[] args) { String input = "password"; String output = encrypt(input); System.out.println("Input: " + input); System.out.println("MD5 Hash: " + output); } }这段代码使用
java.security.MessageDigest类来实现 MD5 加密。使用getInstance("MD5")方法获取 MD5 加密算法的实例,通过调用update()方法来更新输入数据的摘要信息,然后调用digest()方法获取最终的摘要信息。最后将输出摘要信息转换成16进制字符串即可。 - User
md5 加密
- Assistant
MD5(Message-Digest Algorithm 5)是一种哈希算法,将任意长度的输入数据转换为固定长度(128位)的输出数据,常用于数据完整性校验、数字签名等领域。由于MD5算法已经被证实不够安全,因此不建议再用于加密敏感数据,可以考虑使用更为安全的 SHA-256、SHA-3等哈希算法。
MD5加密的步骤:
1.预处理:
MD5算法将输入数据分为若干个64字节的数据块,对于长度不足64字节的数据块,需要填充至64字节。填充的方式为,将一个1和若干个0填充到数据块的结尾,使得填充后数据块的字节长度为56的倍数。最后,将64位的数据块长度(单位为bit)附加到数据块的结尾。
2.初始化:
MD5算法使用4个32位的寄存器A、B、C、D作为状态变量,初始值为以下内容:
A = 0x67452301 B = 0xEFCDAB89 C = 0x98BADCFE D = 0x103254763.处理每个数据块:
MD5算法定义了4个基本的变换函数(F、G、H、I)和一组常数组T,它们是核心处理函数。每个64字节的数据块将被分成16个小块,每个小块占用4个字节,对于每个小块,进行以下操作:
- 将小块转换为32位的整数x;
- 将以下值代入原始的4个状态变量中:
a = A b = B c = C d = D- 根据以下公式计算新的a、b、c、d:
a' = b + ((a + (F(b, c, d) + x + T[i]) <<< s) & 0xFFFFFFFF) b' = c c' = d d' = a- 将计算结果赋值给新的状态变量,即:
A = (A + a') & 0xFFFFFFFF B = (B + b') & 0xFFFFFFFF C = (C + c') & 0xFFFFFFFF D = (D + d') & 0xFFFFFFFF- 重复上述过程(共进行4次基本操作),得到最终的状态变量值。
4.输出结果:
对于所有数据块处理完毕后,将4个状态变量拼接成一个128位整数,即输出结果。
下面给出一个示例代码,实现MD5算法的Java代码实现:
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Encrypt { private static final int S11 = 7; private static final int S12 = 12; private static final int S13 = 17; private static final int S14 = 22; private static final int S21 = 5; private static final int S22 = 9; private static final int S23 = 14; private static final int S24 = 20; private static final int S31 = 4; private static final int S32 = 11; private static final int S33 = 16; private static final int S34 = 23; private static final int S41 = 6; private static final int S42 = 10; private static final int S43 = 15; private static final int S44 = 21; private static final int[] T = new int[] { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }; private static final int BLOCK_SIZE = 64; private static final int LENGTH_SIZE = 8; private byte[] buffer; private long[] state; private long[] count; public MD5Encrypt() { buffer = new byte[BLOCK_SIZE]; state = new long[] {0x67452301L, 0xefcdab89L, 0x98badcfeL, 0x10325476L}; count = new long[] {0, 0}; } public String encrypt(String input) { byte[] data = input.getBytes(); int index = (int) (count[0] >> 3) & 0x3F; long len = data.length; // update length count[0] += (len << 3); if(count[0] < (len << 3)) { count[1]++; } count[1] += (len >> 29); int partLen = BLOCK_SIZE - index; int i = 0; // process input data if(len > partLen) { System.arraycopy(data, 0, buffer, index, partLen); transform(buffer, 0); for(i = partLen; i + BLOCK_SIZE <= len; i += BLOCK_SIZE) { System.arraycopy(data, i, buffer, 0, BLOCK_SIZE); transform(buffer, 0); } index = 0; } // buffer remaining input data if(i < len) { int remaining = (int) (len - i); System.arraycopy(data, i, buffer, index, remaining); } return toHex(state); } private void transform(byte[] block, int offset) { long a = state[0]; long b = state[1]; long c = state[2]; long d = state[3]; long[] x = new long[16]; for(int i=0; i<16; i++) { x[i] = (block[offset+i*4+0] & 0xFF) | (block[offset+i*4+1] & 0xFF) << 8 | (block[offset+i*4+2] & 0xFF) << 16 | (block[offset+i*4+3] & 0xFF) << 24; } a = FF(a, b, c, d, x[0], S11, T[0]); d = FF(d, a, b, c, x[1], S12, T[1]); c = FF(c, d, a, b, x[2], S13, T[2]); b = FF(b, c, d, a, x[3], S14, T[3]); a = FF(a, b, c, d, x[4], S11, T[4]); d = FF(d, a, b, c, x[5], S12, T[5]); c = FF(c, d, a, b, x[6], S13, T[6]); b = FF(b, c, d, a, x[7], S14, T[7]); a = FF(a, b, c, d, x[8], S11, T[8]); d = FF(d, a, b, c, x[9], S12, T[9]); c = FF(c, d, a, b, x[10], S13, T[10]); b = FF(b, c, d, a, x[11], S14, T[11]); a = FF(a, b, c, d, x[12], S11, T[12]); d = FF(d, a, b, c, x[13], S12, T[13]); c = FF(c, d, a, b, x[14], S13, T[14]); b = FF(b, c, d, a, x[15], S14, T[15]); a = GG(a, b, c, d, x[1], S21, T[16]); d = GG(d, a, b, c, x[6], S22, T[17]); c = GG(c, d, a, b, x[11], S23, T[18]); b = GG(b, c, d, a, x[0], S24, T[19]); a = GG(a, b, c, d, x[5], S21, T[20]); d = GG(d, a, b, c, x[10], S22, T[21]); c = GG(c, d, a, b, x[15], S23, T[22]); b = GG(b, c, d, a, x[4], S24, T[23]); a = GG(a, b, c, d, x[9], S21, T[24]); d = GG(d, a, b, c, x[14], S22, T[25]); c = GG(c, d, a, b, x[3], S23, T[26]); b = GG(b, c, d, a, x[8], S24, T[27]); a = GG(a, b, c, d, x[13], S21, T[28]); d = GG(d, a, b, c, x[2], S22, T[29]); c = GG(c, d, a, b, x[7], S23, T[30]); b = GG(b, c, d, a, x[12], S24, T[31]); a = HH(a, b, c, d, x[5], S31, T[32]); d = HH(d, a, b, c, x[8], S32, T[33]); c = HH(c, d, a, b, x[11], S33, T[34]); b = HH(b, c, d, a, x[14], S34, T[35]); a = HH(a, b, c, d, x[1], S31, T[36]); d = HH(d, a, b, c, x[4], S32, T[37]); c = HH(c, d, a, b, x[7], S33, T[38]); b = HH(b, c, d, a, x[10], S34, T[39]); a = HH(a, b, c, d, x[13], S31, T[40]); d = HH(d, a, b, c, x[0], S32, T[41]); c = HH(c, d, a, b, x[3], S33, T[42]); b = HH(b, c, d, a, x[6], S34, T[43]); a = HH(a, b, c, d, x[9], S31, T[44]); d = HH(d, a, b, c, x[12], S32, T[45]); c = HH(c, d, a, b, x[15], S33, T[46]); b = HH(b, c, d, a, x[2], S34, T[47]); a = II(a, b, c, d, x[0], S41, T[48]); d = II(d, a, b, c, x[7], S42, T[49]); c = II(c, d, a, b, x[14], S43, T[50]); b = II(b, c, d, a, x[5], S44, T[51]); a = II(a, b, c, d, x[12], S41, T[52]); d = II(d, a, b, c, x[3], S42, T[53]); c = II(c, d, a, b, x[10], S43, T[54]); b = II(b, c, d, a, x[1], S44, T[55]); a = II(a, b, c, d, x[8], S41, T[56]); d = II(d, a, b, c, x[15], S42, T[57]); c = II(c, d, a, b, x[6], S43, T[58]); b = II(b, c, d, a, x[13], S44, T[59]); a = II(a, b, c, d, x[4], S41, T[60]); d = II(d, a, b, c, x[11], S42, T[61]); c = II(c, d, a, b, x[2], S43, T[62]); b = II(b, c, d, a, x[9], S44, T[63]); state[0] += a; state[1] += b; state[2] += c; state[3] += d; } private long FF(long a, long b, long c, long d, long x, int s, int t) { long temp = a + F(b, c, d) + x + t; temp = (temp << s) | (temp >>> (32 - s)); temp += b; return temp & 0xFFFFFFFFL; } private long GG(long a, long b, long c, long d, long x, int s, int t) { long temp = a + G(b, c, d) + x + t; temp = (temp << s) | (temp >>> (32 - s)); temp += b; return temp & 0xFFFFFFFFL; } private long HH(long a, long b, long c, long d, long x, int s, int t) { long temp = a + H(b, c, d) + x + t; temp = (temp << s) | (temp >>> (32 - s)); temp += b; return temp & 0xFFFFFFFFL; } private long II(long a, long b, long c, long d, long x, int s, int t) { long temp = a + I(b, c, d) + x + t;