环境: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. JAVA基础--JAVA API常见对象(包装类和正则)12

    一.基本类型包装类 1.基本类型包装类介绍 8种基本类型: byte  short    int    long    float   double   char   boolean 这8种基本类型它 ...

  2. 138. Copy List with Random Pointer (not do it by myself)

    A linked list is given such that each node contains an additional random pointer which could point t ...

  3. 51nod 1049【经典】

    自己模拟,全靠体会~ #include <cstdio> #include <stack> #include <iostream> #include <str ...

  4. bzoj 4873: [Shoi2017]寿司餐厅【最大权闭合子图】

    有正负收益,考虑最小割 因为有依赖关系,所以考虑最大权闭合子图 首先对每个d[i][j]建个点,正权连(s,id[i][j],d[i][j])并加到ans上,负权连(id[i][j],t,-d[i][ ...

  5. [Xcode 实际操作]九、实用进阶-(11)系统本地通知的创建和使用

    目录:[Swift]Xcode实际操作 本文将演示系统本地通知的创建和使用. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //引入需要 ...

  6. [Swift]快速反向平方根 | Fast inverse square root

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. hibernate Day2 案例代码

    1.编写实体类Person package com.icss.pojo; public class Person { private int uid; private String uname; pr ...

  8. 详解JS作用域(一)

    一.什么是作用域 存储和访问变量,是任何一种编程语言最基本的功能之一,变量存在哪里?程序需要时如何找到它?这些问题需要一套良好的规则来规范,这套规则,就成为作用域. 二.编译原理 js通常归类为解释语 ...

  9. VMware下OSSIM 5.2.0的下载、安装和初步使用(图文详解)

    不多说,直接上干货! 入门阶段不建议选用最新的版本. 采用OSSIM 4.11 到 OSSIM5.0.3 之间任何版本做实验,sensor的状态都会是“V”.   建议,入门,采用OSSIM5.0.0 ...

  10. 一些API

    /** * Goto the specified frame index, and pause at this index. * @param startIndex The animation wil ...