C Language MD5 encryption

Source: Internet
Author: User
Tags decrypt fread md5 encryption strlen

Md5.h

#include <stdio.h> #include <string.h>/* POINTER defines a generic POINTER type */typedef unsigned char * point er;/* UINT2 defines a-byte word *///typedef unsigned short int uint2;/* UINT4 defines a four byte word */typedef unsig Ned Long int uint4;/* MD5 context. */typedef struct {UINT4 state[4];                                   /* State (ABCD) */uint4 count[2];        /* Number of bits, modulo 2^64 (lsb first) */unsigned char buffer[64];                         /* Input buffer */} md5_ctx;void Md5init (Md5_ctx *context); void Md5update (Md5_ctx *context, unsigned char *input, unsign Ed int inputlen), void md5updaterstring (Md5_ctx *context,const char *string), int md5fileupdatefile (Md5_ctx *context, Char *filename), void md5final (unsigned char digest[16], md5_ctx *context), void MDString (char *string,unsigned char diges T[16]); int Md5file (char *filename,unsigned char digest[16]);

Md5.c

/* md5c.  C-rsa Data Security, Inc., MD5 message-digest algorithm *//* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. Allrights reserved. License to copy and use this software are granted provided that ITIS identified as the "RSA Data Security, Inc. MD5 Message -digestalgorithm "in all material mentioning or referencing this softwareor this function. License is also granted to make and use derivative works providedthat such works be identified as "derived from the RSA D Atasecurity, Inc. MD5 message-digest algorithm "in any materialmentioning or referencing the derived work. RSA Data Security, Inc. makes no representations concerning eitherthe merchantability of this software or the suitability of Thissoftware for any particular purpose. It is provided ' as is ' without express or implied warranty of any kind. These notices must is retained in any copies of all part of thisdocumentation and/or software. */#include "md5.h"/* Constants for Md5transform routine.*/#define S11 7#defineS12 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 21static void md5_memcpy (POINTER output, POINTE  R input, unsigned int len), static void Md5transform (UINT4 state[4], unsigned char block[64]), static void Encode (unsigned  Char *output, UINT4 *input, unsigned int len), static void Md5_memset (POINTER output, int value, unsigned int len), static  void Decode (UINT4 *output, unsigned char *input, unsigned int len); static unsigned char padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};/* 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 are separate from addition To prevent recomputation.*/#define FF (A, B, C, D, X, S, ac) {(a) + = F ((b), (c), (d)) + (x) + (UINT4) (AC); (a) = Rotate_left ((a), (s)); (a) + = (b); } #define GG (A, B, C, D, X, S, ac) {(a) + = G ((b), (c), (d)) + (x) + (UINT4) (AC); (a) = Rotate_left ((a), (s)); (a) + = (b); } #define HH (A, B, C, D, X, S, ac) {(a) + = H ((b), (c), (d)) + (x) + (UINT4) (AC); (a) = Rotate_left ((a), (s)); (a) + = (b); } #define II (A, B, C, D, X, S, ac) {(a) + = I ((b), (c), (d)) + (x) + (UINT4) (AC); (a) = Rotate_left ((a), (s)); (a) + = (b); }/* MD5 initialization. Begins an MD5 operation, writing a new context. */void md5init (Md5_ctx *context)/* Context */{context->count[0] = Context->cou NT[1] = 0;/* Load Magic Initialization constants.*/context-&Gt;state[0] = 0x67452301;context->state[1] = 0xefcdab89;context->state[2] = 0x98badcfe;context->state[3] = 0x10325476;} /* 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 */index = (unsigned int) ((Context->count[0] >> 3) & 0x3F);/* Update Number of bits */ if ((context->count[0] + = ((UINT4) Inputlen << 3) < ((UINT4) Inputlen << 3)) context->count[1]++; CONTEXT-&GT;COUNT[1] + = ((UINT4) Inputlen >>);p Artlen = 64-index;/* Transform as many times as possible.*/if (in Putlen >= Partlen) {md5_memcpy (POINTER) &context->buffer[index], (POINTER) input, Partlen); Md5transform (Context->state, Context->buffer); for (i = Partlen; i + < Inputlen; i + =) Md5transform (context ->state, &input[i]); index= 0;} Elsei = 0;/* Buffer remaining input */md5_memcpy ((POINTER) &context->buffer[index], (POINTER) &input[i], Inputlen-i);} /* MD5 finalization. Ends an MD5 message-digest operation, writing the message digest and zeroizing the context. */void md5final (unsigned char digest[16], md5_ctx *context) {unsigned char bits[8];unsign Ed int index, padlen;/* Save number of bits */encode (bits, context->count, 8);/* Pad out to mod 64.*/index = (unsig Ned Int) ((Context->count[0] >> 3) & 0x3f);p Adlen = (Index < 56)? (56-index): (120-index); Md5update (context, PADDING, padlen);/* Append length (before PADDING) */md5update (context, bits, 8);/* Store State in Di Gest */encode (Digest, context->state, +);/* Zeroize Sensitive Information.*/md5_memset ((POINTER) context, 0, sizeof (*context));} /* MD5 basic transformation. Transforms State based on block. */static void Md5transform (UINT4 state[4], unsigned char block[64]) {UINT4 a = State[0], B = state[1], c = state[2], d = state[3], x[16];D Ecode (x, block, +);/* Round 1 */ff (A, B, C, D, x[0], S11, 0xd76aa478); /* 1 */ff (d, a, B, C, x[1], S12, 0xe8c7b756); /* 2 */FF (c, D, a, B, x[2], S13, 0x242070db); /* 3 */ff (b, C, D, A, x[3], S14, 0xc1bdceee); /* 4 */ff (A, B, C, D, x[4], S11, 0XF57C0FAF); /* 5 */ff (d, a, B, C, x[5], S12, 0x4787c62a); /* 6 */ff (c, D, a, B, x[6], S13, 0xa8304613); /* 7 */ff (b, C, D, A, x[7], S14, 0xfd469501); /* 8 */ff (A, B, C, D, x[8], S11, 0X698098D8); /* 9 */ff (d, a, B, C, x[9], S12, 0X8B44F7AF); /* */FF (c, D, a, B, x[10], S13, 0XFFFF5BB1); /* One */ff (b, C, D, A, x[11], S14, 0x895cd7be); /* */FF (A, B, C, D, X[12], S11, 0x6b901122); /* */FF (D, a, B, C, x[13], S12, 0xfd987193); /* */FF (c, D, a, B, x[14], S13, 0xa679438e); /* */FF (b, C, D, A, x[15], S14, 0x49b40821); /* *//* Round 2 */gg (A, B, C, D, x[1], S21, 0xf61e2562); /* */GG (D, a, B, C, x[6], S22, 0xc040b340); /* */GG (c, D, a, B, x[11], S23, 0x265e5a51); /* */GG (b, C, D, A, x[0], S24, 0XE9B6C7AA); /* */GG (A, B, C, D, x[5], S21, 0xd62f105d); /* */GG (D, a, B, C, x[10], S22, 0x2441453); /* */GG (c, D, a, B, x[15], S23, 0xd8a1e681); /* */GG (b, C, D, A, x[4], S24, 0XE7D3FBC8); /* */GG (A, B, C, D, x[9], S21, 0x21e1cde6); /* */GG (D, a, B, C, x[14], S22, 0XC33707D6); /* */GG (c, D, a, B, x[3], S23, 0xf4d50d87); /* */GG (b, C, D, A, x[8], S24, 0x455a14ed); /* */GG (A, B, C, D, X[13], S21, 0xa9e3e905); /* */GG (D, a, B, C, x[2], S22, 0XFCEFA3F8); /* */GG (c, D, a, B, x[7], S23, 0X676F02D9); /* */GG (b, C, D, A, x[12], S24, 0x8d2a4c8a); /* *//* Round 3 */hh (A, B, C, D, x[5], S31, 0xfffa3942); /* */HH (D, a, B, C, x[8], S32, 0x8771f681); /* */HH (c, D, a, B, x[11], S33, 0x6d9d6122); /* */HH (b, C, D, A, x[14], S34, 0xfde5380c); /* */HH (A, B, C, D, x[1], S31, 0xa4beea44); /* PNs */hh (d, a, B, C, x[4], S32, 0x4bdecfa9); /* */HH (c, D, a, B, x[7], S33, 0XF6BB4B60); /* */HH (b, C, D, A, x[10], S34, 0XBEBFBC70); /* */HH (A, B, C, D, X[13], S31, 0X289B7EC6); /* */HH (D, a, B, C, x[0], S32, 0XEAA127FA); /* */HH (c, D, a, B, x[3], S33, 0xd4ef3085); /* */HH (b, C, D, A, x[6], S34, 0X4881D05); /* */HH (A, B, C, D, x[9], S31, 0xd9d4d039); /* */HH (D, a, B, C, x[12], S32, 0xe6db99e5); /* */HH (c, D, a, B, x[15], S33, 0X1FA27CF8); /* */HH (b, C, D, A, x[2], S34, 0xc4ac5665); /* *//* Round 4 */ii (A, B, C, D, x[0], S41, 0xf4292244); /* */II (D, a, B, C, x[7], S42, 0X432AFF97); /* */II (c, D, a, B, x[14], S43, 0XAB9423A7); /* Wuyi */ii (b, C, D, A, x[5], S44, 0xfc93a039); /* */II (A, B, C, D, X[12], S41, 0X655B59C3); /* */II (D, a, B, C, x[3], S42, 0x8f0ccc92); /* */II (c, D, a, B, x[10], S43, 0xffeff47d); /* */II (b, C, D, A, x[1], S44, 0X85845DD1); /* */II (A, B, C, D, x[8], S41, 0x6fa87e4f); /* */II (D, a, B, C, x[15], S42, 0XFE2CE6E0); /* */II (c, D, a, B, x[6], S43, 0xa3014314); /* */II (b, C, D, A, x[13], S44, 0X4E0811A1); /* */II (A, B, C, D, x[4], S41, 0xf7537e82); /* */II (D, a, B, C, x[11], S42, 0xbd3af235); /* */II (c, D, a, B, x[2], S43, 0X2AD7D2BB); /* */II (b, C, D, A, x[9], S44, 0xeb86d391);  /* */state[0] + = a;state[1] + b;state[2] + c;state[3] + = d;/* zeroize sensitive Information.*/md5_memset ((POINTER) x, 0, sizeof (x));} /* encodes input (UINT4) into output (unsigned char). Assumes Len is a multiple of 4. */static void Encode (unsigned char *output, UINT4 *input, unsigned int len) {unsigned int i, j;for (i = 0, j = 0; J < l En i++, J + = 4) {Output[j] = (unsigned char) (Input[i] & 0xff); output[j+1] = (unsigned char) ((Input[i] >> 8) & 0  XFF) output[j+2] = (unsigned char) ((Input[i] >>) & 0xff); output[j+3] = (unsigned char) ((Input[i] >> 24) & 0xFF);}} /* Decodes input (unsigned char) into output (UINT4). Assumes Len is a multiple of 4. */static void Decode (UINT4 *output, unsigned char *input, unsigned int len) {unsigned int i, j;for (i = 0, j = 0; j < Len; i++, J + + 4) output[i] = ((UINT4) input[j]) | (((UINT4) input[j+1]) << 8) | (((UINT4) input[j+2]) << 16) | (((UINT4) input[j+3]) << 24);} /* note:replace ' for loop ' with standard memcpy if possible. */static void md5_memcpy (POINTER output, POINTER input, unsigned int len) {unsigned int i;for (i = 0; i < len; i++) OUTP Ut[i] = Input[i];} /* note:replace ' for loop ' with standard memset if possible. */static void Md5_memset (POINTER output, int value, unsigned int len) {unsigned int i;for (i = 0; i < len; i++) ((char * ) (output) [I] = (char) value;} /* Digests a string and prints the result. */void MDString (char *string,unsigned char digest[16]) {md5_ctx context;unsigned int len = strlen (string); Md5init (&context); Md5update (&context, (unsigned char *) string, len); Md5final (Digest, &context);} /* Digests a file and prints the result. */int Md5file (char *filename,unsigned char digest[16]) {FILE *file; Md5_ctx context;int len;unsigned Char buffer[1024];if ((file = fopen (filename, "RB")) = = NULL) return-1;else {md5init (&A Mp;context), while (len = fread (buffer, 1, 1024x768, file)) md5update (&context, buffer, Len); Md5final (Digest, &context); fclose (file);} return 0;} void Md5updaterstring (Md5_ctx *context,const char *string) {unsigned int len = strlen (string); Md5update (context, (unsigned char *) string, len);} int Md5fileupdatefile (md5_ctx *context,char *filename) {file *file;int len;unsigned char buffer[1024];if (file = fopen (f  Ilename, "RB")) = = NULL) return-1;else {while (len = fread (buffer, 1, 1024x768, file)) md5update (context, buffer, Len); fclose (file);} return 0;}      int main (int argc,char *argv) {int i;      Md5_ctx MD5;                          Md5init (&AMP;MD5);       Initialize the structure for MD5 encryption unsigned char DECRYPT[17];    The result of storing the encrypted char *encrypt = "123456";   Md5update (&md5,encrypt,strlen ((char *) encrypt));       Encrypt the characters you want to encrypt md5final (DECRYPT,&AMP;MD5);                                      Get the final result printf ("Before encryption:%s\n after encryption:", encrypt);      for (i=0; i<16; i++) {printf ("%02x", Decrypt[i]);    } printf ("\n\n\n encryption ends!\n"); return 1;}

C language MD5 encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.