Encryption-基础:MD5加密
环境: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加密的更多相关文章
- Python基础-MD5加密
import hashlibm = hashlib.md5()#构造一个md5 m.update(b"Hello")#加密前必须转化成二进制字节类型print(m.hexdiges ...
- 基础学习14天 MD5加密
private static string GetMD5(string str) { //创建MD5对象 MD5 md5 = MD5.Create(); //字符串类型转换Wie字节 byte[] b ...
- pythone函数基础(10)MD5加密
导入hashlib模块import hashlibs='yulin123456's.encode()#把数字转换成bytes类型m=hashlib.md5(s.encode())print(m.hex ...
- python基础之 反射,md5加密 以及isinstance, type, issubclass内置方法的运用
内容梗概: 1. isinstance, type, issubclass 2. 区分函数和方法 3. 反射(重点) 4. md5加密 1. isinstance, type, issubclass1 ...
- Android对敏感数据进行MD5加密(基础回顾)
1.在工具类的包下新建一个进行md5加密的工具类MD5Utils.java package com.example.mobilesafe.utils; import java.security.Mes ...
- 关于CryptoJS中md5加密以及aes加密的随笔
最近项目中用到了各种加密,其中就包括从没有接触过得aes加密,因此从网上各种查,官方的一种说法: 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学 ...
- java中使用MD5加密技术
在项目中经常会对一些信息进行加密,现在常用的信息加密技术有:MD5.RSA.DES等,今天主要说一下,md5加密,以及如何在java代码根据自己的业务需求使用md5. MD5简介: MD5即Messa ...
- MD5加密详解
MD5加密详解 引言: 我在百度百科上查找到了关于MD5的介绍,我从中摘要一些重要信息: Message Digest Algorithm MD5(中文名为信息摘要算法第五版)为计算机安全领域广泛使用 ...
- Python hashlib模块 (主要记录md5加密)
python提供了一个进行hash加密的模块:hashlib 下面主要记录下其中的md5加密方式(sha1加密一样把MD5换成sha1) >>> import hashlib > ...
- android环境下两种md5加密方式
在平时开发过程中,MD5加密是一个比较常用的算法,最常见的使用场景就是在帐号注册时,用户输入的密码经md5加密后,传输至服务器保存起来.虽然md5加密经常用,但是md5的加密原理我还真说不上来,对md ...
随机推荐
- jQuery 生成随机字符
//获取长度为len的随机字母 //获取长度为len的随机字母 function zimu(len){ len = len || 1; var $chars = 'ABCDEFGHIJKLMNOPQR ...
- MFC控件:listctrl使用方法总结
以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtrl类 SDK:以 “ListView_”开头的一些宏.如 ListView_InsertCol ...
- java日期时间处理集合
本文主要介绍java中日期时间的处理,包括获取时间,时间相加减,格式化等操作. 持续更新中... 时间格式化 //时间格式化 SimpleDateFormat dateFormat = new Sim ...
- TRANSFORM_TEX是做什么的
简单来说,TRANSFORM_TEX主要作用是拿顶点的uv去和材质球的tiling和offset作运算,确保材质球里的缩放和偏移设置是正确的. (v.texcoord就是顶点的uv) 而_MainTe ...
- unity gl 画线
using UnityEngine; using System.Collections; public class TGLLine : MonoBehaviour { private static M ...
- 常用的高级sql查询
1.根据主键id数组批量修改 void updateByIdArr(Integer[] idArr); <update id="updateByIdArr" paramete ...
- C 语言实例 - 复数相加
C 语言实例 - 复数相加 C 语言实例 C 语言实例 使用结构体(struct)将两个复数相加. 我们把形如 a+bi(a,b均为实数)的数称为复数,其中 a 称为实部,b 称为虚部,i 称为虚数单 ...
- 升级log4j到log4j2报错:cannot access org.apache.http.annotation.NotThreadSafe
问题与分析 今天把项目的log4j的依赖改成了log4j2的依赖后,发现使用Maven打包时报错如下: [ERROR] Failed to execute goal org.apache.maven. ...
- [題解](二分答案/單調隊列)luogu_P1419尋找段落
果然又抄的題解... 顯然答案具有單調性,而對于平均數計算的式子我們移一下項, 若s[l..r]>mid*(r-l+1)无解, 於是我們把每個數都減去一個mid,看和的正負即可,如果為正就可能有 ...
- laravel配合swoole使用总结
最近对接硬件做了两个项目,用到了swoole 第一个是门禁系统,需要远程开门.离线报警.定时开门.离线刷卡等功能 1.远程开门: 目前用cli创建个临时客户端连接服务端发送命令,服务端处理完成后客户端 ...