• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java与C++实现相同的MD5加密算法简单实例

java 搞代码 4年前 (2022-01-05) 23次浏览 已收录 0个评论

下面小编就为大家带来一篇Java与C++实现相同的MD5加密算法简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

1、Java版

 package com.lyz.utils.common; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * MD5加密 * @author liuyazhuang */ public class MD5Hash { public static String md5Java(String message) { String digest = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] hash = md.digest(message.getBytes("UTF-8")); //converting byte array to 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   #include   #include   #include   void  MD5Digest(char  *pszInput,  unsigned  long  nInputSize,  char  *pszOutPut); 

(2)md5.cpp

 #include   #include   #include   #include   #include  "md5.h" typedef  unsigned  char  *POINTER; typedef  unsigned  short  int  UINT2; typedef  unsigned  long  int  UINT4; typedef  struct { UINT4  state[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)  <>  (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  >  8)  <b style="color:transparent">来源gao@dai!ma.com搞$代^码网</b>&  0xff); output[j+2]  =  (unsigned  char)((input[i]  >>  16)  &  0xff); output[j+3]  =  (unsigned  char)((input[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)input[j+2])  <<  16)  |  (((UINT4)input[j+3])  <count[0]  =  context->count[1]  =  0; context->state[0]  =  0x67452301; context->state[1]  =  0xefcdab89; context->state[2]  =  0x98badcfe; context->state[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  <count[1]++; context->count[1]  +=  ((UINT4)inputLen  >>  29); partLen  =  64  -  index; if  (inputLen  >=  partLen)  { memcpy((POINTER)&context->buffer[index],  (POINTER)input,  partLen); MD5Transform(context->state,  context->buffer); for  (i  =  partLen;  i  +  63  state,  &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  =  (index  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,  (unsigned  char  *)pszInput,  len); MD5Final  ((unsigned  char  *)pszOutPut,  &context); } main() { char szDigest[16]; char encrypt[200]; printf("请输入要计算MD5值的字符串:"); gets(encrypt); printf("\n加密结果:"); MD5Digest(encrypt,strlen(encrypt),szDigest); int i; for (i=0;i<16;i++) printf ("%02X",(unsigned char)szDigest[i]); getchar(); } 

3、运行效果

这里我们都以输入123456为例

(1)java输出结果如下:

(2)C++输出结果如下:

以上就是小编为大家带来的Java与C++实现相同的MD5加密算法简单实例的全部内容了,希望对大家有所帮助,多多支持gaodaima搞代码网~

以上就是Java与C++实现相同的MD5加密算法简单实例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java与C++实现相同的MD5加密算法简单实例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址