1 、 java 版
paquete com.lyz.utils.common; import java.io.unsupportedencodingException; import java.security.MessageGest; import java.security.nosuchalgorithmexception; / ** * md5 加密 * @author liuyazhuang */ public class md5hash {public static string md5Java (string Message) {String Digest = null; intente {MessageDigest md = MessageDigest.getInstance ("MD5"); byte [] hash = md.digest (message.getBytes ("utf-8")); // Convertir la matriz de bytes a hexadecimal string 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); } 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, un sin firmar Long NinputSize, char *pszoutput);
(2) MD5.CPP
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <tring.h> #include "md5.h" typedef unsigned char *pointer; typedef unsigned short int uint2; typedef sinsigned long int uint4; typedef struct {estado uint4 [4]; UINT4 cuenta [2]; Buffer de char sin firmar [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); } Enline void codeN (unsigned char *output, uint4 *entrada, unsigned int len) {unsigned int i, j; para (i = 0, j = 0; j <len; i ++, j+= 4) {output [j] = (unsigned char) (entrada [i] & 0xff); salida [j+1] = (unsigned char) ((entrada [i] >> 8) y 0xff); salida [j+2] = (unsigned char) ((entrada [i] >> 16) y 0xff); salida [j+3] = (unsigned char) ((entrada [i] >> 24) y 0xff); }} en línea void decode (salida uint4 *, entrada unsigned char *, unsigned int len) {unsigned int i, j; para (i = 0, j = 0; j <len; i ++, j+= 4) salida [i] = ((uint4) entrada [j]) | ((((Uint4) entrada [j+1]) << 8) | ((((Uint4) entrada [j+2]) << 16) | (((UINT4) entrada [J+3]) << 24); } en línea void md5transform (estado uint4 [4], un bloque de char unsigned [64]) {uint4 a = estado [0], b = estado [1], c = estado [2], d = estado [3], x [16]; Decodificación (x, bloque, 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); estado [0] += a; estado [1] += b; estado [2] += c; estado [3] += D; memset ((puntero) x, 0, sizeof (x)); } inline void md5init (md5_ctx *context) {context-> count [0] = context-> count [1] = 0; context-> estado [0] = 0x67452301; context-> estado [1] = 0xefcdab89; context-> estado [2] = 0x98badcfe; context-> estado [3] = 0x10325476; } Inline void md5Update (MD5_CTX *context, unsigned char *entrada, unsigned int inputLen) {unsigned int i, index, partlen; index = (unsigned int) ((context-> count [0] >> 3) y 0x3f); if ((context-> count [0]+= ((uint4) inputlen << 3)) <((uint4) inputlen << 3)) context-> count [1] ++; context-> count [1] += ((uint4) inputlen >> 29); partlen = 64 - índice; if (inputLen> = partlen) {memcpy ((puntero) y context-> buffer [index], (puntero) entrada, partlen); Md5transform (context-> estado, context-> buffer); para (i = partlen; i + 63 <inputlen; i + = 64) md5transform (context-> estado, e input [i]); índice = 0; } else i = 0; memcpy ((puntero) y context-> buffer [índice], (puntero) e input [i], inputlen-i); } en línea void md5final (unsigned char digest [16], md5_ctx *context) {bits de char sin firmar [8]; Unsigned int index, Padlen; Codificar (bits, context-> count, 8); index = (unsigned int) ((context-> count [0] >> 3) y 0x3f); Padlen = (índice <56)? (56 - índice): (120 - índice); Md5Update (contexto, relleno, padlen); Md5Update (contexto, bits, 8); Codificar (digest, context-> estado, 16); MemSet ((puntero) contexto, 0, sizeof (*context)); } void md5digest (char *pszinput, sin firmar Long NinputSize, char *pszoutput) {contexto md5_ctx; unsigned int len = strlen (pszinput); MD5Init (y contexto); Md5Update (& context, (unsigned char *) pszinput, len); Md5final ((unsigned char *) pszoutput, & context); } main () {char szdigest [16]; char encrypt [200]; printf ("请输入要计算 md5 值的字符串:"); obtiene (encrypt); printf ("/n 加密结果:"); MD5Digest (CiCrypt, Strlen (CiCrypt), Szdigest); int i; para (i = 0; i <16; i ++) printf ("%02x", (unsigned char) szdigest [i]); getchar ();}3 、运行效果
这里我们都以输入 123456 为例
(1) Java 输出结果如下 :
(2) C ++ 输出结果如下 :
以上就是小编为大家带来的 java 与 c ++ 实现相同的 md5 加密算法简单实例的全部内容了 , 希望对大家有所帮助 , 多多支持武林网 ~