原创
转载请注明出处,侵权必究
1、MD5算法介绍
MD5由密码学家Ron Rivest提出,根源来自于一系列信息摘要算法,从MD到MD5。
2、算法流程
2.1 填充字节
在原消息中添加填充位,添加的长度在1到512之间。从而使得填充后的消息长度等于一个值,比512位的倍数少64位。例如原消息100位,填充348位,得到448位,448=512-64。其中,这没有填充的64位用来记录原消息的长度值,如果超过了2的64方位,就取低64位。
2.2 分块
将整个消息长度分为512位的倍数块,每块由16个32比特的字构成。
2.3初始化寄存器
MD5缓冲区初始化算法要使用128位长的缓冲区以存储中间结果和最终Hash值。缓冲区用4个32比特长的寄存器A,B,C,D构成。每个寄存器的初始十六进制值分别为A=0x01234567,B=89ABCDEF,C=0xFEDCBA98,D=0x76543210
2.5 处理每一个分块
每个分块都由压缩函数H()处理,压缩函数是算法的核心。包括四轮处理,每轮16步,总共64步。
2.6 输出结果
每个分块处理后,最后压缩函数的输出即为产生的消息摘要。
3、盐值的使用
在客户端的登录服务器时,如果只用MD5加密密码,第一容易被破解(反向破解,网上已经有工具);第二容易被重放攻击。
盐值的使用可以解决以上两个问题。
3.1 MD5破解问题
在原明文密码基础上加一个很长的字符串(盐值),在进行MD5加密。足够长的字符很难被网上的反向破解方法破解。
3.2 重放攻击问题
每次客户端连上服务器后,服务器先生成一个盐值(salt),发送给客户端。客户端必须在MD5加密用户密码明文后和该盐值结合,再进行MD5加密,即md5(md5(密码明文)+salt)。这个盐值随时都会改变,所以和上次相同的概率基本为0。进而同一个用户发送给服务器的MD5加密结果每次均不相同。从而攻击者无法通过重放攻击来实现登录。
4、 算法实现
4.1 Java
采用了双MD5加密用户密码和盐值,md5(md5(密码明文)+salt)
package com.nesc.security;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang3.RandomStringUtils;
/**
*
* 双MD5加密用户密码和盐值,md5(md5(密码明文)+salt)
*
* @author nesc418
* @Date 2019-1-25
* @version 0.0.1
*/
public class Md5 {
/**
* 获取随机字符串
* @return String
*/
public static String getRandStr() {
return RandomStringUtils.randomAlphanumeric(20);
}
/**
* 获取加密后的信息摘要
* @return {@link String}
*/
public static String getKeySaltHash(String userKey,String salt) {
String digest = null;
try {
byte[] keyByte = userKey.getBytes("UTF-8");
byte[] saltByte = salt.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] userKeyHash = md.digest(keyByte);
// StringBuilder sb1 = new StringBuilder(2 * userKeyHash.length);
// for (byte b : userKeyHash) {
// sb1.append(String.format("%02x", b & 0xff));
// }
// digest = sb1.toString();
byte[] cat = new byte[userKeyHash.length + saltByte.length];
System.arraycopy(userKeyHash, 0, cat, 0, userKeyHash.length);
System.arraycopy(saltByte, 0, cat, userKeyHash.length, saltByte.length);
StringBuilder sb = new StringBuilder(2 * cat.length);
for (byte b : cat) {
sb.append(String.format("%02x", b & 0xff));
}
digest = sb.toString();
System.out.println(digest);
// String keyHashCatSalt = userKeyHash.toString().concat(salt);//把salt接到后面
byte[] hash = md.digest(cat);
//converting byte array to Hexadecimal String
sb = new StringBuilder(2 * hash.length);
for (byte b : hash) {
sb.append(String.format("%02x", b & 0xff));
}
digest = sb.toString();
}catch (NoSuchAlgorithmException ex) {
//Logger.getLogger(StringReplace.class.getName()).log(Level.SEVERE, null, ex);
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return digest;
}
}
4.2 cpp
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef unsigned char *POINTER;
typedef unsigned short int UINT2;
typedef unsigned long int UINT4;
typedef struct
{
UINT4 state[4];
UINT4 count[2];
unsigned char buffer[64];
} MD5_CTX;
void MD5Init(MD5_CTX *);
void MD5Update(MD5_CTX *, unsigned char *, unsigned int);
void MD5Final(unsigned char [16], MD5_CTX *);
bool getRandStr(char * randStr);
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
static unsigned char PADDING[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#define FF(a, b, c, d, x, s, ac) { (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
#define GG(a, b, c, d, x, s, ac) { (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
#define HH(a, b, c, d, x, s, ac) { (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
#define II(a, b, c, d, x, s, ac) { (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
inline void Encode(unsigned char *output, UINT4 *input, unsigned int len)
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (unsigned char)(input[i] & 0xff);
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
}
}
inline void Decode(UINT4 *output, unsigned char *input, unsigned int len)
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
}
inline void MD5Transform (UINT4 state[4], unsigned char block[64])
{
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
Decode (x, block, 64);
FF (a, b, c, d, x[ 0], S11, 0xd76aa478);
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756);
FF (c, d, a, b, x[ 2], S13, 0x242070db);
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee);
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf);
FF (d, a, b, c, x[ 5], S12, 0x4787c62a);
FF (c, d, a, b, x[ 6], S13, 0xa8304613);
FF (b, c, d, a, x[ 7], S14, 0xfd469501);
FF (a, b, c, d, x[ 8], S11, 0x698098d8);
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af);
FF (c, d, a, b, x[10], S13, 0xffff5bb1);
FF (b, c, d, a, x[11], S14, 0x895cd7be);
FF (a, b, c, d, x[12], S11, 0x6b901122);
FF (d, a, b, c, x[13], S12, 0xfd987193);
FF (c, d, a, b, x[14], S13, 0xa679438e);
FF (b, c, d, a, x[15], S14, 0x49b40821);
GG (a, b, c, d, x[ 1], S21, 0xf61e2562);
GG (d, a, b, c, x[ 6], S22, 0xc040b340);
GG (c, d, a, b, x[11], S23, 0x265e5a51);
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
GG (a, b, c, d, x[ 5], S21, 0xd62f105d);
GG (d, a, b, c, x[10], S22, 0x2441453);
GG (c, d, a, b, x[15], S23, 0xd8a1e681);
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6);
GG (d, a, b, c, x[14], S22, 0xc33707d6);
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87);
GG (b, c, d, a, x[ 8], S24, 0x455a14ed);
GG (a, b, c, d, x[13], S21, 0xa9e3e905);
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
GG (c, d, a, b, x[ 7], S23, 0x676f02d9);
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);
HH (a, b, c, d, x[ 5], S31, 0xfffa3942);
HH (d, a, b, c, x[ 8], S32, 0x8771f681);
HH (c, d, a, b, x[11], S33, 0x6d9d6122);
HH (b, c, d, a, x[14], S34, 0xfde5380c);
HH (a, b, c, d, x[ 1], S31, 0xa4beea44);
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60);
HH (b, c, d, a, x[10], S34, 0xbebfbc70);
HH (a, b, c, d, x[13], S31, 0x289b7ec6);
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa);
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085);
HH (b, c, d, a, x[ 6], S34, 0x4881d05);
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039);
HH (d, a, b, c, x[12], S32, 0xe6db99e5);
HH (c, d, a, b, x[15], S33, 0x1fa27cf8);
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665);
II (a, b, c, d, x[ 0], S41, 0xf4292244);
II (d, a, b, c, x[ 7], S42, 0x432aff97);
II (c, d, a, b, x[14], S43, 0xab9423a7);
II (b, c, d, a, x[ 5], S44, 0xfc93a039);
II (a, b, c, d, x[12], S41, 0x655b59c3);
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
II (c, d, a, b, x[10], S43, 0xffeff47d);
II (b, c, d, a, x[ 1], S44, 0x85845dd1);
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f);
II (d, a, b, c, x[15], S42, 0xfe2ce6e0);
II (c, d, a, b, x[ 6], S43, 0xa3014314);
II (b, c, d, a, x[13], S44, 0x4e0811a1);
II (a, b, c, d, x[ 4], S41, 0xf7537e82);
II (d, a, b, c, x[11], S42, 0xbd3af235);
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
II (b, c, d, a, x[ 9], S44, 0xeb86d391);
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
memset ((POINTER)x, 0, sizeof (x));
}
//初始化
inline void MD5Init(MD5_CTX *context)
{
context->count[0] = context->count[1] = 0;
// printf("count=%ld %ld ",context->count[0],context->count[1]);
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
}
inline void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen)
{
unsigned int i, index, partLen;
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
if ((context->count[0] += ((UINT4)inputLen << 3))
< ((UINT4)inputLen << 3))
context->count[1]++;
context->count[1] += ((UINT4)inputLen >> 29);
partLen = 64 - index;
if (inputLen >= partLen) {
memcpy((POINTER)&context->buffer[index], (POINTER)input, partLen);
MD5Transform(context->state, context->buffer);
for (i = partLen; i + 63 < inputLen; i += 64)
MD5Transform (context->state, &input[i]);
index = 0;
}
else
i = 0;
memcpy((POINTER)&context->buffer[index], (POINTER)&input[i], inputLen-i);
}
inline void MD5Final(unsigned char digest[16], MD5_CTX *context)
{
unsigned char bits[8];
unsigned int index, padLen;
Encode (bits, context->count, 8);
index = (unsigned int)((context->count[0] >> 3) & 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
MD5Update (context, PADDING, padLen);
MD5Update (context, bits, 8);
Encode (digest, context->state, 16);
memset ((POINTER)context, 0, sizeof (*context));
}
void MD5Digest(char *pszInput, unsigned long nInputSize, char *pszOutPut)
{
MD5_CTX context;
unsigned int len = strlen (pszInput);
MD5Init (&context);
MD5Update (&context, (unsigned char *)pszInput, len);
MD5Final ((unsigned char *)pszOutPut, &context);
}
//生成一个随机字符串
bool getRandStr(char * randStr,int length){
srand(time(0)); //产生随机化种子
int k=rand()%length+1; //随机生成一个字符串的长度,50以内
printf("\nSalt String:");
for(int i=0;i<=k-1;i++)
{
int x,s; //x表示这个字符的ascii码 ,s表示这个字符的大小写
s=rand()%2; //随机使s为1或0,为1就是大写,为0就是小写
if(s==1) //如果s=1
x=rand()%('Z'-'A'+1)+'A'; //将x赋为大写字母的ascii码
else
x=rand()%('z'-'a'+1)+'a'; //如果s=0,x赋为小写字母的ascii码
*(randStr+i)=x;
printf("%c",x); //将x转换为字符输出
}
printf("\n");
return true;
}
int main(int argc, char *argv[])
{
//说明:每次登陆需要发送salt(明文)、用户名(明文)、两次MD5后的消息摘要(md5(md5(密码明文)+salt))
char szDigest[200]={0};//消息摘要为128bits,共16字节,赋初值0
char user_key_temp[200]={0};//缓存密码加密结果和salt,首先要赋初值0,不然会编码错误
char user_key[200] = "songchaochao";//密码
char salt[200] = {"pvVtW7S2wEVULGO05mye"};//salt 长度不超过50
//第一步,用户密码加密MD5
MD5Digest(user_key,strlen(user_key),user_key_temp);
int i;
printf("Md5 Key:");
for (i=0;i<16;i++)
{
printf("%02X",(unsigned char)user_key_temp[i]);
}
printf("\n");
printf("\n");
//第二步,生成一个随机字符串,a-z,A-Z
//getRandStr(salt,50);
printf("salt:");
for (i=0;salt[i];i++)
{
printf("%02X",(unsigned char)salt[i]);
}
printf("\n");
printf("\n");
//第三步,将用户密码MD5加密后的结果和随机字符串连接
strcat(user_key_temp,salt);
printf("Md5 Key+salt:");
for (i=0;user_key_temp[i];i++)
{
printf("%02X",(unsigned char)user_key_temp[i]);
}printf("\n");printf("\n");
//第四步,将连接后的数据进行MD5加密
MD5Digest(user_key_temp,strlen(user_key_temp),szDigest);
printf("\n");
printf("\nEncoded msg:");
for (i=0;i<16;i++)
{
printf("%02X",(unsigned char)szDigest[i]);
}
return 0;
// QApplication a(argc, argv);
// MainWindow w;
// w.show();
// return a.exec();
}
欢迎关注我的微信公众号
互联网矿工