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 ...
随机推荐
- 2.Python基础认识(格式化输出,while语句,运算符,编码,单位转化)
Python基础认识 1.字符串的格式化初识及占位符的简单应用 字符串的格式化 按照既定的要求进行有规定排版的一种输出方式. #我们想要输出的格式如下: ----------------------- ...
- 单片机C基本编程规范
为了提高源程序的质量和可维护性,从而最终提高软件产品生产力,特编写此规范.本标准规定了程序设计人员进行程序设计时必须遵循的规范.本规范主要针对单片机编程语言和08编译器而言,包括排版.注释.命名.变量 ...
- 最新apple邓白氏码申请地址
时间:2015-11-04 https://developer.apple.com/program/enroll/dunsLookupForm.action
- Codeforces 1138B(列方程枚举)
构造模拟要分情况讨论感觉不是够本质,然后官解是因为只有四个量所以可以根据限制条件列两个方程,再枚举一下解就可以了. const int maxn = 5000 + 5; int n, c[maxn], ...
- centos 6.x下pxe+tftp+http+kickstart无人值守安装操作系统
1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过 ...
- nodejs 学习(4) express+mongoose
一.关于安装和启动: 1.设置环境变量:D:\Program Files\MongoDB\bin 2.启动时需要cd到bin 目录,然后 mongod --dbpath "D:\mongdb ...
- Java GUI 事件监听
现在使用的仍是AWT的事件模型.涉及到3类对象: Event Source:事件源,即事件发生所在的组件 Event:事件,封装了此次事件的相关信息 Event Listener:事件监听器,监听事件 ...
- 爬虫的两种解析方式 xpath和bs4
1.xpath解析 from lxml import etree 两种方式使用:将html文档变成一个对象,然后调用对象的方法去查找指定的节点 (1)本地文件 tree = etree.parse(文 ...
- JS动态获取项目名以及获取URL地址中的参数
在项目当中我们可能会遇到例如改变的项目名称之后,相对应的地址就需要改变,为了减少工作量,将地址当中的项目名这一块写成动态获取的,那么最关键一点就是我要先获取它,再进行操作: 知识点整理,话不多说,直接 ...
- FPGA的嵌入式RAM
FPGA中的嵌入式RAM分为两种:专用的BRAM和分布是RAM(用LUT实现的).这两种RAM又可以配置成单端口和双端口的RAM和ROM.双端口RAM又可以根据读写地址是否在同一块分为Double P ...