1 、 Java 版
paket com.lyz.utils.Common; impor java.io.unsupportedencodingException; impor java.security.messagedigest; impor java.security.nosuchalgorithmException; / ** * md5 加密 * @author liuyazhuang */ kelas publik md5hash {public static string md5Java (pesan string) {string digest = null; Coba {MessageDigest MD = MessageSpetedigest.getInstance ("md5"); byte [] hash = md.digest (message.getbytes ("UTF-8")); // mengonversi array byte ke hexadecimal string stringBuilder sb = new stringBuilder (2 * hash.length); untuk (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> batal 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 char unsigned char *pointer; typedef Int uint2 yang tidak ditandatangani; typedef unsigned long int uint4; typedef struct {uint4 state [4]; Hitungan UInt4 [2]; buffer char yang tidak ditandatangani [64]; } Md5_ctx; void md5Init (md5_ctx *); void md5update (md5_ctx *, char unsigned *, 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 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 yang ” 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) (a) +k) ((b), (c), (d) (x) (a) +k); (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 (output char *unsigned, uint4 *input, unsigned int len) {unsigned int i, j; untuk (i = 0, j = 0; j <len; i ++, j+= 4) {output [j] = (unsigned char) (input [i] & 0xff); output [j+1] = (char unsigned) ((input [i] >> 8) & 0xff); output [j+2] = (unsigned char) ((input [i] >> 16) & 0xff); output [j+3] = (unsigned char) ((input [i] >> 24) & 0xff); }} inline void decode (output4 *output, input char *unsigned, unsigned int len) {unsigned int i, j; untuk (i = 0, j = 0; j <len; i ++, j+= 4) output [i] = ((uint4) input [j]) | (((Uint4) input [j+1]) << 8) | (((Uint4) input [j+2]) << 16) | ((((Uint4) input [j+3]) << 24); } inline void md5transform (state uint4 [4], blok char unsigned [64]) {uint4 a = status [0], b = status [1], c = status [2], d = status [3], x [16]; Decode (x, blok, 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); status [0] += a; status [1] += b; status [2] += c; status [3] += D; memset ((pointer) x, 0, sizeof (x)); } inline void md5Init (md5_ctx *konteks) {context-> count [0] = context-> count [1] = 0; konteks-> status [0] = 0x67452301; konteks-> status [1] = 0xefcdab89; konteks-> status [2] = 0x98badcfe; konteks-> status [3] = 0x10325476; } inline void md5Update (md5_ctx *konteks, input char *unsigned, inputlen int unsigned) {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] ++; konteks-> hitung [1] += ((uint4) inputlen >> 29); partlen = 64 - indeks; if (inputlen> = partlen) {memcpy ((pointer) & context-> buffer [index], (pointer) input, partlen); Md5transform (context-> state, context-> buffer); untuk (i = partlen; i + 63 <inputlen; i + = 64) md5transform (context-> state, & input [i]); indeks = 0; } else i = 0; memcpy ((pointer) & context-> buffer [index], (pointer) & input [i], inputlen-i); } inline void md5final (unsigned char digest [16], md5_ctx *konteks) {bit char unsigned [8]; Indeks int yang tidak ditandatangani, Padlen; Enkode (bit, konteks-> Count, 8); index = (unsigned int) ((Context-> Count [0] >> 3) & 0x3f); Padlen = (indeks <56)? (56 - indeks): (120 - indeks); Md5update (konteks, padding, padlen); Md5update (konteks, bit, 8); Encode (Digest, Context-> State, 16); memset ((pointer) konteks, 0, sizeof (*konteks)); } void md5digest (char *pszinput, unsigned long ninputsize, char *pszoutput) {md5_ctx konteks; unsigned int len = strlen (pszinput); Md5init (& konteks); Md5update (& konteks, (char unsigned *) pszinput, len); Md5final ((char unsigned *) pszoutput, & konteks); } main () {char szdigest [16]; Enkripsi Char [200]; printf ("请输入要计算 md5 值的字符串:"); mendapat (enkripsi); printf ("/n 加密结果:"); Md5digest (enkripsi, strlen (enkripsi), szdigest); int i; untuk (i = 0; i <16; i ++) printf ("%02x", (char unsigned) szdigest [i]); getchar ();}3 、运行效果
这里我们都以输入 123456 为例
(1) Java 输出结果如下 :
(2) C ++ 输出结果如下 :
以上就是小编为大家带来的 java 与 c ++ 实现相同的 md5 加密算法简单实例的全部内容了 , 希望对大家有所帮助 , 多多支持武林网 ~