环境:vc2003

.h

 /* MD5.H - header file for MD5C.C
*/ /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved. License to copy and use this software is granted provided that it
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
Algorithm" in all material mentioning or referencing this software
or this function. License is also granted to make and use derivative works provided
that such works are identified as "derived from the RSA Data
Security, Inc. MD5 Message-Digest Algorithm" in all material
mentioning or referencing the derived work. RSA Data Security, Inc. makes no representations concerning either
the merchantability of this software or the suitability of this
software for any particular purpose. It is provided "as is"
without express or implied warranty of any kind. These notices must be retained in any copies of any part of this
documentation and/or software.
*/ #ifndef MD5_SECRIT_H__
#define MD5_SECRIT_H__ namespace SECRIET
{
/* MD5 context. */
typedef unsigned int uint32_t;
typedef struct {
uint32_t state[]; /* state (ABCD) */
uint32_t count[]; /* number of bits, modulo 2^64 (lsb first) */
unsigned char buffer[]; /* input buffer */
} MD5_CTX; void MD5Init(MD5_CTX *context);
void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen);
void MD5Final(unsigned char digest[], MD5_CTX *context); class CMD5
{
public:
MD5_CTX _ctxMd5; public:
CMD5() {MD5Init(&_ctxMd5);}
~CMD5() {} public:
void Encode( unsigned char *input, unsigned int inputLen, unsigned char digest[] )
{
MD5Update( &_ctxMd5, input, inputLen );
MD5Final( digest, &_ctxMd5);
} };
}
#endif

