// md2II.cpp : Defines the entry point for the console application. // /* MD2-II Hash Function */ /* by Alexander PUKALL 2005 */ /* Code free for all, even for commercial software */ /* No restriction to use. Public Domain */ /* Hash a user password in a 528-byte key (4224 bits) */ /* For BLOWFISH II */ /* Microsoft Visual C++ 6.0 code */ /* Based on MD2 by Ronald Rivest 1989 */ /* Users never use passwords long enough and random enough. */ /* This function takes a user password as input and provides */ /* a 528-byte key as output. This 528-byte key is then passed to Blowfish II. */ /* MD2-II can be used to hash passwords for Blowfish by simply */ /* change #define n1 528 to */ /* #define n1 72 for Blowfish 576 bits or */ /* #define n1 56 for Blowfish 448 bits */ /* MD2-II can be used with other algorithms like */ /* #define n1 16 for AES-128 (128 bits) */ /* #define n1 16 for IDEA (128 bits). It avoids IDEA weak keys */ /* #define n1 21 for 3DES (168 bits). */ /* #define n1 24 for AES-192 (192 bits) */ /* #define n1 32 for AES-256 (256 bits) */ /* MD2-II can be used to create a session key from a user password */ /* and initialization vector. */ /* MD2-II is not intended for general use like MD5 or SHA1. */ /* It is only for hashing passwords. */ /* For this purpose, it is completely secure since */ /* the hash is never accessible to an attacker. */ /* So pre-image or collision attacks don't work. */ #include "stdafx.h" #include #include #define n1 528 int x1,x2,i; unsigned char h2[n1]; unsigned char h1[n1*3]; static void init() { x1 = 0; x2 = 0; for (i = 0; i < n1; i++) h2[i] = 0; for (i = 0; i < n1; i++) h1[i] = 0; } static void hashing(unsigned char t1[], size_t b6) { static unsigned char s4[256] = { 13, 199, 11, 67, 237, 193, 164, 77, 115, 184, 141, 222, 73, 38, 147, 36, 150, 87, 21, 104, 12, 61, 156, 101, 111, 145, 119, 22, 207, 35, 198, 37, 171, 167, 80, 30, 219, 28, 213, 121, 86, 29, 214, 242, 6, 4, 89, 162, 110, 175, 19, 157, 3, 88, 234, 94, 144, 118, 159, 239, 100, 17, 182, 173, 238, 68, 16, 79, 132, 54, 163, 52, 9, 58, 57, 55, 229, 192, 170, 226, 56, 231, 187, 158, 70, 224, 233, 245, 26, 47, 32, 44, 247, 8, 251, 20, 197, 185, 109, 153, 204, 218, 93, 178, 212, 137, 84, 174, 24, 120, 130, 149, 72, 180, 181, 208, 255, 189, 152, 18, 143, 176, 60, 249, 27, 227, 128, 139, 243, 253, 59, 123, 172, 108, 211, 96, 138, 10, 215, 42, 225, 40, 81, 65, 90, 25, 98, 126, 154, 64, 124, 116, 122, 5, 1, 168, 83, 190, 131, 191, 244, 240, 235, 177, 155, 228, 125, 66, 43, 201, 248, 220, 129, 188, 230, 62, 75, 71, 78, 34, 31, 216, 254, 136, 91, 114, 106, 46, 217, 196, 92, 151, 209, 133, 51, 236, 33, 252, 127, 179, 69, 7, 183, 105, 146, 97, 39, 15, 205, 112, 200, 166, 223, 45, 48, 246, 186, 41, 148, 140, 107, 76, 85, 95, 194, 142, 50, 49, 134, 23, 135, 169, 221, 210, 203, 63, 165, 82, 161, 202, 53, 14, 206, 232, 103, 102, 195, 117, 250, 99, 0, 74, 160, 241, 2, 113}; int b1,b2,b3,b4,b5; b4=0; while (b6) { for (; b6 && x2 < n1; b6--, x2++) { b5 = t1[b4++]; h1[x2 + n1] = b5; h1[x2 + (n1*2)] = b5 ^ h1[x2]; x1 = h2[x2] ^= s4[b5 ^ x1]; } if (x2 == n1) { b2 = 0; x2 = 0; for (b3 = 0; b3 < (n1+2); b3++) { for (b1 = 0; b1 < (n1*3); b1++) b2 = h1[b1] ^= s4[b2]; b2 = (b2 + b3) % 256; } } } } static void end(unsigned char h4[n1]) { unsigned char h3[n1]; int i, n4; n4 = n1 - x2; for (i = 0; i < n4; i++) h3[i] = n4; hashing(h3, n4); hashing(h2, sizeof(h2)); for (i = 0; i < n1; i++) h4[i] = h1[i]; } int main() { unsigned char h4[n1]; int i,w; /* Example hashing password 16 'A' */ /* Note : hashing(data,len of data) */ unsigned char data[1]={0x41}; unsigned char text[19]; /* strcpy = null terminated string */ init(); for (i=0;i<16;i++) { hashing(data, 1); } end(h4); printf("Md2-II Hash 528 bytes 'AAAAAAAAAAAAAAAA' Tab test:\n\n"); w=1; for (i=0;i