环境: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. C++里的强制类型转换符reinterpret_cast、static_cast 、dynamic_cast、const_cast 区别

    C 风格(C-style)强制转型如下: (T) exdivssion // cast exdivssion to be of type T 函数风格(Function-style)强制转型使用这样的 ...

  2. 51nod1416(dfs)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1416 题意:中文题诶- 思路:dfs 搜索同一颜色的点.. 只 ...

  3. uoj#266. 【清华集训2016】Alice和Bob又在玩游戏(博弈论)

    传送门 完了我连sg函数是个啥都快忘了 设\(sg[u]\)为以\(u\)为根节点的子树的\(sg\)函数值,\(rem[u]\)表示\(u\)到根节点的路径删掉之后剩下的游戏的异或值 根节点\(u\ ...

  4. 00 | QPS

    每秒查询率 QPS Query Per Second 某个查询服务器 在 规定时间内 处理了多少流量 对应的fetches/sec,即每秒响应请求数,就是最大吞吐量 原理:每天80%的访问集中在20% ...

  5. ACM2017Tsukuba:H - Homework

    第一问求最多,不需要区分数学作业和信息作业,直接模拟就行了 第二问考虑每天只能产生1的贡献,每天拆成两个点,限制每天只能有1的贡献,剩下的源点连数学作业,信息作业连汇点,再将数学作业和信息作业连能连的 ...

  6. 集合框架Collection<E>接口

  7. [題解]hdu_6412公共子序列

    https://blog.csdn.net/nka_kun/article/details/81902421 #include<bits/stdc++.h> #define ll long ...

  8. Linux (二)

    PS :显示系统进程 -a :显示所有进程(包括其他用户的进程) -u :用户以及其他详细信息 -x :显示没有控制终端的进程 -ef :显示所有 top :用于动态地监视进程活动与系统负载的信息 p ...

  9. Appium禁止appium setting和unlock在设备上重复安装

    1.文件:/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-android-dri ...

  10. getAnnotation为null的坑

    在写一个基于SpringAOP的权限控制的. 自己定义了一个注解,然后逻辑代码需要通过获取自定义注解的一个属性来进行权限控制. 下面简单上一下关键代码: 自定义注解: @Documented //有关 ...