002
17.04.2006, 00:05 Uhr
~damo
Gast
|
vielen Dank ich habe die Algorithmen, und die Muster-Codes sehr viel gelesen, und meine eigene Codes schon geschrieben. ich glaube sie sind fast ähnlich, aber meine Codes kann mir nicht die richtige Lösung geben. also, 1. Fehler kann sein: die Algorithmen brauchen gross Konstante(32 bit) zu addieren, z.B. 0xf0000000 + 0xf0000000, wenn ich mit "unsigned long", dann passiert Überlauf. alle Algorithmen im Internet nicht geschrieben, wie es geht. ich habe auch aber im Muster-Codes gesehen, solche Konstanten auch mit "int" deklariert, ???
2. Fehler kann sein: ich habe die Algorithmen nicht richtig verstanden.
C++: |
#include<iostream> #include<fstream> #include<vector> #include<bitset> using namespace std;
inline unsigned long rotate_left(const unsigned long& x, int n) { return (x << n) | (x >> (32-n)); }
inline void FF(unsigned long& A,unsigned long& B,unsigned long& C,unsigned long& D,unsigned long Mj,int s,unsigned long Ti) // 2 Konstanten addieren passiert Überlauf { unsigned long F = (B & C) | (~B & D); /*unsigned long temp = D; D = C; C = B; A = A + F + Mj + Ti; B = B + rotate_left(A,s); A = temp;*/ // manche Algorithmen so definieren // aber beide kann nicht richtig arbeiten A = A + F + Mj + Ti; A = rotate_left(A,s); A = A +B; } inline void GG(unsigned long& A,unsigned long& B,unsigned long& C,unsigned long& D,unsigned long Mj,int s,unsigned long Ti) { unsigned long G = (B & D) | (C & ~D); /*unsigned long temp = D; D = C; C = B; A = A + G + Mj + Ti; B = B + rotate_left(A,s); A = temp;*/ A = A + G + Mj + Ti; A = rotate_left(A,s); A = A +B; } inline void HH(unsigned long& A,unsigned long& B,unsigned long& C,unsigned long& D,unsigned long Mj,int s,unsigned long Ti) { unsigned long H = (B ^ C ^ D); /*unsigned long temp = D; D = C; C = B; A = A + H + Mj + Ti; B = B + rotate_left(A,s); A = temp;*/ A = A + H + Mj + Ti; A = rotate_left(A,s); A = A +B; } inline void II(unsigned long& A,unsigned long& B,unsigned long& C,unsigned long& D,unsigned long Mj,int s,unsigned long Ti) { unsigned long I = (C ^ (B | ~D)); /*unsigned long temp = D; D = C; C = B; A = A + I + Mj + Ti; B = B + rotate_left(A,s); A = temp;*/ A = A + I + Mj + Ti; A = rotate_left(A,s); A = A +B; } template<class T> void print_v(vector<T>& v) { vector<T> :: iterator pos; for (pos=v.begin();pos<v.end();pos++) cout<<*pos<<' '; cout<<endl; }
int main() { ifstream fin("md5.txt",ios::binary); // Inhalt "", oder "a" fin.seekg(0,ios::end); unsigned long byte = fin.tellg(); cout<<"byte: "<<byte<<endl; // 0 oder 1 int Rest = 56 - byte % 64; char *buf = new char[byte]; fin.seekg(0,ios::beg); fin.read(buf,byte); fin.close();
vector<bool> vb(byte*8); // vector für bit vector< bitset<8> > temp(byte); for (int i=0;i<byte;i++) temp[i] = buf[i]; // change char mit "bitset" to bit // for (int i=0;i<byte;i++) cout<<temp[i]<<endl; for (int i=0,k=0;i<byte;i++) { for (int j=0;j<8;j++) vb[k++] = temp[i][7-j]; } // dann koppiert bit zu vb (bitset von rechts zu links !!!) vb.push_back(1); // vb anhängen '1' if (Rest == 0) vb.resize(byte*8+512); // if mod == 448, anhängen 1+511( Default ==0) else vb.resize(byte*8+Rest*8); // else anhängen '0'
bitset<64> Ende = byte; // anhängen 64 bit Länge for (int i=0;i<64;i++) vb.push_back(Ende[63-i]); print_v(vb); // guck mal (richtig?) cout<<vb.size()<<endl; // 512
unsigned long A = 0X67452301; unsigned long B = 0XEFCDAB89; unsigned long C = 0X98BADCFE; unsigned long D = 0X10325476;
unsigned long a = A; unsigned long b = B; unsigned long c = C; unsigned long d = D;
// einfach sein, File nur 512 bits vector < bitset<32> > M(16); // für Message for (int i=0,k=0;i<16;i++) for (int j=0;j<32;j++) M[i][31-j] = vb[k++]; // teilen Message in 16 Teilen, jede 32 bits, // also 16 mal 32 bit grosse Konstante. // (so Algorithmus zu verstehen ,richtig?)
// Round 1 FF(a, b, c, d, M[ 0].to_ulong(), 7, 0xd76aa478); FF(d, a, b, c, M[ 1].to_ulong(), 12, 0xe8c7b756); FF(c, d, a, b, M[ 2].to_ulong(), 17, 0x242070db); FF(b, c, d, a, M[ 3].to_ulong(), 22, 0xc1bdceee); FF(a, b, c, d, M[ 4].to_ulong(), 7, 0xf57c0faf); FF(d, a, b, c, M[ 5].to_ulong(), 12, 0x4787c62a); FF(c, d, a, b, M[ 6].to_ulong(), 17, 0xa8304613); FF(b, c, d, a, M[ 7].to_ulong(), 22, 0xfd469501); FF(a, b, c, d, M[ 8].to_ulong(), 7, 0x698098d8); FF(d, a, b, c, M[ 9].to_ulong(), 12, 0x8b44f7af); FF(c, d, a, b, M[10].to_ulong(), 17, 0xffff5bb1); FF(b, c, d, a, M[11].to_ulong(), 22, 0x895cd7be); FF(a, b, c, d, M[12].to_ulong(), 7, 0x6b901122); FF(d, a, b, c, M[13].to_ulong(), 12, 0xfd987193); FF(c, d, a, b, M[14].to_ulong(), 17, 0xa679438e); FF(b, c, d, a, M[15].to_ulong(), 22, 0x49b40821); // round 2 GG(a, b, c, d, M[ 1].to_ulong(), 5, 0xf61e2562); GG(d, a, b, c, M[ 6].to_ulong(), 9, 0xc040b340); GG(c, d, a, b, M[11].to_ulong(), 14, 0x265e5a51); GG(b, c, d, a, M[ 0].to_ulong(), 20, 0xe9b6c7aa); GG(a, b, c, d, M[ 5].to_ulong(), 5, 0xd62f105d); GG(d, a, b, c, M[10].to_ulong(), 9, 0x02441453); GG(c, d, a, b, M[15].to_ulong(), 14, 0xd8a1e681); GG(b, c, d, a, M[ 4].to_ulong(), 20, 0xe7d3fbc8); GG(a, b, c, d, M[ 9].to_ulong(), 5, 0x21e1cde6); GG(d, a, b, c, M[14].to_ulong(), 9, 0xc33707d6); GG(c, d, a, b, M[ 3].to_ulong(), 14, 0xf4d50d87); GG(b, c, d, a, M[ 8].to_ulong(), 20, 0x455a14ed); GG(a, b, c, d, M[13].to_ulong(), 5, 0xa9e3e905); GG(d, a, b, c, M[ 2].to_ulong(), 9, 0xfcefa3f8); GG(c, d, a, b, M[ 7].to_ulong(), 14, 0x676f02d9); GG(b, c, d, a, M[12].to_ulong(), 20, 0x8d2a4c8a); // round 3 HH(a, b, c, d, M[ 5].to_ulong(), 4, 0xfffa3942); HH(d, a, b, c, M[ 8].to_ulong(), 11, 0x8771f681); HH(c, d, a, b, M[11].to_ulong(), 16, 0x6d9d6122); HH(b, c, d, a, M[14].to_ulong(), 23, 0xfde5380c); HH(a, b, c, d, M[ 1].to_ulong(), 4, 0xa4beea44); HH(d, a, b, c, M[ 4].to_ulong(), 11, 0x4bdecfa9); HH(c, d, a, b, M[ 7].to_ulong(), 16, 0xf6bb4b60); HH(b, c, d, a, M[10].to_ulong(), 23, 0xbebfbc70); HH(a, b, c, d, M[13].to_ulong(), 4, 0x289b7ec6); HH(d, a, b, c, M[ 0].to_ulong(), 11, 0xeaa127fa); HH(c, d, a, b, M[ 3].to_ulong(), 16, 0xd4ef3085); HH(b, c, d, a, M[ 6].to_ulong(), 23, 0x04881d05); HH(a, b, c, d, M[ 9].to_ulong(), 4, 0xd9d4d039); HH(d, a, b, c, M[12].to_ulong(), 11, 0xe6db99e5); HH(c, d, a, b, M[15].to_ulong(), 16, 0x1fa27cf8); HH(b, c, d, a, M[ 2].to_ulong(), 23, 0xc4ac5665); // round 4 II(a, b, c, d, M[ 0].to_ulong(), 6, 0xf4292244); II(d, a, b, c, M[ 7].to_ulong(), 10, 0x432aff97); II(c, d, a, b, M[14].to_ulong(), 15, 0xab9423a7); II(b, c, d, a, M[ 5].to_ulong(), 21, 0xfc93a039); II(a, b, c, d, M[12].to_ulong(), 6, 0x655b59c3); II(d, a, b, c, M[ 3].to_ulong(), 10, 0x8f0ccc92); II(c, d, a, b, M[10].to_ulong(), 15, 0xffeff47d); II(b, c, d, a, M[ 1].to_ulong(), 21, 0x85845dd1); II(a, b, c, d, M[ 8].to_ulong(), 6, 0x6fa87e4f); II(d, a, b, c, M[15].to_ulong(), 10, 0xfe2ce6e0); II(c, d, a, b, M[ 6].to_ulong(), 15, 0xa3014314); II(b, c, d, a, M[13].to_ulong(), 21, 0x4e0811a1); II(a, b, c, d, M[ 4].to_ulong(), 6, 0xf7537e82); II(d, a, b, c, M[11].to_ulong(), 10, 0xbd3af235); II(c, d, a, b, M[ 2].to_ulong(), 15, 0x2ad7d2bb); II(b, c, d, a, M[ 9].to_ulong(), 21, 0xeb86d391);
A = A + a; B = B + b; C = C + c; D = D + d;
cout<<hex<<D<<endl; cout<<hex<<C<<endl; cout<<hex<<B<<endl; cout<<hex<<A<<endl; // solche Output kann man die richtige Lösung lesen???
// MD5 ("") = d41d8cd98f00b204e9800998ecf8427e // MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661 // MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72 }
|
vielen Dank!
mod edit: BENUTZE DIE CPP TAGS SELBER Dieser Post wurde am 17.04.2006 um 17:06 Uhr von Pablo editiert. |