1 、 Java 版
пакет com.liz.utils.common; импортировать java.io.unsupportedencodingexception; Импорт java.security.messagedigest; Импорт java.security.nosuchalgorithmexception; / ** * md5 加密 * @author liuyazhuang */ public class md5hash {public static String md5java (String message) {String digest = null; try {messagegest md = messagedigest.getinstance ("md5"); byte [] hash = md.digest (message.getbytes ("utf-8")); // преобразование байтового массива в шестнадцатеричную строку StringBuilder SB = New StringBuilder (2 * hash.length); для (Byte B: hash) {sb.append (string.format ("%02x", b & 0xff)); } digest = sb.toString (); } catch (UnsupportEncodingException ex) {//logger.getLogger(StringReplace.class.getName()).log(Level.Severe, null, Ex); } catch (nosuchalgorithmexception ex) {//logger.getLogger(StringReplace.class.getName()).log(Level.Severe, null, Ex); } return Digest; } 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 ninptize, 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 unsigned short int uint2; typedef без знака Long int uint4; typedef struct {uint4 wation [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 *); #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); Выход [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 *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) вход [j+2]) << 16) | (((Uint4) вход [J+3]) << 24); } inline void md5transform (uint4 state [4], unsigned char block [64]) {uint4 a = состояние [0], b = состояние [1], c = состояние [2], d = состояние [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; контекст-> состояние [1] = 0xefcdab89; контекст-> состояние [2] = 0x98badcfe; контекст-> состояние [3] = 0x10325476; } inline void md5update (md5_ctx *контекст, 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] ++; контекст-> count [1] += ((uint4) inputlen >> 29); partlen = 64 - индекс; 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-> wation, & 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 = (индекс <56)? (56 - Индекс): (120 - Индекс); Md5update (контекст, прокладка, падлен); Md5update (контекст, биты, 8); Encode (Digest, Context-> State, 16); memset ((pointer) контекст, 0, sizeof (*context)); } void md5digest (char *pszinput, unsigned long ninputsize, char *pszoutput) {context md5_ctx; unsigned int len = strlen (pszinput); Md5init (& context); Md5update (& context, (unsigned char *) pszinput, len); Md5final ((unsigned char *) pszoutput, & context); } main () {char szdigest [16]; char incrypt [200]; printf ("请输入要计算 md5 值的字符串:"); получает (шифровать); printf ("/n 加密结果:"); MD5Digest (Encrypt, 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 加密算法简单实例的全部内容了 , 希望对大家有所帮助 , 多多支持武林网 ~