1、 java版
包com.lyz.utils.common;導入java.io.unsupportedencodingexception;導入java.security.messagedigest;導入java.security.nosuchalgorithmexception; / ** * md5加密 * @author liuyazhuang */ public class md5hash {public static string md5java(字符串消息){string digest = null;嘗試{MessageDigest md = Messagedigest.getInstance(“ MD5”); byte [] hash = md.digest(message.getBytes(“ utf-8”)); //將字節數組轉換為十六進製字符串StringBuilder SB = new StringBuilder(2 * Hash.length); for(字節B:hash){sb.append(string.format(“%02X”,b&0xff)); } digest = sb.tostring(); } catch(unsupportedEncodingException ex){//logger.getLogger(stringreplace.class.getname()).log(level.severe,null,null,ex); } catch(nosuchalgorithmexception ex){//logger.getLogger(stringreplace.class.getname()).log(level.severe,null,null,ex); }返回摘要; } public static void main(string [] args){system.out.println(md5java(“ admin”).touppercase()); }}}2 C ++代碼
(1)MD5.H
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> void md5digest(char *pszinput,無簽名的長ninputsize,char *pszoutput);
(2)MD5.CPP
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include“ md5.h” typedef unsigned unsigned char *pointer; typedef unsigned short int uint2; typedef unsigned long int uint4; typedef struct {uint4 state [4]; UINT4計數[2];未簽名的char緩衝區[64]; } md5_ctx; void md5init(md5_ctx *); void md5update(md5_ctx *,unsigned char *,unsigned int); void md5 -final(unsigned char [16],md5_ctx *); #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、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,b,c,c,c,d,x,x,s,s,s,s,ac){(a) + = f(a) + = f(a),(b),(c),(c),(c),(d),(d),(d),(d),x) +(x) +(x) +(ac)(ac)(ac)(ac)(ac)(ac) (a)= rotate_left((a),(s)); (a) +=(b); } #define gg(a,b,c,d,x,s,ac){(a) + = g((b),(c),(d),(d)) +(x) +(x) +(uint4)(ac); (a)= rotate_left((a),(s)); (a) +=(b); } #define HH(a,b,c,d,x,s,ac){(a) + = h((b),(c),(d),(d)) +(x) +(x) +(uint4)(ac); (a)= rotate_left((a),(s)); (a) +=(b); } #define II(a,b,c,d,x,s,ac){(a) + = i((b),(c),(d),(d)) +(x) +(x) +(uint4)(ac); (a)= rotate_left((a),(s)); (a) +=(b); } inline void encode(unsigned char *輸出,uint4 *輸入,unsigned int len){unsigned int i,j;對於(i = 0,j = 0; j <len; i ++,j+= 4){output [j] =(unsigned char)(input [i]&0xff);輸出[J+1] =(unsigned char)((輸入[i] >> 8)&0xff);輸出[J+2] =(unsigned char)((輸入[i] >> 16)&0xff);輸出[j+3] =(unsigned char)((輸入[i] >> 24)&0xff); }} inline void decode(uint4 *輸出,unsigned char *輸入,unsigned int len){unsigned int i,j; for(i = 0,j = 0; j <len; i ++,j+= 4)輸出[i] =((uint4)輸入[j])| (((UINT4)輸入[J+1])<< 8)| (((UINT4)輸入[J+2])<< 16)| (((UINT4)輸入[J+3])<< 24); } inline void md5transform(uint4狀態[4],unsigned char block [64]){uint4 a = state [0],b = state [1],c = state [2],d = state [3],x [16];解碼(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,0x8b444f7af); 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);狀態[0] += a;狀態[1] += b;狀態[2] += c;狀態[3] += D; memset((指針)x,0,sizeof(x)); } inline void md5init(md5_ctx *context){context-> count-> count [0] = context-> count [1] = 0;上下文 - >狀態[0] = 0x67452301; context-> state [1] = 0xEFCDAB89; context->狀態[2] = 0x98badcfe; context->狀態[3] = 0x10325476; } inline void md5update(md5_ctx *上下文,unsigned char *輸入,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-> count [1] ++; context-> count [1] +=((UINT4)inputlen >> 29); partlen = 64-索引; if(inputlen> = partlen){memcpy((指針)和上下文 - > buffer [index],(指針)輸入,partlen); md5transform(context-> state,context-> buffer); for(i = partlen; i + 63 <inputlen; i + = 64)md5transform(context-> state和input [i]);索引= 0; } else i = 0; memcpy((指針)和上下文 - > buffer [index],(指針)和輸入[i],inputlen-i); } inline void md5final(unsigned char Digest [16],md5_ctx *上下文){unsigned char bits [8];未簽名的int索引,帕德倫;編碼(位,上下文 - > count,8); index =(unsigned int)((context-> count [0] >> 3)&0x3f); padlen =(索引<56)? (56-索引):( 120-索引); md5update(上下文,填充,帕德倫); md5update(上下文,位,8);編碼(摘要,上下文 - >狀態,16); memset((指針)上下文,0,sizeof(*context)); } void md5digest(char *pszinput,unsigned long ninputsize,char *pszoutput){md5_ctx上下文; unsigned int len = strlen(pszinput); md5init(&context); md5update(&context,(unsigned char *)pszinput,len); md5final((未符號char *)pszoutput,&context); } main(){char szdigest [16]; Char Encrypt [200]; printf(“請輸入要計算md5值的字符串:”);獲取(加密); printf(“/n加密結果:”); md5digest(加密,strlen(encrypt),szdigest); int i;對於(i = 0; i <16; i ++)printf(“%02x”,(unsigned char)szdigest [i]); getchar();}3 、運行效果
這裡我們都以輸入123456為例
(1)Java輸出結果如下:
(2)C ++::
以上就是小編為大家帶來的java與c ++實現相同的md5加密算法簡單實例的全部內容了,希望對大家有所幫助,多多支持武林網〜