1 版 java 、
패키지 com.lyz.utils.common; java.io.unsupportedencodingException 가져 오기; java.security.messagegegest; java.security.nosuchalgorithmexception import; / ** * md5 加密 * @author liuyazhuang */ public class md5hash {public static string md5java (문자열 메시지) {String digest = null; try {messageDigest md = messageDigest.getInstance ( "md5"); 바이트 [] hash = md.digest (message.getBytes ( "utf-8")); // 바이트 배열을 16 진수 문자열로 변환 stringbuilder sb = new StringBuilder (2 * hash.length); for (byte b : hash) {sb.append (string.format ( "%02x", b & 0xff)); } digest = sb.toString (); } catch (UnSupportedEncodingException ex) {//logger.getLogger(StringReplace.class.getName()) .log(level.severe, null, ex); } catch (nosuchalgorithmexception ex) {//logger.getLogger(StringReplace.class.getName()) .log(level.severe, 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, unsigned long ninputsize, char *pszoutput);
(2) MD5.CPP
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "md5.h"typedef unsigned char *pointer; typedef는 서명되지 않은 짧은 int uint2; typedef는 서명되지 않은 긴 int uint4; typedef struct {uint4 상태 [4]; UINT4 수 [2]; 서명되지 않은 숯 버퍼 [64]; } md5_ctx; void md5init (md5_ctx *); void md5update (md5_ctx *, 부호없는 char *, 부호없는 int); void md5final (부호없는 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 15 #define s44 21 정적 부호없는 char 패딩 [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, 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)) | ((z)) | ((y) & (~ z))) #define h (x, y, z) (x) ^ (y) ^ (z)) #define i (X, Z, Z, Z (Z) (~ 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)) + (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 (부호없는 char *출력, uint4 *입력, 부호없는 int len) {unsigned int i, j; for (i = 0, j = 0; j <len; i ++, j+= 4) {output [j] = (unsigned char) (입력 [i] & 0xff); 출력 [j+1] = (부호없는 char) ((입력 [i] >> 8) & 0xff); 출력 [j+2] = (부호없는 char) ((입력 [i] >> 16) & 0xff); 출력 [j+3] = (부호없는 char) ((입력 [i] >> 24) & 0xff); }} inline void decode (uint4 *출력, 부호없는 char *입력, 서명되지 않은 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 state [4], 부호없는 숯 블록 [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, 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); 상태 [0] += a; 상태 [1] += B; 상태 [2] += C; 상태 [3] += D; memset ((포인터) x, 0, sizeof (x)); } inline void md5init (md5_ctx *context) {context-> count [0] = context-> count [1] = 0; 컨텍스트-> 상태 [0] = 0x67452301; context-> state [1] = 0xefcdab89; 컨텍스트-> 상태 [2] = 0x98badcfe; 컨텍스트-> 상태 [3] = 0x10325476; } inline void md5update (md5_ctx *컨텍스트, 부호없는 char *입력, 서명되지 않은 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- 인덱스; if (inputlen> = partlen) {memcpy ((포인터) & context-> 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 ((포인터) 및 컨텍스트-> 버퍼 [index], (포인터) & 입력 [i], inputlen-i); } inline void md5final (부호없는 숯 다이제스트 [16], md5_ctx *context) {unsigned char bits [8]; 서명되지 않은 Int Index, Padlen; 인코딩 (비트, 컨텍스트-> 카운트, 8); index = (unsigned int) ((context-> count [0] >> 3) & 0x3f); padlen = (index <56)? (56- 인덱스) : (120- 인덱스); md5update (컨텍스트, 패딩, 자물쇠); md5update (컨텍스트, 비트, 8); encode (digest, context-> state, 16); memset ((포인터) 컨텍스트, 0, sizeof (*context)); } void md5digest (char *pszinput, unsigned long ninputsize, char *pszoutput) {md5_ctx 컨텍스트; 서명되지 않은 int len = strlen (pszinput); md5init (& 컨텍스트); md5update (& context, (서명 된 char *) pszinput, Len); md5final ((서명 된 char *) pszoutput, & context); } main () {char szdigest [16]; 숯 암호화 [200]; printf ( "请输入要计算 md5 值的字符串 :"); gets (암호화); printf ( "/n": "); md5digest (암호화, strlen (암호), szdigest); int i; (i = 0; i <16; i ++) printf ( "%02x", (unsigned char) szdigest [i]); getchar ();}3 、运行效果
这里我们都以输入 123456 为例
(1) Java j :
(2) C ++ c :
以上就是小编为大家带来的 Java a C ++ 实现相同的 md5 加密算法简单实例的全部内容了, 希望对大家有所帮助, 多多支持武林网 ~