.cpp

 /*
* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
*/ /*
* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights
* reserved.
*
* License to copy and use this software is granted provided that it is
* identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm"
* in all material mentioning or referencing this software or this function.
*
* License is also granted to make and use derivative works provided that such
* works are identified as "derived from the RSA Data Security, Inc. MD5
* Message-Digest Algorithm" in all material mentioning or referencing the
* derived work.
*
* RSA Data Security, Inc. makes no representations concerning either the
* merchantability of this software or the suitability of this software for
* any particular purpose. It is provided "as is" without express or implied
* warranty of any kind.
*
* These notices must be retained in any copies of any part of this
* documentation and/or software.
*/ #include "Stdafx.h"
#include "md5.h" /*
* Constants for MD5Transform routine.
*/
#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 namespace SECRIET
{ static unsigned char PADDING[] = {
0x80, , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , ,
}; /*
* F, G, H and I are basic MD5 functions.
*/
#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))) /*
* ROTATE_LEFT rotates x left n bits.
*/
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) /*
* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is
* separate from addition to prevent recomputation.
*/
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
} /*
* Encodes input (uint32_t) into output (unsigned char). Assumes len is a
* multiple of 4.
*/
static void
Encode(unsigned char *output, uint32_t * input, unsigned int len)
{
unsigned int i, j; // ASSERT((len % 4) == 0); for (i = , j = ; j < len; i++, j += ) {
output[j] = (unsigned char) (input[i] & 0xff);
output[j + ] = (unsigned char) ((input[i] >> ) & 0xff);
output[j + ] = (unsigned char) ((input[i] >> ) & 0xff);
output[j + ] = (unsigned char) ((input[i] >> ) & 0xff);
}
} /*
* Decodes input (unsigned char) into output (uint32_t). Assumes len is a
* multiple of 4.
*/
static void
Decode(uint32_t * output, unsigned char *input, unsigned int len)
{
unsigned int i, j; for (i = , j = ; j < len; i++, j += )
output[i] = ((uint32_t) input[j]) | (((uint32_t) input[j + ]) << ) |
(((uint32_t) input[j + ]) << ) | (((uint32_t) input[j + ]) << );
} /*
* MD5 basic transformation. Transforms state based on block.
*/
static void
MD5Transform(uint32_t state[], unsigned char block[])
{
uint32_t a = state[], b = state[], c = state[], d = state[],
x[]; Decode(x, block, ); /* Round 1 */
FF(a, b, c, d, x[], S11, 0xd76aa478); /* 1 */
FF(d, a, b, c, x[], S12, 0xe8c7b756); /* 2 */
FF(c, d, a, b, x[], S13, 0x242070db); /* 3 */
FF(b, c, d, a, x[], S14, 0xc1bdceee); /* 4 */
FF(a, b, c, d, x[], S11, 0xf57c0faf); /* 5 */
FF(d, a, b, c, x[], S12, 0x4787c62a); /* 6 */
FF(c, d, a, b, x[], S13, 0xa8304613); /* 7 */
FF(b, c, d, a, x[], S14, 0xfd469501); /* 8 */
FF(a, b, c, d, x[], S11, 0x698098d8); /* 9 */
FF(d, a, b, c, x[], S12, 0x8b44f7af); /* 10 */
FF(c, d, a, b, x[], S13, 0xffff5bb1); /* 11 */
FF(b, c, d, a, x[], S14, 0x895cd7be); /* 12 */
FF(a, b, c, d, x[], S11, 0x6b901122); /* 13 */
FF(d, a, b, c, x[], S12, 0xfd987193); /* 14 */
FF(c, d, a, b, x[], S13, 0xa679438e); /* 15 */
FF(b, c, d, a, x[], S14, 0x49b40821); /* 16 */ /* Round 2 */
GG(a, b, c, d, x[], S21, 0xf61e2562); /* 17 */
GG(d, a, b, c, x[], S22, 0xc040b340); /* 18 */
GG(c, d, a, b, x[], S23, 0x265e5a51); /* 19 */
GG(b, c, d, a, x[], S24, 0xe9b6c7aa); /* 20 */
GG(a, b, c, d, x[], S21, 0xd62f105d); /* 21 */
GG(d, a, b, c, x[], S22, 0x2441453); /* 22 */
GG(c, d, a, b, x[], S23, 0xd8a1e681); /* 23 */
GG(b, c, d, a, x[], S24, 0xe7d3fbc8); /* 24 */
GG(a, b, c, d, x[], S21, 0x21e1cde6); /* 25 */
GG(d, a, b, c, x[], S22, 0xc33707d6); /* 26 */
GG(c, d, a, b, x[], S23, 0xf4d50d87); /* 27 */
GG(b, c, d, a, x[], S24, 0x455a14ed); /* 28 */
GG(a, b, c, d, x[], S21, 0xa9e3e905); /* 29 */
GG(d, a, b, c, x[], S22, 0xfcefa3f8); /* 30 */
GG(c, d, a, b, x[], S23, 0x676f02d9); /* 31 */
GG(b, c, d, a, x[], S24, 0x8d2a4c8a); /* 32 */ /* Round 3 */
HH(a, b, c, d, x[], S31, 0xfffa3942); /* 33 */
HH(d, a, b, c, x[], S32, 0x8771f681); /* 34 */
HH(c, d, a, b, x[], S33, 0x6d9d6122); /* 35 */
HH(b, c, d, a, x[], S34, 0xfde5380c); /* 36 */
HH(a, b, c, d, x[], S31, 0xa4beea44); /* 37 */
HH(d, a, b, c, x[], S32, 0x4bdecfa9); /* 38 */
HH(c, d, a, b, x[], S33, 0xf6bb4b60); /* 39 */
HH(b, c, d, a, x[], S34, 0xbebfbc70); /* 40 */
HH(a, b, c, d, x[], S31, 0x289b7ec6); /* 41 */
HH(d, a, b, c, x[], S32, 0xeaa127fa); /* 42 */
HH(c, d, a, b, x[], S33, 0xd4ef3085); /* 43 */
HH(b, c, d, a, x[], S34, 0x4881d05); /* 44 */
HH(a, b, c, d, x[], S31, 0xd9d4d039); /* 45 */
HH(d, a, b, c, x[], S32, 0xe6db99e5); /* 46 */
HH(c, d, a, b, x[], S33, 0x1fa27cf8); /* 47 */
HH(b, c, d, a, x[], S34, 0xc4ac5665); /* 48 */ /* Round 4 */
II(a, b, c, d, x[], S41, 0xf4292244); /* 49 */
II(d, a, b, c, x[], S42, 0x432aff97); /* 50 */
II(c, d, a, b, x[], S43, 0xab9423a7); /* 51 */
II(b, c, d, a, x[], S44, 0xfc93a039); /* 52 */
II(a, b, c, d, x[], S41, 0x655b59c3); /* 53 */
II(d, a, b, c, x[], S42, 0x8f0ccc92); /* 54 */
II(c, d, a, b, x[], S43, 0xffeff47d); /* 55 */
II(b, c, d, a, x[], S44, 0x85845dd1); /* 56 */
II(a, b, c, d, x[], S41, 0x6fa87e4f); /* 57 */
II(d, a, b, c, x[], S42, 0xfe2ce6e0); /* 58 */
II(c, d, a, b, x[], S43, 0xa3014314); /* 59 */
II(b, c, d, a, x[], S44, 0x4e0811a1); /* 60 */
II(a, b, c, d, x[], S41, 0xf7537e82); /* 61 */
II(d, a, b, c, x[], S42, 0xbd3af235); /* 62 */
II(c, d, a, b, x[], S43, 0x2ad7d2bb); /* 63 */
II(b, c, d, a, x[], S44, 0xeb86d391); /* 64 */ state[] += a;
state[] += b;
state[] += c;
state[] += d; /*
* Zeroize sensitive information.
*/
memset((unsigned char *) x, , sizeof(x));
} /**
* MD5Init:
* @context: MD5 context to be initialized.
*
* Initializes MD5 context for the start of message digest computation.
**/
void
MD5Init(MD5_CTX * context)
{
context->count[] = context->count[] = ;
/* Load magic initialization constants. */
context->state[] = 0x67452301;
context->state[] = 0xefcdab89;
context->state[] = 0x98badcfe;
context->state[] = 0x10325476;
} /**
* MD5Update:
* @context: MD5 context to be updated.
* @input: pointer to data to be fed into MD5 algorithm.
* @inputLen: size of @input data in bytes.
*
* MD5 block update operation. Continues an MD5 message-digest operation,
* processing another message block, and updating the context.
**/ void
MD5Update(MD5_CTX * context, unsigned char *input, unsigned int inputLen)
{
unsigned int i, index, partLen; /* Compute number of bytes mod 64 */
index = (unsigned int) ((context->count[] >> ) & 0x3F); /* Update number of bits */
if ((context->count[] += ((uint32_t) inputLen << )) < ((uint32_t) inputLen << )) {
context->count[]++;
}
context->count[] += ((uint32_t) inputLen >> ); partLen = - index; /* Transform as many times as possible. */
if (inputLen >= partLen) {
memcpy((unsigned char *) & context->buffer[index], (unsigned char *) input, partLen);
MD5Transform(context->state, context->buffer); for (i = partLen; i + < inputLen; i += ) {
MD5Transform(context->state, &input[i]);
}
index = ;
} else {
i = ;
}
/* Buffer remaining input */
if ((inputLen - i) != ) {
memcpy((unsigned char *) & context->buffer[index], (unsigned char *) & input[i], inputLen - i);
}
} /**
* MD5Final:
* @digest: 16-byte buffer to write MD5 checksum.
* @context: MD5 context to be finalized.
*
* Ends an MD5 message-digest operation, writing the the message
* digest and zeroing the context. The context must be initialized
* with MD5Init() before being used for other MD5 checksum calculations.
**/ void
MD5Final(unsigned char digest[], MD5_CTX * context)
{
unsigned char bits[];
unsigned int index, padLen; /* Save number of bits */
Encode(bits, context->count, ); /*
* Pad out to 56 mod 64.
*/
index = (unsigned int) ((context->count[] >> ) & 0x3f);
padLen = (index < ) ? ( - index) : ( - index);
MD5Update(context, PADDING, padLen); /* Append length (before padding) */
MD5Update(context, bits, );
/* Store state in digest */
Encode(digest, context->state, ); /*
* Zeroize sensitive information.
*/
memset((unsigned char *) context, , sizeof(*context));
} }

