加密什麼加密字元串嗎,我這里有md5的演算法
public final static String MD5(String pwd) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] strTemp = pwd.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
❷ C#實現MD5加密
花了五分鍾就搞定了。
有時間多光顧一下我的博客:http://hi..com/greatzhenren
using System;
using System.Text;
using System.Security.Cryptography;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string inStr;
inStr = "犍為真人";
inStr += "\xa3\xac\xa1\xa3";
inStr += "fdjf,jkgfkl";
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] InBytes = Encoding.GetEncoding("GB2312").GetBytes(inStr);
byte[] OutBytes= md5.ComputeHash(InBytes);
string OutString = "";
for (int i = 0; i < OutBytes.Length; i++)
{
OutString += OutBytes[i].ToString("x2");
}
Console.WriteLine(OutString);
}
}
}
❸ java中md5加密
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class md5 {
public String str;
public void md5s(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
System.out.println("result: " + buf.toString());// 32位的加密
System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String agrs[]) {
md5 md51 = new md5();
md51.md5s("4");//加密4
}
}
❹ java的md5的加密演算法代碼
import java.lang.reflect.*;
/*******************************************************************************
* keyBean 類實現了RSA Data Security, Inc.在提交給IETF 的RFC1321中的keyBean message-digest
* 演算法。
******************************************************************************/
public class keyBean {
/*
* 下面這些S11-S44實際上是一個4*4的矩陣,在原始的C實現中是用#define 實現的, 這里把它們實現成為static
* final是表示了只讀,切能在同一個進程空間內的多個 Instance間共享
*/
static final int S11 = 7;
static final int S12 = 12;
static final int S13 = 17;
static final int S14 = 22;
static final int S21 = 5;
static final int S22 = 9;
static final int S23 = 14;
static final int S24 = 20;
static final int S31 = 4;
static final int S32 = 11;
static final int S33 = 16;
static final int S34 = 23;
static final int S41 = 6;
static final int S42 = 10;
static final int S43 = 15;
static final int S44 = 21;
static final byte[] PADDING = { -128, 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 };
/*
* 下面的三個成員是keyBean計算過程中用到的3個核心數據,在原始的C實現中 被定義到keyBean_CTX結構中
*/
private long[] state = new long[4]; // state (ABCD)
private long[] count = new long[2]; // number of bits, molo 2^64 (lsb
// first)
private byte[] buffer = new byte[64]; // input buffer
/*
* digestHexStr是keyBean的唯一一個公共成員,是最新一次計算結果的 16進制ASCII表示.
*/
public String digestHexStr;
/*
* digest,是最新一次計算結果的2進制內部表示,表示128bit的keyBean值.
*/
private byte[] digest = new byte[16];
/*
* getkeyBeanofStr是類keyBean最主要的公共方法,入口參數是你想要進行keyBean變換的字元串
* 返回的是變換完的結果,這個結果是從公共成員digestHexStr取得的.
*/
public String getkeyBeanofStr(String inbuf) {
keyBeanInit();
keyBeanUpdate(inbuf.getBytes(), inbuf.length());
keyBeanFinal();
digestHexStr = "";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 這是keyBean這個類的標准構造函數,JavaBean要求有一個public的並且沒有參數的構造函數
public keyBean() {
keyBeanInit();
return;
}
/* keyBeanInit是一個初始化函數,初始化核心變數,裝入標準的幻數 */
private void keyBeanInit() {
count[0] = 0L;
count[1] = 0L;
// /* Load magic initialization constants.
state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L;
return;
}
/*
* F, G, H ,I 是4個基本的keyBean函數,在原始的keyBean的C實現中,由於它們是
* 簡單的位運算,可能出於效率的考慮把它們實現成了宏,在java中,我們把它們 實現成了private方法,名字保持了原來C中的。
*/
private long F(long x, long y, long z) {
return (x & y) | ((~x) & z);
}
private long G(long x, long y, long z) {
return (x & z) | (y & (~z));
}
private long H(long x, long y, long z) {
return x ^ y ^ z;
}
private long I(long x, long y, long z) {
return y ^ (x | (~z));
}
/*
* FF,GG,HH和II將調用F,G,H,I進行近一步變換 FF, GG, HH, and II transformations for
* rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent
* recomputation.
*/
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
/*
* keyBeanUpdate是keyBean的主計算過程,inbuf是要變換的位元組串,inputlen是長度,這個
* 函數由getkeyBeanofStr調用,調用之前需要調用keyBeaninit,因此把它設計成private的
*/
private void keyBeanUpdate(byte[] inbuf, int inputLen) {
int i, index, partLen;
byte[] block = new byte[64];
index = (int) (count[0] >>> 3) & 0x3F;
// /* Update number of bits */
if ((count[0] += (inputLen << 3)) < (inputLen << 3))
count[1]++;
count[1] += (inputLen >>> 29);
partLen = 64 - index;
// Transform as many times as possible.
if (inputLen >= partLen) {
keyBeanMemcpy(buffer, inbuf, index, 0, partLen);
keyBeanTransform(buffer);
for (i = partLen; i + 63 < inputLen; i += 64) {
keyBeanMemcpy(block, inbuf, 0, i, 64);
keyBeanTransform(block);
}
index = 0;
} else
i = 0;
// /* Buffer remaining input */
keyBeanMemcpy(buffer, inbuf, index, i, inputLen - i);
}
/*
* keyBeanFinal整理和填寫輸出結果
*/
private void keyBeanFinal() {
byte[] bits = new byte[8];
int index, padLen;
// /* Save number of bits */
Encode(bits, count, 8);
// /* Pad out to 56 mod 64.
index = (int) (count[0] >>> 3) & 0x3f;
padLen = (index < 56) ? (56 - index) : (120 - index);
keyBeanUpdate(PADDING, padLen);
// /* Append length (before padding) */
keyBeanUpdate(bits, 8);
// /* Store state in digest */
Encode(digest, state, 16);
}
/*
* keyBeanMemcpy是一個內部使用的byte數組的塊拷貝函數,從input的inpos開始把len長度的
* 位元組拷貝到output的outpos位置開始
*/
private void keyBeanMemcpy(byte[] output, byte[] input, int outpos,
int inpos, int len) {
int i;
for (i = 0; i < len; i++)
output[outpos + i] = input[inpos + i];
}
/*
* keyBeanTransform是keyBean核心變換程序,有keyBeanUpdate調用,block是分塊的原始位元組
*/
private void keyBeanTransform(byte block[]) {
long a = state[0], b = state[1], c = state[2], d = state[3];
long[] x = new long[16];
Decode(x, block, 64);
/* Round 1 */
a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */
c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
/*
* Encode把long數組按順序拆成byte數組,因為java的long類型是64bit的, 只拆低32bit,以適應原始C實現的用途
*/
private void Encode(byte[] output, long[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (byte) (input[i] & 0xffL);
output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
}
}
/*
* Decode把byte數組按順序合成成long數組,因為java的long類型是64bit的,
* 只合成低32bit,高32bit清零,以適應原始C實現的用途
*/
private void Decode(long[] output, byte[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
| (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
return;
}
/*
* b2iu是我寫的一個把byte按照不考慮正負號的原則的」升位」程序,因為java沒有unsigned運算
*/
public static long b2iu(byte b) {
return b < 0 ? b & 0x7F + 128 : b;
}
/*
* byteHEX(),用來把一個byte類型的數轉換成十六進制的ASCII表示,
* 因為java中的byte的toString無法實現這一點,我們又沒有C語言中的 sprintf(outbuf,"%02X",ib)
*/
public static String byteHEX(byte ib) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] ob = new char[2];
ob[0] = Digit[(ib >>> 4) & 0X0F];
ob[1] = Digit[ib & 0X0F];
String s = new String(ob);
return s;
}
public static void main(String args[]) {
keyBean m = new keyBean();
if (Array.getLength(args) == 0) { // 如果沒有參數,執行標準的Test Suite
System.out.println("keyBean Test suite:");
System.out.println("keyBean(\"):" + m.getkeyBeanofStr(""));
System.out.println("keyBean(\"a\"):" + m.getkeyBeanofStr("a"));
System.out.println("keyBean(\"abc\"):" + m.getkeyBeanofStr("abc"));
System.out.println("keyBean(\"message digest\"):"
+ m.getkeyBeanofStr("message digest"));
System.out.println("keyBean(\"abcdefghijklmnopqrstuvwxyz\"):"
+ m.getkeyBeanofStr("abcdefghijklmnopqrstuvwxyz"));
System.out
.println("keyBean(\"\"):"
+ m
.getkeyBeanofStr(""));
} else
System.out.println("keyBean(" + args[0] + ")="
+ m.getkeyBeanofStr(args[0]));
}
}
❺ 介紹iOS中MD5加密演算法的使用
前言
軟體開發過程中,對數據進行加密是保證數據安全的重要手段,常見的加密有Base64加密和MD5加密。Base64加密是可逆的,MD5加密目前來說一般是不可逆的。
MD5生成的是固定的128bit,即128個0和1的二進制位,而在實際應用開發中,通常是以16進制輸出的,所以正好就是32位的16進制,說白了也就是32個16進制的數字。
MD5主要特點是 不可逆,相同數據的MD5值肯定一樣,不同數據的MD5值不一樣(也不是絕對的,但基本是不能一樣的)。
MD5演算法還具有以下性質:
1、壓縮性:任意長度的數據,算出的MD5值長度都是固定的。
2、容易計算:從原數據計算出MD5值很容易。
3、抗修改性:對原數據進行任何改動,哪怕只修改1個位元組,所得到的MD5值都有很大區別。
4、弱抗碰撞:已知含氏原數據和其MD5值,想找到一個具有相同MD5值的數據(即偽造數據)是非常困難的。
5、強抗碰撞:想緩顫找到兩個不同的數據,使它們具有相同的MD5值,是非常困難的。
6、MD5加密是不可解密的,但是網上有一些解析MD5的,那個相當於一個大型的資料庫,通過匹配MD5去找到原密碼。所以,只要在要加密的字元串前面加上一些字母數字元號或者多次MD5加密,這樣出來的結果一般是解析不出來的。
MD5的應用:
由於MD5加密演算法具有較好的安全性,而且免費,因此該加密演算法被廣泛使用
大多數的'登錄功能向後台提交密碼時都會使用到這種演算法
注意點:
(1)一定要和後台開發人員約定好,MD5加密的位數是16位還是32位(大多數都是32位的),16位的可以通過32位的轉換得到。
(2)MD5加密區分 大小寫,使用時要和後台約定好。
MD5解密:
解密網站:http://www.cmd5.com/
為了讓MD5碼更加安全 涌現了很多其他方法 如加鹽。 鹽要足夠長足夠亂 得到的MD5碼就很難查到。
終端代碼:$ echo -n abc|openssl md5 給字元串abc加密、
蘋果包裝了MD5加密的擾老敗方法,使用起來十分的方便。
#import@interface MD5Encrypt : NSObject// MD5加密/**由於MD5加密是不可逆的,多用來進行驗證*/// 32位小寫+(NSString *)MD5ForLower32Bate:(NSString *)str;// 32位大寫+(NSString *)MD5ForUpper32Bate:(NSString *)str;// 16為大寫+(NSString *)MD5ForUpper16Bate:(NSString *)str;// 16位小寫+(NSString *)MD5ForLower16Bate:(NSString *)str;@end
#import "MD5Encrypt.h"#import@implementation MD5Encrypt#pragma mark - 32位 小寫+(NSString *)MD5ForLower32Bate:(NSString *)str{ //要進行UTF8的轉碼 const char* input = [str UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(input, (CC_LONG)strlen(input), result); NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { [digest appendFormat:@"%02x", result[i]]; } return digest;}#pragma mark - 32位 大寫+(NSString *)MD5ForUpper32Bate:(NSString *)str{ //要進行UTF8的轉碼 const char* input = [str UTF8String]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(input, (CC_LONG)strlen(input), result); NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) { [digest appendFormat:@"%02X", result[i]]; } return digest;}#pragma mark - 16位 大寫+(NSString *)MD5ForUpper16Bate:(NSString *)str{ NSString *md5Str = [self MD5ForUpper32Bate:str]; NSString *string; for (int i=0; i<24; i++) { string=[md5Str substringWithRange:NSMakeRange(8, 16)]; } return string;}#pragma mark - 16位 小寫+(NSString *)MD5ForLower16Bate:(NSString *)str{ NSString *md5Str = [self MD5ForLower32Bate:str]; NSString *string; for (int i=0; i<24; i++) { string=[md5Str substringWithRange:NSMakeRange(8, 16)]; } return string;}@end
❻ 可變MD5加密(Java實現)
可變在這里含義很簡單 就是最終的加密結果是可變的 而非必需按標准MD 加密實現 Java類庫security中的MessageDigest類就提供了MD 加密的支持 實現起來非常方便 為了實現更多效果 我們可以如下設計MD 工具類
Java代碼
package ** ** util;
import java security MessageDigest;
/**
* 標准MD 加密方法 使用java類庫的security包的MessageDigest類處理
* @author Sarin
*/
public class MD {
/**
* 獲得MD 加密密碼的方法
*/
public static String getMD ofStr(String origString) {
String origMD = null;
try {
MessageDigest md = MessageDigest getInstance( MD );
byte[] result = md digest(origString getBytes());
origMD = byteArray HexStr(result);
} catch (Exception e) {
e printStackTrace();
}
return origMD ;
}
/**
* 處理位元組數組得到MD 密碼的方法
*/
private static String byteArray HexStr(byte[] bs) {
StringBuffer *** = new StringBuffer();
for (byte b : bs) {
*** append(byte HexStr(b));
}
return *** toString();
}
/**
* 位元組標准移位轉十六進制方法
*/
private static String byte HexStr(byte b) {
String hexStr = null;
int n = b;
if (n < ) {
//若需要自定義加密 請修改這個移位演算法即可
n = b & x F + ;
}
hexStr = Integer toHexString(n / ) + Integer toHexString(n % );
return hexStr toUpperCase();
}
/**
* 提供一個MD 多次加密方法
*/
public static String getMD ofStr(String origString int times) {
String md = getMD ofStr(origString);
for (int i = ; i < times ; i++) {
md = getMD ofStr(md );
}
return getMD ofStr(md );
}
/**
* 密碼驗證方法
*/
public static boolean verifyPassword(String inputStr String MD Code) {
return getMD ofStr(inputStr) equals(MD Code);
}
/**
* 重載一個多次加密時的密碼驗證方法
*/
public static boolean verifyPassword(String inputStr String MD Code int times) {
return getMD ofStr(inputStr times) equals(MD Code);
}
/**
* 提供一個測試的主函數
*/
public static void main(String[] args) {
System out println( : + getMD ofStr( ));
System out println( : + getMD ofStr( ));
System out println( sarin: + getMD ofStr( sarin ));
System out println( : + getMD ofStr( ));
}
}
可以看出實現的過程非常簡單 因為由java類庫提供了處理支持 但是要清楚的是這種方式產生的密碼不是標準的MD 碼 它需要進行移位處理才能得到標准MD 碼 這個程序的關鍵之處也在這了 怎麼可變?調整移位演算法不就可變了么!不進行移位 也能夠得到 位的密碼 這就不是標准加密了 只要加密和驗證過程使用相同的演算法就可以了
MD 加密還是很安全的 像CMD 那些窮舉破解的只是針對標准MD 加密的結果進行的 如果自定義移位演算法後 它還有效么?可以說是無解的了 所以MD 非常安全可靠
為了更可變 還提供了多次加密的方法 可以在MD 基礎之上繼續MD 就是對 位的第一次加密結果再MD 恩 這樣去破解?沒有任何意義
這樣在MIS系統中使用 安全可靠 歡迎交流 希望對使用者有用
我們最後看看由MD 加密演算法實現的類 那是非常龐大的
Java代碼
import java lang reflect *;
/**
* **********************************************
* md 類實現了RSA Data Security Inc 在提交給IETF
* 的RFC 中的MD message digest 演算法
* ***********************************************
*/
public class MD {
/* 下面這些S S 實際上是一個 * 的矩陣 在原始的C實現中是用#define 實現的
這里把它們實現成為static final是表示了只讀 切能在同一個進程空間內的多個
Instance間共享*/
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final byte[] PADDING = {
};
/* 下面的三個成員是MD 計算過程中用到的 個核心數據 在原始的C實現中
被定義到MD _CTX結構中
*/
private long[] state = new long[ ]; // state (ABCD)
private long[] count = new long[ ]; // number of bits molo ^ (l *** first)
private byte[] buffer = new byte[ ]; // input buffer
/* digestHexStr是MD 的唯一一個公共成員 是最新一次計算結果的
進制ASCII表示
*/
public String digestHexStr;
/* digest 是最新一次計算結果的 進制內部表示 表示 bit的MD 值
*/
private byte[] digest = new byte[ ];
/*
getMD ofStr是類MD 最主要的公共方法 入口參數是你想要進行MD 變換的字元串
返回的是變換完的結果 這個結果是從公共成員digestHexStr取得的.
*/
public String getMD ofStr(String inbuf) {
md Init();
md Update(inbuf getBytes() inbuf length());
md Final();
digestHexStr = ;
for (int i = ; i < ; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 這是MD 這個類的標准構造函數 JavaBean要求有一個public的並且沒有參數的構造函數
public MD () {
md Init();
return;
}
/* md Init是一個初始化函數 初始化核心變數 裝入標準的幻數 */
private void md Init() {
count[ ] = L;
count[ ] = L;
///* Load magic initialization constants
state[ ] = x L;
state[ ] = xefcdab L;
state[ ] = x badcfeL;
state[ ] = x L;
return;
}
/* F G H I 是 個基本的MD 函數 在原始的MD 的C實現中 由於它們是
簡單的位運算 可能出於效率的考慮把它們實現成了宏 在java中 我們把它們
實現成了private方法 名字保持了原來C中的 */
private long F(long x long y long z) {
return (x & y) | ((~x) & z);
}
private long G(long x long y long z) {
return (x & z) | (y & (~z));
}
private long H(long x long y long z) {
return x ^ y ^ z;
}
private long I(long x long y long z) {
return y ^ (x | (~z));
}
/*
FF GG HH和II將調用F G H I進行近一步變換
FF GG HH and II transformations for rounds and
Rotation is separate from addition to prevent reputation
*/
private long FF(long a long b long c long d long x long s long ac) {
a += F(b c d) + x + ac;
a = ((int) a << s) | ((int) a >>> ( s));
a += b;
return a;
}
private long GG(long a long b long c long d long x long s long ac) {
a += G(b c d) + x + ac;
a = ((int) a << s) | ((int) a >>> ( s));
a += b;
return a;
}
private long HH(long a long b long c long d long x long s long ac) {
a += H(b c d) + x + ac;
a = ((int) a << s) | ((int) a >>> ( s));
a += b;
return a;
}
private long II(long a long b long c long d long x long s long ac) {
a += I(b c d) + x + ac;
a = ((int) a << s) | ((int) a >>> ( s));
a += b;
return a;
}
/*
md Update是MD 的主計算過程 inbuf是要變換的位元組串 inputlen是長度 這個
函數由getMD ofStr調用 調用之前需要調用md init 因此把它設計成private的
*/
private void md Update(byte[] inbuf int inputLen) {
int i index partLen;
byte[] block = new byte[ ];
index = (int) (count[ ] >>> ) & x F;
// /* Update number of bits */
if ((count[ ] += (inputLen << )) < (inputLen << ))
count[ ]++;
count[ ] += (inputLen >>> );
partLen = index;
// Transform as many times as possible
if (inputLen >= partLen) {
md Memcpy(buffer inbuf index partLen);
md Transform(buffer);
for (i = partLen; i + < inputLen; i += ) {
md Memcpy(block inbuf i );
md Transform(block);
}
index = ;
} else
i = ;
///* Buffer remaining input */
md Memcpy(buffer inbuf index i inputLen i);
}
/*
md Final整理和填寫輸出結果
*/
private void md Final() {
byte[] bits = new byte[ ];
int index padLen;
///* Save number of bits */
Encode(bits count );
///* Pad out to mod
index = (int) (count[ ] >>> ) & x f;
padLen = (index < ) ? ( index) : ( index);
md Update(PADDING padLen);
///* Append length (before padding) */
md Update(bits );
///* Store state in digest */
Encode(digest state );
}
/* md Memcpy是一個內部使用的byte數組的塊拷貝函數 從input的inpos開始把len長度的
位元組拷貝到output的outpos位置開始
*/
private void md Memcpy(byte[] output byte[] input int outpos int inpos int len) {
int i;
for (i = ; i < len; i++)
output[outpos + i] = input[inpos + i];
}
/*
md Transform是MD 核心變換程序 有md Update調用 block是分塊的原始位元組
*/
private void md Transform(byte block[]) {
long a = state[ ] b = state[ ] c = state[ ] d = state[ ];
long[] x = new long[ ];
Decode(x block );
/* Round */
a = FF(a b c d x[ ] S xd aa L); /* */
d = FF(d a b c x[ ] S xe c b L); /* */
c = FF(c d a b x[ ] S x dbL); /* */
b = FF(b c d a x[ ] S xc bdceeeL); /* */
a = FF(a b c d x[ ] S xf c fafL); /* */
d = FF(d a b c x[ ] S x c aL); /* */
c = FF(c d a b x[ ] S xa L); /* */
b = FF(b c d a x[ ] S xfd L); /* */
a = FF(a b c d x[ ] S x d L); /* */
d = FF(d a b c x[ ] S x b f afL); /* */
c = FF(c d a b x[ ] S xffff bb L); /* */
b = FF(b c d a x[ ] S x cd beL); /* */
a = FF(a b c d x[ ] S x b L); /* */
d = FF(d a b c x[ ] S xfd L); /* */
c = FF(c d a b x[ ] S xa eL); /* */
b = FF(b c d a x[ ] S x b L); /* */
/* Round */
a = GG(a b c d x[ ] S xf e L); /* */
d = GG(d a b c x[ ] S xc b L); /* */
c = GG(c d a b x[ ] S x e a L); /* */
b = GG(b c d a x[ ] S xe b c aaL); /* */
a = GG(a b c d x[ ] S xd f dL); /* */
d = GG(d a b c x[ ] S x L); /* */
c = GG(c d a b x[ ] S xd a e L); /* */
b = GG(b c d a x[ ] S xe d fbc L); /* */
a = GG(a b c d x[ ] S x e cde L); /* */
d = GG(d a b c x[ ] S xc d L); /* */
c = GG(c d a b x[ ] S xf d d L); /* */
b = GG(b c d a x[ ] S x a edL); /* */
a = GG(a b c d x[ ] S xa e e L); /* */
d = GG(d a b c x[ ] S xfcefa f L); /* */
c = GG(c d a b x[ ] S x f d L); /* */
b = GG(b c d a x[ ] S x d a c aL); /* */
/* Round */
a = HH(a b c d x[ ] S xfffa L); /* */
d = HH(d a b c x[ ] S x f L); /* */
c = HH(c d a b x[ ] S x d d L); /* */
b = HH(b c d a x[ ] S xfde cL); /* */
a = HH(a b c d x[ ] S xa beea L); /* */
d = HH(d a b c x[ ] S x bdecfa L); /* */
c = HH(c d a b x[ ] S xf bb b L); /* */
b = HH(b c d a x[ ] S xbebfbc L); /* */
a = HH(a b c d x[ ] S x b ec L); /* */
d = HH(d a b c x[ ] S xeaa faL); /* */
c = HH(c d a b x[ ] S xd ef L); /* */
b = HH(b c d a x[ ] S x d L); /* */
a = HH(a b c d x[ ] S xd d d L); /* */
d = HH(d a b c x[ ] S xe db e L); /* */
c = HH(c d a b x[ ] S x fa cf L); /* */
b = HH(b c d a x[ ] S xc ac L); /* */
/* Round */
a = II(a b c d x[ ] S xf L); /* */
d = II(d a b c x[ ] S x aff L); /* */
c = II(c d a b x[ ] S xab a L); /* */
b = II(b c d a x[ ] S xfc a L); /* */
a = II(a b c d x[ ] S x b c L); /* */
d = II(d a b c x[ ] S x f ccc L); /* */
c = II(c d a b x[ ] S xffeff dL); /* */
b = II(b c d a x[ ] S x dd L); /* */
a = II(a b c d x[ ] S x fa e fL); /* */
d = II(d a b c x[ ] S xfe ce e L); /* */
c = II(c d a b x[ ] S xa L); /* */
b = II(b c d a x[ ] S x e a L); /* */
a = II(a b c d x[ ] S xf e L); /* */
d = II(d a b c x[ ] S xbd af L); /* */
c = II(c d a b x[ ] S x ad d bbL); /* */
b = II(b c d a x[ ] S xeb d L); /* */
state[ ] += a;
state[ ] += b;
state[ ] += c;
state[ ] += d;
}
/*Encode把long數組按順序拆成byte數組 因為java的long類型是 bit的
只拆低 bit 以適應原始C實現的用途
*/
private void Encode(byte[] output long[] input int len) {
int i j;
for (i = j = ; j < len; i++ j += ) {
output[j] = (byte) (input[i] & xffL);
output[j + ] = (byte) ((input[i] >>> ) & xffL);
output[j + ] = (byte) ((input[i] >>> ) & xffL);
output[j + ] = (byte) ((input[i] >>> ) & xffL);
}
}
/*Decode把byte數組按順序合成成long數組 因為java的long類型是 bit的
只合成低 bit 高 bit清零 以適應原始C實現的用途
*/
private void Decode(long[] output byte[] input int len) {
int i j;
for (i = j = ; j < len; i++ j += )
output[i] = b iu(input[j]) | (b iu(input[j + ]) << ) | (b iu(input[j + ]) << )
| (b iu(input[j + ]) << );
return;
}
/*
b iu是我寫的一個把byte按照不考慮正負號的原則的"升位"程序 因為java沒有unsigned運算
*/
public static long b iu(byte b) {
return b < ? b & x F + : b;
}
/*byteHEX() 用來把一個byte類型的數轉換成十六進制的ASCII表示
因為java中的byte的toString無法實現這一點 我們又沒有C語言中的
sprintf(outbuf % X ib)
*/
public static String byteHEX(byte ib) {
char[] Digit = { A B C D E F };
char[] ob = new char[ ];
ob[ ] = Digit[(ib >>> ) & X F];
ob[ ] = Digit[ib & X F];
String s = new String(ob);
return s;
}
public static void main(String args[]) {
MD m = new MD ();
if (Array getLength(args) == ) { //如果沒有參數 執行標準的Test Suite
System out println( MD Test suite: );
System out println( MD ( ): + m getMD ofStr( ));
System out println( MD ( a ): + m getMD ofStr( a ));
System out println( MD ( abc ): + m getMD ofStr( abc ));
System out println( MD ( ): + m getMD ofStr( ));
System out println( MD ( ): + m getMD ofStr( ));
System out println( MD ( message digest ): + m getMD ofStr( message digest ));
System out println( MD ( abcdefghijklmnopqrstuvwxyz ): + m getMD ofStr( abcdefghijklmnopqrstuvwxyz ));
System out println( MD ( ):
+ m getMD ofStr( ));
} else
System out println( MD ( + args[ ] + )= + m getMD ofStr(args[ ]));
}
lishixin/Article/program/Java/hx/201311/26604
❼ md5 演算法程序+詳細注釋,高分求教!
MD5加密演算法簡介
一、綜述
MD5的全稱是message-digest algorithm 5(信息-摘要演算法),在90年代初由mit laboratory for computer science和rsa data security inc的ronald l. rivest開發出來,經md2、md3和md4發展而來。它的作用是讓大容量信息在用數字簽名軟體簽署私人密匙前被"壓縮"成一種保密的格式(就是把一 個任意長度的位元組串變換成一定長的大整數)。不管是md2、md4還是md5,它們都需要獲得一個隨機長度的信息並產生一個128位的信息摘要。雖然這些 演算法的結構或多或少有些相似,但md2的設計與md4和md5完全不同,那是因為md2是為8位機器做過設計優化的,而md4和md5卻是面向32位的電 腦。這三個演算法的描述和c語言源代碼在internet rfcs 1321中有詳細的描述(http://www.ietf.org/rfc/rfc1321.txt),這是一份最權威的文檔,由ronald l. rivest在1992年8月向ieft提交。
rivest在1989年開發出md2演算法。在這個演算法中,首先對信 息進行數據補位,使信息的位元組長度是16的倍數。然後,以一個16位的檢驗和追加到信息末尾。並且根據這個新產生的信息計算出散列值。後來,rogier 和chauvaud發現如果忽略了檢驗和將產生md2沖突。md2演算法的加密後結果是唯一的--既沒有重復。
為了加強演算法的安全性, rivest在1990年又開發出md4演算法。md4演算法同樣需要填補信息以確保信息的位元組長度加上448後能被512整除(信息位元組長度mod 512 = 448)。然後,一個以64位二進製表示的信息的最初長度被添加進來。信息被處理成512位damg?rd/merkle迭代結構的區塊,而且每個區塊要 通過三個不同步驟的處理。den boer和bosselaers以及其他人很快的發現了攻擊md4版本中第一步和第三步的漏洞。dobbertin向大家演示了如何利用一部普通的個人電 腦在幾分鍾內找到md4完整版本中的沖突(這個沖突實際上是一種漏洞,它將導致對不同的內容進行加密卻可能得到相同的加密後結果)。毫無疑問,md4就此 被淘汰掉了。
盡管md4演算法在安全上有個這么大的漏洞,但它對在其後才被開發出來的好幾種信息安全加密演算法的出現卻有著不可忽視的引導作用。除了md5以外,其中比較有名的還有sha-1、ripe-md以及haval等。
一年以後,即1991年,rivest開發出技術上更為趨近成熟的md5演算法。它在md4的基礎上增加了"安全-帶子"(safety-belts)的 概念。雖然md5比md4稍微慢一些,但卻更為安全。這個演算法很明顯的由四個和md4設計有少許不同的步驟組成。在md5演算法中,信息-摘要的大小和填充 的必要條件與md4完全相同。den boer和bosselaers曾發現md5演算法中的假沖突(pseudo-collisions),但除此之外就沒有其他被發現的加密後結果了。
van oorschot和wiener曾經考慮過一個在散列中暴力搜尋沖突的函數(brute-force hash function),而且他們猜測一個被設計專門用來搜索md5沖突的機器(這台機器在1994年的製造成本大約是一百萬美元)可以平均每24天就找到一 個沖突。但單從1991年到2001年這10年間,竟沒有出現替代md5演算法的md6或被叫做其他什麼名字的新演算法這一點,我們就可以看出這個瑕疵並沒有 太多的影響md5的安全性。上面所有這些都不足以成為md5的在實際應用中的問題。並且,由於md5演算法的使用不需要支付任何版權費用的,所以在一般的情 況下(非絕密應用領域。但即便是應用在絕密領域內,md5也不失為一種非常優秀的中間技術),md5怎麼都應該算得上是非常安全的了。
二、演算法的應用
md5的典型應用是對一段信息(message)產生信息摘要(message-digest),以防止被篡改。比如,在unix下有很多軟體在下載的時候都有一個文件名相同,文件擴展名為.md5的文件,在這個文件中通常只有一行文本,大致結構如:
md5 (tanajiya.tar.gz) =
這就是tanajiya.tar.gz文件的數字簽名。md5將整個文件當作一個大文本信息,通過其不可逆的字元串變換演算法,產生了這個唯一的md5信 息摘要。如果在以後傳播這個文件的過程中,無論文件的內容發生了任何形式的改變(包括人為修改或者下載過程中線路不穩定引起的傳輸錯誤等),只要你對這個 文件重新計算md5時就會發現信息摘要不相同,由此可以確定你得到的只是一個不正確的文件。如果再有一個第三方的認證機構,用md5還可以防止文件作者的 "抵賴",這就是所謂的數字簽名應用。
md5還廣泛用於加密和解密技術上。比如在unix系統中用戶的密碼就是以md5(或其它類似的算 法)經加密後存儲在文件系統中。當用戶登錄的時候,系統把用戶輸入的密碼計算成md5值,然後再去和保存在文件系統中的md5值進行比較,進而確定輸入的 密碼是否正確。通過這樣的步驟,系統在並不知道用戶密碼的明碼的情況下就可以確定用戶登錄系統的合法性。這不但可以避免用戶的密碼被具有系統管理員許可權的 用戶知道,而且還在一定程度上增加了密碼被破解的難度。
正是因為這個原因,現在被黑客使用最多的一種破譯密碼的方法就是一種被稱為"跑字 典"的方法。有兩種方法得到字典,一種是日常搜集的用做密碼的字元串表,另一種是用排列組合方法生成的,先用md5程序計算出這些字典項的md5值,然後 再用目標的md5值在這個字典中檢索。我們假設密碼的最大長度為8位位元組(8 bytes),同時密碼只能是字母和數字,共26+26+10=62個字元,排列組合出的字典的項數則是p(62,1)+p(62,2)….+p (62,8),那也已經是一個很天文的數字了,存儲這個字典就需要tb級的磁碟陣列,而且這種方法還有一個前提,就是能獲得目標賬戶的密碼md5值的情況 下才可以。這種加密技術被廣泛的應用於unix系統中,這也是為什麼unix系統比一般操作系統更為堅固一個重要原因。
三、演算法描述
對md5演算法簡要的敘述可以為:md5以512位分組來處理輸入的信息,且每一分組又被劃分為16個32位子分組,經過了一系列的處理後,演算法的輸出由四個32位分組組成,將這四個32位分組級聯後將生成一個128位散列值。
在md5演算法中,首先需要對信息進行填充,使其位元組長度對512求余的結果等於448。因此,信息的位元組長度(bits length)將被擴展至n*512+448,即n*64+56個位元組(bytes),n為一個正整數。填充的方法如下,在信息的後面填充一個1和無數個 0,直到滿足上面的條件時才停止用0對信息的填充。然後,在在這個結果後面附加一個以64位二進製表示的填充前信息長度。經過這兩步的處理,現在的信息字 節長度=n*512+448+64=(n+1)*512,即長度恰好是512的整數倍。這樣做的原因是為滿足後面處理中對信息長度的要求。
md5中有四個32位被稱作鏈接變數(chaining variable)的整數參數,他們分別為:a=0x01234567,b=0x89abcdef,c=0xfedcba98,d=0x76543210。
當設置好這四個鏈接變數後,就開始進入演算法的四輪循環運算。循環的次數是信息中512位信息分組的數目。
將上面四個鏈接變數復制到另外四個變數中:a到a,b到b,c到c,d到d。
主循環有四輪(md4隻有三輪),每輪循環都很相似。第一輪進行16次操作。每次操作對a、b、c和d中的其中三個作一次非線性函數運算,然後將所得結 果加上第四個變數,文本的一個子分組和一個常數。再將所得結果向右環移一個不定的數,並加上a、b、c或d中之一。最後用該結果取代a、b、c或d中之 一。
以一下是每次操作中用到的四個非線性函數(每輪一個)。
f(x,y,z) =(x&y)|((~x)&z)
g(x,y,z) =(x&z)|(y&(~z))
h(x,y,z) =x^y^z
i(x,y,z)=y^(x|(~z))
(&是與,|是或,~是非,^是異或)
這四個函數的說明:如果x、y和z的對應位是獨立和均勻的,那麼結果的每一位也應是獨立和均勻的。
f是一個逐位運算的函數。即,如果x,那麼y,否則z。函數h是逐位奇偶操作符。
假設mj表示消息的第j個子分組(從0到15),
<< ff(a,b,c,d,mj,s,ti) 表示 a=b+((a+(f(b,c,d)+mj+ti)
<< gg(a,b,c,d,mj,s,ti) 表示 a=b+((a+(g(b,c,d)+mj+ti)
<< hh(a,b,c,d,mj,s,ti) 表示 a=b+((a+(h(b,c,d)+mj+ti)
<< ii(a,b,c,d,mj,s,ti) 表示 a=b+((a+(i(b,c,d)+mj+ti)
<< 這四輪(64步)是:
第一輪
ff(a,b,c,d,m0,7,0xd76aa478)
ff(d,a,b,c,m1,12,0xe8c7b756)
ff(c,d,a,b,m2,17,0x242070db)
ff(b,c,d,a,m3,22,0xc1bdceee)
ff(a,b,c,d,m4,7,0xf57c0faf)
ff(d,a,b,c,m5,12,0x4787c62a)
ff(c,d,a,b,m6,17,0xa8304613)
ff(b,c,d,a,m7,22,0xfd469501)
ff(a,b,c,d,m8,7,0x698098d8)
ff(d,a,b,c,m9,12,0x8b44f7af)
ff(c,d,a,b,m10,17,0xffff5bb1)
ff(b,c,d,a,m11,22,0x895cd7be)
ff(a,b,c,d,m12,7,0x6b901122)
ff(d,a,b,c,m13,12,0xfd987193)
ff(c,d,a,b,m14,17,0xa679438e)
ff(b,c,d,a,m15,22,0x49b40821)
第二輪
gg(a,b,c,d,m1,5,0xf61e2562)
gg(d,a,b,c,m6,9,0xc040b340)
gg(c,d,a,b,m11,14,0x265e5a51)
gg(b,c,d,a,m0,20,0xe9b6c7aa)
gg(a,b,c,d,m5,5,0xd62f105d)
gg(d,a,b,c,m10,9,0x02441453)
gg(c,d,a,b,m15,14,0xd8a1e681)
gg(b,c,d,a,m4,20,0xe7d3fbc8)
gg(a,b,c,d,m9,5,0x21e1cde6)
gg(d,a,b,c,m14,9,0xc33707d6)
gg(c,d,a,b,m3,14,0xf4d50d87)
gg(b,c,d,a,m8,20,0x455a14ed)
gg(a,b,c,d,m13,5,0xa9e3e905)
gg(d,a,b,c,m2,9,0xfcefa3f8)
gg(c,d,a,b,m7,14,0x676f02d9)
gg(b,c,d,a,m12,20,0x8d2a4c8a)
第三輪
hh(a,b,c,d,m5,4,0xfffa3942)
hh(d,a,b,c,m8,11,0x8771f681)
hh(c,d,a,b,m11,16,0x6d9d6122)
hh(b,c,d,a,m14,23,0xfde5380c)
hh(a,b,c,d,m1,4,0xa4beea44)
hh(d,a,b,c,m4,11,0x4bdecfa9)
hh(c,d,a,b,m7,16,0xf6bb4b60)
hh(b,c,d,a,m10,23,0xbebfbc70)
hh(a,b,c,d,m13,4,0x289b7ec6)
hh(d,a,b,c,m0,11,0xeaa127fa)
hh(c,d,a,b,m3,16,0xd4ef3085)
hh(b,c,d,a,m6,23,0x04881d05)
hh(a,b,c,d,m9,4,0xd9d4d039)
hh(d,a,b,c,m12,11,0xe6db99e5)
hh(c,d,a,b,m15,16,0x1fa27cf8)
hh(b,c,d,a,m2,23,0xc4ac5665)
第四輪
ii(a,b,c,d,m0,6,0xf4292244)
ii(d,a,b,c,m7,10,0x432aff97)
ii(c,d,a,b,m14,15,0xab9423a7)
ii(b,c,d,a,m5,21,0xfc93a039)
ii(a,b,c,d,m12,6,0x655b59c3)
ii(d,a,b,c,m3,10,0x8f0ccc92)
ii(c,d,a,b,m10,15,0xffeff47d)
ii(b,c,d,a,m1,21,0x85845dd1)
ii(a,b,c,d,m8,6,0x6fa87e4f)
ii(d,a,b,c,m15,10,0xfe2ce6e0)
ii(c,d,a,b,m6,15,0xa3014314)
ii(b,c,d,a,m13,21,0x4e0811a1)
ii(a,b,c,d,m4,6,0xf7537e82)
ii(d,a,b,c,m11,10,0xbd3af235)
ii(c,d,a,b,m2,15,0x2ad7d2bb)
ii(b,c,d,a,m9,21,0xeb86d391)
常數ti可以如下選擇:
在第i步中,ti是4294967296*abs(sin(i))的整數部分,i的單位是弧度。(4294967296等於2的32次方)
所有這些完成之後,將a、b、c、d分別加上a、b、c、d。然後用下一分組數據繼續運行演算法,最後的輸出是a、b、c和d的級聯。
當你按照我上面所說的方法實現md5演算法以後,你可以用以下幾個信息對你做出來的程序作一個簡單的測試,看看程序有沒有錯誤。
md5 ("") =
md5 ("a") =
md5 ("abc") =
md5 ("message digest") =
md5 ("abcdefghijklmnopqrstuvwxyz") =
md5 ("") =
md5 ("1234567890") =
如果你用上面的信息分別對你做的md5演算法實例做測試,最後得出的結論和標准答案完全一樣,那我就要在這里象你道一聲祝賀了。要知道,我的程序在第一次編譯成功的時候是沒有得出和上面相同的結果的。
四、MD5的安全性
md5相對md4所作的改進:
1. 增加了第四輪;
2. 每一步均有唯一的加法常數;
3. 為減弱第二輪中函數g的對稱性從(x&y)|(x&z)|(y&z)變為(x&z)|(y&(~z));
4. 第一步加上了上一步的結果,這將引起更快的雪崩效應;
5. 改變了第二輪和第三輪中訪問消息子分組的次序,使其更不相似;
6. 近似優化了每一輪中的循環左移位移量以實現更快的雪崩效應。各輪的位移量互不相同。
❽ 求MD5加密演算法的C語言源碼
(轉載) #include <stdio.h> #include <stdlib.h> #include <memory.h> #include <time.h> #include <errno.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include "../md5/md5.h" #define T1 0xd76aa478 #define T2 0xe8c7b756 #define T3 0x242070db #define T4 0xc1bdceee #define T5 0xf57c0faf #define T6 0x4787c62a #define T7 0xa8304613 #define T8 0xfd469501 #define T9 0x698098d8 #define T10 0x8b44f7af #define T11 0xffff5bb1 #define T12 0x895cd7be #define T13 0x6b901122 #define T14 0xfd987193 #define T15 0xa679438e #define T16 0x49b40821 #define T17 0xf61e2562 #define T18 0xc040b340 #define T19 0x265e5a51 #define T20 0xe9b6c7aa #define T21 0xd62f105d #define T22 0x02441453 #define T23 0xd8a1e681 #define T24 0xe7d3fbc8 #define T25 0x21e1cde6 #define T26 0xc33707d6 #define T27 0xf4d50d87 #define T28 0x455a14ed #define T29 0xa9e3e905 #define T30 0xfcefa3f8 #define T31 0x676f02d9 #define T32 0x8d2a4c8a #define T33 0xfffa3942 #define T34 0x8771f681 #define T35 0x6d9d6122 #define T36 0xfde5380c #define T37 0xa4beea44 #define T38 0x4bdecfa9 #define T39 0xf6bb4b60 #define T40 0xbebfbc70 #define T41 0x289b7ec6 #define T42 0xeaa127fa #define T43 0xd4ef3085 #define T44 0x04881d05 #define T45 0xd9d4d039 #define T46 0xe6db99e5 #define T47 0x1fa27cf8 #define T48 0xc4ac5665 #define T49 0xf4292244 #define T50 0x432aff97 #define T51 0xab9423a7 #define T52 0xfc93a039 #define T53 0x655b59c3 #define T54 0x8f0ccc92 #define T55 0xffeff47d #define T56 0x85845dd1 #define T57 0x6fa87e4f #define T58 0xfe2ce6e0 #define T59 0xa3014314 #define T60 0x4e0811a1 #define T61 0xf7537e82 #define T62 0xbd3af235 #define T63 0x2ad7d2bb #define T64 0xeb86d391 static void md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/) { md5_word_t a = pms->abcd[0], b = pms->abcd[1], c = pms->abcd[2], d = pms->abcd[3]; md5_word_t t; #ifndef ARCH_IS_BIG_ENDIAN # define ARCH_IS_BIG_ENDIAN 1 /* slower, default implementation */ #endif #if ARCH_IS_BIG_ENDIAN /* * On big-endian machines, we must arrange the bytes in the right * order. (This also works on machines of unknown byte order.) */ md5_word_t X[16]; const md5_byte_t *xp = data; int i; for (i = 0; i < 16; i, xp = 4) X[i] = xp[0] (xp[1] << 8) (xp[2] << 16) (xp[3] << 24); #else /* !ARCH_IS_BIG_ENDIAN */ /* * On little-endian machines, we can process properly aligned data * without ing it. */ md5_word_t xbuf[16]; const md5_word_t *X; if (!((data - (const md5_byte_t *)0) & 3)) { /* data are properly aligned */ X = (const md5_word_t *)data; } else { /* not aligned */ memcpy(xbuf, data, 64); X = xbuf; } #endif #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) /* Round 1. */ /* Let [abcd k s i] denote the operation a = b ((a F(b,c,d) X[k] T[i]) <<< s). */ #define F(x, y, z) (((x) & (y)) | (~(x) & (z))) #define SET(a, b, c, d, k, s, Ti)\ t = a F(b,c,d) X[k] Ti;\ a = ROTATE_LEFT(t, s) b /* Do the following 16 operations. */ SET(a, b, c, d, 0, 7, T1); SET(d, a, b, c, 1, 12, T2); SET(c, d, a, b, 2, 17, T3); SET(b, c, d, a, 3, 22, T4); SET(a, b, c, d, 4, 7, T5); SET(d, a, b, c, 5, 12, T6); SET(c, d, a, b, 6, 17, T7); SET(b, c, d, a, 7, 22, T8); SET(a, b, c, d, 8, 7, T9); SET(d, a, b, c, 9, 12, T10); SET(c, d, a, b, 10, 17, T11); SET(b, c, d, a, 11, 22, T12); SET(a, b, c, d, 12, 7, T13); SET(d, a, b, c, 13, 12, T14); SET(c, d, a, b, 14, 17, T15); SET(b, c, d, a, 15, 22, T16); #undef SET /* Round 2. */ /* Let [abcd k s i] denote the operation a = b ((a G(b,c,d) X[k] T[i]) <<< s). */ #define G(x, y, z) (((x) & (z)) | ((y) & ~(z))) #define SET(a, b, c, d, k, s, Ti)\ t = a G(b,c,d) X[k] Ti;\ a = ROTATE_LEFT(t, s) b /* Do the following 16 operations. */ SET(a, b, c, d, 1, 5, T17); SET(d, a, b, c, 6, 9, T18); SET(c, d, a, b, 11, 14, T19); SET(b, c, d, a, 0, 20, T20); SET(a, b, c, d, 5, 5, T21); SET(d, a, b, c, 10, 9, T22); SET(c, d, a, b, 15, 14, T23); SET(b, c, d, a, 4, 20, T24); SET(a, b, c, d, 9, 5, T25); SET(d, a, b, c, 14, 9, T26); SET(c, d, a, b, 3, 14, T27); SET(b, c, d, a, 8, 20, T28); SET(a, b, c, d, 13, 5, T29); SET(d, a, b, c, 2, 9, T30); SET(c, d, a, b, 7, 14, T31); SET(b, c, d, a, 12, 20, T32); #undef SET
❾ 跪求md5演算法的可執行程序,最好帶上流程圖和源代碼,謝了~
1. Java版MD5
MD5Util.java
[java] view plain
package com.cncounter.util.common;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Java消息摘要演算法 MD5 工具類,其實其他摘要演算法的實現也類似
*/
public class MD5Util {
/**
* 對文本執行 md5 摘要加密, 此演算法與 mysql,JavaScript生成的md5摘要進行過一致性對比.
* @param plainText
* @return 返回值中的字母為小寫
*/
public static String md5(String plainText) {
if (null == plainText) {
plainText = "";
}
String MD5Str = "";
try {
// JDK 6 支持以下6種消息摘要演算法,不區分大小寫
// md5,sha(sha-1),md2,sha-256,sha-384,sha-512
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuilder builder = new StringBuilder(32);
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
builder.append("0");
builder.append(Integer.toHexString(i));
}
MD5Str = builder.toString();
// LogUtil.println("result: " + buf.toString());// 32位的加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return MD5Str;
}
// 一個簡版測試
public static void main(String[] args) {
String m1 = md5("1");
String m2 = md5(m1);
/* 輸出為
* m1=
* m2=
*/
System.out.println("m1="+m1);
System.out.println("m2="+m2);
}
}
2. MySQL版MD5
MySQL直接支持 md5函數調用
[sql] view plain
select md5('1') as m1, md5(md5('1')) as m2;
執行結果為:
[plain] view plain
MariaDB [(none)]> select md5('1') as m1, md5(md5('1')) as m2;
+----------------------------------+----------------------------------+
| m1 | m2 |
+----------------------------------+----------------------------------+
| | |
+----------------------------------+----------------------------------+
1 row in set (0.00 sec)
3. JavaScript 版MD5函數
md5.js 代碼如下:
[javascript] view plain
/*! JavaScript 的 MD5 實現 */
// 括弧表達式, (xxxxx) 是用來將內部的語句、表達式的結果作為一個結果.
// 常見的是將json字元串用 eval 解析時,需要 eval("(" +jsonstr+ ")");
// () 內部定義了一個空間, 裡面定義的變數不會污染到全局空間,很適合做lib
// (function UMD(對象/函數名name, 上下文this, 函數/對象的定義)) 返回一個匿名函數
// 因為第一個括弧內 的結果是一個函數,而函數可以這樣調用: (function(形參){})(實參);
// 這種匿名函數被瀏覽器解析後會自動執行一次.
(function UMD(name, context, definition) {
if ( typeof mole !== "undefined" && mole.exports) {
// 如果 mole 存在,並且mole.exports存在,則將賦值結果賦給 它
// 可以不用管
mole.exports = definition();
} else if ( typeof define === "function" && define.amd) {
// 如果 define 這個函數存在,應該是另一個基礎類庫,則使用define
// 可以不用管
define(definition);
} else {
// 簡單一點,可以看成: 調用傳入的definition函數,將返回的對象綁定到全局空間
// 當然,根據傳入的上下文不同,也可以綁定到其他對象下面,成為一個屬性方法.
context[name] = definition(name, context);
}
}
)("md5", this, function DEF(name, context) {"use strict";
// 上面的 use strict 表示嚴格語法模式,有錯誤就拒絕執行.
// 而普通的JS,是解釋執行,不執行的地方,有些錯誤也不影響其他代碼的執行
// 作為類庫,使用嚴格模式是很有必要的.嚴格模式聲明必須放到一個namespace空間的最起始處.
//
var old_public_api = (context || {})[name];
// 最後要返回的對象/函數.
function md5_func(text) {
return hex_md5(text);
};
// 下面一堆是具體的演算法... 可以先不用管
/////////////////////////////////////////////////////
//計算MD5
var hexcase = 0;
function hex_md5(a) {
if (a == "")
return a;
return rstr2hex(rstr_md5(str2rstr_utf8(a)))
};
function hex_hmac_md5(a, b) {
return rstr2hex(rstr_hmac_md5(str2rstr_utf8(a), str2rstr_utf8(b)))
};
function md5_vm_test() {
return hex_md5("abc").toLowerCase() == ""
};
function rstr_md5(a) {
return binl2rstr(binl_md5(rstr2binl(a), a.length * 8))
};
function rstr_hmac_md5(c, f) {
var e = rstr2binl(c);
if (e.length > 16) {
e = binl_md5(e, c.length * 8)
}
var a = Array(16), d = Array(16);
for (var b = 0; b < 16; b++) {
a[b] = e[b] ^ 909522486;
d[b] = e[b] ^ 1549556828
}
var g = binl_md5(a.concat(rstr2binl(f)), 512 + f.length * 8);
return binl2rstr(binl_md5(d.concat(g), 512 + 128))
};
function rstr2hex(c) {
try { hexcase
} catch(g) {
hexcase = 0
}
var f = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var b = "";
var a;
for (var d = 0; d < c.length; d++) {
a = c.charCodeAt(d);
b += f.charAt((a >>> 4) & 15) + f.charAt(a & 15)
}
return b
};
function str2rstr_utf8(c) {
var b = "";
var d = -1;
var a, e;
while (++d < c.length) {
a = c.charCodeAt(d);
e = d + 1 < c.length ? c.charCodeAt(d + 1) : 0;
if (55296 <= a && a <= 56319 && 56320 <= e && e <= 57343) {
a = 65536 + ((a & 1023) << 10) + (e & 1023);
d++
}
if (a <= 127) {
b += String.fromCharCode(a)
} else {
if (a <= 2047) {
b += String.fromCharCode(192 | ((a >>> 6) & 31), 128 | (a & 63))
} else {
if (a <= 65535) {
b += String.fromCharCode(224 | ((a >>> 12) & 15), 128 | ((a >>> 6) & 63), 128 | (a & 63))
} else {
if (a <= 2097151) {
b += String.fromCharCode(240 | ((a >>> 18) & 7), 128 | ((a >>> 12) & 63), 128 | ((a >>> 6) & 63), 128 | (a & 63))
}
}
}
}
}
return b
};
function rstr2binl(b) {
var a = Array(b.length >> 2);
for (var c = 0; c < a.length; c++) {
a[c] = 0
}
for (var c = 0; c < b.length * 8; c += 8) {
a[c >> 5] |= (b.charCodeAt(c / 8) & 255) << (c % 32)
}
return a
};
function binl2rstr(b) {
var a = "";
for (var c = 0; c < b.length * 32; c += 8) {
a += String.fromCharCode((b[c >> 5] >>> (c % 32)) & 255)
}
return a
};
function binl_md5(p, k) {
p[k >> 5] |= 128 << ((k) % 32);
p[(((k + 64) >>> 9) << 4) + 14] = k;
var o = 1732584193;
var n = -271733879;
var m = -1732584194;
var l = 271733878;
for (var g = 0; g < p.length; g += 16) {
var j = o;
var h = n;
var f = m;
var e = l;
o = md5_ff(o, n, m, l, p[g + 0], 7, -680876936);
l = md5_ff(l, o, n, m, p[g + 1], 12, -389564586);
m = md5_ff(m, l, o, n, p[g + 2], 17, 606105819);
n = md5_ff(n, m, l, o, p[g + 3], 22, -1044525330);
o = md5_ff(o, n, m, l, p[g + 4], 7, -176418897);
l = md5_ff(l, o, n, m, p[g + 5], 12, 1200080426);
m = md5_ff(m, l, o, n, p[g + 6], 17, -1473231341);
n = md5_ff(n, m, l, o, p[g + 7], 22, -45705983);
o = md5_ff(o, n, m, l, p[g + 8], 7, 1770035416);
l = md5_ff(l, o, n, m, p[g + 9], 12, -1958414417);
m = md5_ff(m, l, o, n, p[g + 10], 17, -42063);
n = md5_ff(n, m, l, o, p[g + 11], 22, -1990404162);
o = md5_ff(o, n, m, l, p[g + 12], 7, 1804603682);
l = md5_ff(l, o, n, m, p[g + 13], 12, -40341101);
m = md5_ff(m, l, o, n, p[g + 14], 17, -1502002290);
n = md5_ff(n, m, l, o, p[g + 15], 22, 1236535329);
o = md5_gg(o, n, m, l, p[g + 1], 5, -165796510);
l = md5_gg(l, o, n, m, p[g + 6], 9, -1069501632);
m = md5_gg(m, l, o, n, p[g + 11], 14, 643717713);
n = md5_gg(n, m, l, o, p[g + 0], 20, -373897302);
o = md5_gg(o, n, m, l, p[g + 5], 5, -701558691);
l = md5_gg(l, o, n, m, p[g + 10], 9, 38016083);
m = md5_gg(m, l, o, n, p[g + 15], 14, -660478335);
n = md5_gg(n, m, l, o, p[g + 4], 20, -405537848);
o = md5_gg(o, n, m, l, p[g + 9], 5, 568446438);
l = md5_gg(l, o, n, m, p[g + 14], 9, -1019803690);
m = md5_gg(m, l, o, n, p[g + 3], 14, -187363961);
n = md5_gg(n, m, l, o, p[g + 8], 20, 1163531501);
o = md5_gg(o, n, m, l, p[g + 13], 5, -1444681467);
l = md5_gg(l, o, n, m, p[g + 2], 9, -51403784);
m = md5_gg(m, l, o, n, p[g + 7], 14, 1735328473);
n = md5_gg(n, m, l, o, p[g + 12], 20, -1926607734);
o = md5_hh(o, n, m, l, p[g + 5], 4, -378558);
l = md5_hh(l, o, n, m, p[g + 8], 11, -2022574463);
m = md5_hh(m, l, o, n, p[g + 11], 16, 1839030562);
n = md5_hh(n, m, l, o, p[g + 14], 23, -35309556);
o = md5_hh(o, n, m, l, p[g + 1], 4, -1530992060);
l = md5_hh(l, o, n, m, p[g + 4], 11, 1272893353);
m = md5_hh(m, l, o, n, p[g + 7], 16, -155497632);
n = md5_hh(n, m, l, o, p[g + 10], 23, -1094730640);
o = md5_hh(o, n, m, l, p[g + 13], 4, 681279174);
l = md5_hh(l, o, n, m, p[g + 0], 11, -358537222);
m = md5_hh(m, l, o, n, p[g + 3], 16, -722521979);
n = md5_hh(n, m, l, o, p[g + 6], 23, 76029189);
❿ 急求 MD5的加密解密演算法,用C++實現的源代碼 高分答謝
要代碼,還是要相關的解釋資料?
---------------------------------
要代碼的話:
兩個文件:
--------------------------
1. md5.h:
#pragma once
typedef unsigned long int UINT32;
typedef unsigned short int UINT16;
/* MD5 context. */
typedef struct {
UINT32 state[4]; /* state (ABCD) */
UINT32 count[2]; /* number of bits, molo 2^64 (lsb first) */
unsigned char buffer[64]; /* input buffer */
} MD5_CTX;
void MD5Init (MD5_CTX *);
void MD5Update (MD5_CTX *, unsigned char *, unsigned int);
void MD5Final (unsigned char [16], MD5_CTX *);
--------------------------
2. md5.cpp:
#include "md5.h"
#include "memory.h"
#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 void MD5Transform (UINT32 a[4], unsigned char b[64]);
static void Encode (unsigned char *, UINT32 *, unsigned int);
static void Decode (UINT32 *, unsigned char *, unsigned int);
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) + (UINT32)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (UINT32)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (UINT32)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (UINT32)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
void MD5Init (MD5_CTX *context)
{
context->count[0] = context->count[1] = 0;
context->state[0] = 0x67452301;
context->state[1] = 0xefcdab89;
context->state[2] = 0x98badcfe;
context->state[3] = 0x10325476;
}
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] += ((UINT32)inputLen << 3))
< ((UINT32)inputLen << 3))
context->count[1]++;
context->count[1] += ((UINT32)inputLen >> 29);
partLen = 64 - index;
if (inputLen >= partLen) {
memcpy((unsigned char *)&context->buffer[index], (unsigned char *)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((unsigned char *)&context->buffer[index], (unsigned char *)&input[i],
inputLen-i);
}
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 ((unsigned char *)context, 0, sizeof (*context));
}
static void MD5Transform (UINT32 state[4], unsigned char block[64])
{
UINT32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
Decode (x, block, 64);
/* Round 1 */
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* Round 2 */
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* Round 3 */
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
/* Round 4 */
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
memset ((unsigned char *)x, 0, sizeof (x));
}
static void Encode (unsigned char *output, UINT32 *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);
}
}
static void Decode (UINT32 *output, unsigned char *input, unsigned int len)
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((UINT32)input[j]) | (((UINT32)input[j+1]) << 8) |
(((UINT32)input[j+2]) << 16) | (((UINT32)input[j+3]) << 24);
}
--------------------------
就這兩個文件。使用的時候把它們加入工程或者makefile,調用時包含md5.h即可,給個簡單的例子,輸入一個字元串然後計算它的md5值並輸出,在VC6.0和GCC4.4下測試通過:
#include <stdio.h>
#include <string.h>
#include "md5.h"
int main ()
{
char tmp[128];
unsigned char digest[16];
MD5_CTX context;
scanf("%s",tmp);
MD5Init (&context);
MD5Update (&context, (unsigned char*)tmp, strlen(tmp));
MD5Final (digest,&context);
printf("MD5Value:");
for(int i=0; i<16; ++i)
{
printf("%02X",digest[i]);
}
printf("\n");
return 0;
}