1 、 Java 版
package com.lyz.utils.common; Importer java.io.UNSUPPORTEDENCODINGException; Importer Java.Security.MessagediGest; importer java.security.nosuchalgorithMexception; / ** * md5 加密 * @author liUyazhuang * / public class md5hash {public static String md5java (String Message) {String digest = null; essayez {messagediGest md = messagediGest.getInstance ("md5"); Byte [] hash = md.digest (message.getBytes ("utf-8")); // Conversion du tableau d'octets en chaîne hexadécimale StringBuilder sb = new StringBuilder (2 * hash.length); pour (octet b: hash) {sb.append (string.format ("% 02x", b & 0xff)); } digest = sb.toString (); } catch (unportdencodingException 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"). toupperase ()); }}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" Pointer de char * non signé; typedef non signed Int uint2; typedef unsigned long int uint4; typedef struct {uint4 state [4]; Uint4 Count [2]; tampon char non signé [64]; } Md5_ctx; void md5Init (md5_ctx *); void md5update (md5_ctx *, char * non signé *, unsigned int); void md5final (char non signé [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); } en ligne void encode (sortie de char * non signée, entrée uint4 *, intr Len unsigned) {unsigned int i, j; pour (i = 0, j = 0; j <len; i ++, j + = 4) {output [j] = (char non signé) (entrée [i] & 0xff); sortie [j + 1] = (char non signé) ((entrée [i] >> 8) & 0xff); sortie [j + 2] = (char non signé) ((entrée [i] >> 16) & 0xff); sortie [j + 3] = (char non signé) ((entrée [i] >> 24) & 0xff); }} inline void Decode (uint4 * sortie, entrée de char * non signée, intr Len unsigned) {unsigned int i, j; pour (i = 0, j = 0; j <len; i ++, j + = 4) sortie [i] = ((uint4) entrée [j]) | (((Uint4) entrée [J + 1]) << 8) | (((Uint4) entrée [J + 2]) << 16) | (((Uint4) entrée [J + 3]) << 24); } inline void md5Transform (uint4 state [4], bloc de char non signé [64]) {uint4 a = état [0], b = état [1], c = état [2], d = état [3], x [16]; Decode (x, bloc, 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); état [0] + = a; état [1] + = b; état [2] + = c; état [3] + = d; memset ((pointeur) x, 0, sizeof (x)); } inline void md5Init (md5_ctx * context) {context-> count [0] = context-> count [1] = 0; context-> état [0] = 0x67452301; context-> état [1] = 0XEFCDAB89; Context-> State [2] = 0x98BADCFE; context-> état [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 ((pointeur) & context-> buffer [index], (pointeur) entrée, partlen); Md5Transform (context-> état, context-> tampon); pour (i = partlen; i + 63 <inputlen; i + = 64) md5Transform (context-> état, & input [i]); index = 0; } else i = 0; memcpy ((pointeur) & context-> buffer [index], (pointeur) & input [i], inputlen-i); } Inline void md5final (unsigned char Digest [16], MD5_CTX * Context) {Bits char non signé [8]; index int non signé, padlen; Encode (bits, context-> count, 8); index = (unsigned int) ((context-> count [0] >> 3) & 0x3f); Padlen = (index <56)? (56 - index): (120 - index); Md5update (contexte, rembourrage, padlen); Md5update (contexte, 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, (non signé *) pszinput, len); MD5Final ((UNSIGNED CHAR *) PSZOutput, & Context); } main () {char szdigest [16]; Char Encrypt [200]; printf ("请输入要计算 md5 值的字符串:"); obtient (crypter); printf ("/ n 加密结果:"); Md5digest (Encrypt, strlen (Encrypt), szdigest); int i; pour (i = 0; i <16; i ++) printf ("% 02x", (char) szdigest [i]); getchar ();}3 、运行效果
这里我们都以输入 123456 为例
(1) Java 输出结果如下 :
(2) C ++ 输出结果如下:
以上就是小编为大家带来的 java 与 c ++ 实现相同的 md5 加密算法简单实例的全部内容了 , 希望对大家有所帮助 , 多多支持武林网 ~