Encryption-基础:MD5加密的更多相关文章

  1. Python基础-MD5加密

    import hashlibm = hashlib.md5()#构造一个md5 m.update(b"Hello")#加密前必须转化成二进制字节类型print(m.hexdiges ...

  2. 基础学习14天 MD5加密

    private static string GetMD5(string str) { //创建MD5对象 MD5 md5 = MD5.Create(); //字符串类型转换Wie字节 byte[] b ...

  3. pythone函数基础(10)MD5加密

    导入hashlib模块import hashlibs='yulin123456's.encode()#把数字转换成bytes类型m=hashlib.md5(s.encode())print(m.hex ...

  4. python基础之 反射,md5加密 以及isinstance, type, issubclass内置方法的运用

    内容梗概: 1. isinstance, type, issubclass 2. 区分函数和方法 3. 反射(重点) 4. md5加密 1. isinstance, type, issubclass1 ...

  5. Android对敏感数据进行MD5加密(基础回顾)

    1.在工具类的包下新建一个进行md5加密的工具类MD5Utils.java package com.example.mobilesafe.utils; import java.security.Mes ...

  6. 关于CryptoJS中md5加密以及aes加密的随笔

    最近项目中用到了各种加密,其中就包括从没有接触过得aes加密,因此从网上各种查,官方的一种说法: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学 ...

  7. java中使用MD5加密技术

    在项目中经常会对一些信息进行加密,现在常用的信息加密技术有:MD5.RSA.DES等,今天主要说一下,md5加密,以及如何在java代码根据自己的业务需求使用md5. MD5简介: MD5即Messa ...

  8. MD5加密详解

    MD5加密详解 引言: 我在百度百科上查找到了关于MD5的介绍,我从中摘要一些重要信息: Message Digest Algorithm MD5(中文名为信息摘要算法第五版)为计算机安全领域广泛使用 ...

  9. Python hashlib模块 (主要记录md5加密)

    python提供了一个进行hash加密的模块:hashlib 下面主要记录下其中的md5加密方式(sha1加密一样把MD5换成sha1) >>> import hashlib > ...

  10. android环境下两种md5加密方式

    在平时开发过程中,MD5加密是一个比较常用的算法,最常见的使用场景就是在帐号注册时,用户输入的密码经md5加密后,传输至服务器保存起来.虽然md5加密经常用,但是md5的加密原理我还真说不上来,对md ...

随机推荐

  1. E20190303-hm

    invoke vt. 乞灵,祈求; 提出或授引…以支持或证明; 召鬼; 借助;

  2. IT兄弟连 JavaWeb教程 JSTL常用标签

    1.条件标签 条件标签能够实现Java语言中的if语句以及if-else语句的功能,它包括以下几种: <c:if>:用于实现Java语言中的if语句的功能. <c:choose> ...

  3. performSegueWithIdentifier:sender里边的sender是啥意思

    performSegueWithIdentifier:sender里边的sender是啥意思啊?怎样用啊? [self performSegueWithIdentifier:@"pushSi ...

  4. hadoop 2.5.1单机安装部署伪集群

    环境:ubuntu 14.04 server 64版本 hadoop 2.5.1 jdk 1.6 部署的步骤主要参考了http://blog.csdn.net/greensurfer/article/ ...

  5. 使用MethodSwizzle导致按home app进入后台或者app间切换发生crash的解决方法

    参考文章: 1.http://blog.csdn.net/alincexiaohao/article/details/45913857 2.http://www.cocoachina.com/ios/ ...

  6. [Noip2012普及组]摆花

    Description 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共 m 盆.通过调查顾客的喜好,小明列出了顾客最喜欢的 n 种花,从 1 到 n 标号.为了在门口展出更多种花,规定 ...

  7. Codeforces 1107F(dp)

    怎么就没人解释一下为啥用b排序可以保证正确性呢……太菜了,理解了好久. 时间流逝价值会丢失的背包,类似题洛谷1417 本题与洛谷1417不同之处在于流逝是有截止的. 1.这个dp[j]的含义是:最后跑 ...

  8. bryce1010专题训练——LCT&&树链剖分

    LCT&&树链剖分专题 参考: https://blog.csdn.net/forever_wjs/article/details/52116682

  9. 1-24List三个子类的特点

    List的三个子类的特点 因为三个类都实现了List接口,所以里面的方法都差不多,那这三个类都有什么特点呢? ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高. Vect ...

  10. 540 Single Element in a Sorted Array 有序数组中的单一元素

    给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数.示例 1:输入: [1,1,2,3,3,4,4,8,8]输出: 2 示例 2:输入: [3,3,7,7,10,1 ...