1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| #include "md5.cpp" #include <iostream> #include <unordered_map> #include <string>
const int BLOCK_LENGTH = 16; const int ROUND_COUNT = 16; typedef unsigned char uchar;
uchar sbox[] = {210, 213, 115, 178, 122, 4, 94, 164, 199, 230, 237, 248, 54, 217, 156, 202, 212, 177, 132, 36, 245, 31, 163, 49, 68, 107, 91, 251, 134, 242, 59, 46, 37, 124, 185, 25, 41, 184, 221, 63, 10, 42, 28, 104, 56, 155, 43, 250, 161, 22, 92, 81, 201, 229, 183, 214, 208, 66, 128, 162, 172, 147, 1, 74, 15, 151, 227, 247, 114, 47, 53, 203, 170, 228, 226, 239, 44, 119, 123, 67, 11, 175, 240, 13, 52, 255, 143, 88, 219, 188, 99, 82, 158, 14, 241, 78, 33, 108, 198, 85, 72, 192, 236, 129, 131, 220, 96, 71, 98, 75, 127, 3, 120, 243, 109, 23, 48, 97, 234, 187, 244, 12, 139, 18, 101, 126, 38, 216, 90, 125, 106, 24, 235, 207, 186, 190, 84, 171, 113, 232, 2, 105, 200, 70, 137, 152, 165, 19, 166, 154, 112, 142, 180, 167, 57, 153, 174, 8, 146, 194, 26, 150, 206, 141, 39, 60, 102, 9, 65, 176, 79, 61, 62, 110, 111, 30, 218, 197, 140, 168, 196, 83, 223, 144, 55, 58, 157, 173, 133, 191, 145, 27, 103, 40, 246, 169, 73, 179, 160, 253, 225, 51, 32, 224, 29, 34, 77, 117, 100, 233, 181, 76, 21, 5, 149, 204, 182, 138, 211, 16, 231, 0, 238, 254, 252, 6, 195, 89, 69, 136, 87, 209, 118, 222, 20, 249, 64, 130, 35, 86, 116, 193, 7, 121, 135, 189, 215, 50, 148, 159, 93, 80, 45, 17, 205, 95 };
int p[] = {3, 9, 0, 1, 8, 7, 15, 2, 5, 6, 13, 10, 4, 12, 11, 14};
uchar dbox[] = {221, 62, 140, 111, 5, 213, 225, 242, 157, 167, 40, 80, 121, 83, 93, 64, 219, 253, 123, 147, 234, 212, 49, 115, 131, 35, 160, 191, 42, 204, 175, 21, 202, 96, 205, 238, 19, 32, 126, 164, 193, 36, 41, 46, 76, 252, 31, 69, 116, 23, 247, 201, 84, 70, 12, 184, 44, 154, 185, 30, 165, 171, 172, 39, 236, 168, 57, 79, 24, 228, 143, 107, 100, 196, 63, 109, 211, 206, 95, 170, 251, 51, 91, 181, 136, 99, 239, 230, 87, 227, 128, 26, 50, 250, 6, 255, 106, 117, 108, 90, 208, 124, 166, 192, 43, 141, 130, 25, 97, 114, 173, 174, 150, 138, 68, 2, 240, 207, 232, 77, 112, 243, 4, 78, 33, 129, 125, 110, 58, 103, 237, 104, 18, 188, 28, 244, 229, 144, 217, 122, 178, 163, 151, 86, 183, 190, 158, 61, 248, 214, 161, 65, 145, 155, 149, 45, 14, 186, 92, 249, 198, 48, 59, 22, 7, 146, 148, 153, 179, 195, 72, 137, 60, 187, 156, 81, 169, 17, 3, 197, 152, 210, 216, 54, 37, 34, 134, 119, 89, 245, 135, 189, 101, 241, 159, 226, 180, 177, 98, 8, 142, 52, 15, 71, 215, 254, 162, 133, 56, 231, 0, 218, 16, 1, 55, 246, 127, 13, 176, 88, 105, 38, 233, 182, 203, 200, 74, 66, 73, 53, 9, 220, 139, 209, 118, 132, 102, 10, 222, 75, 82, 94, 29, 113, 120, 20, 194, 67, 11, 235, 47, 27, 224, 199, 223, 85 };
int q[] = {2, 3, 7, 0, 12, 8, 9, 5, 4, 1, 11, 14, 13, 10, 15, 6};
inline void mxor(uchar* a, uchar* b, uchar* tmp) { for(int i = 0; i < BLOCK_LENGTH; i++) { tmp[i] = a[i] ^ b[i]; } }
inline void copy(uchar* from, uchar* to) { for(int i = 0; i < BLOCK_LENGTH; i++) { to[i] = from[i]; } }
void fun(uchar* key, uchar* ct, uchar* tmp) { for(int i = 0; i < ROUND_COUNT; i++) { mxor(ct, key, tmp); copy(tmp, ct); for(int i = 0; i < BLOCK_LENGTH; i++) { ct[i] = sbox[ct[i]]; } uchar nct[BLOCK_LENGTH]; for(int i = 0; i < BLOCK_LENGTH; i++) { nct[i] = ct[p[i]]; } copy(nct, ct); } }
void dec(uchar* key, uchar* ct, uchar* tmp) { for(int i = 0; i < ROUND_COUNT; i++) { uchar nct[BLOCK_LENGTH]; for(int i = 0; i < BLOCK_LENGTH; i++) { nct[i] = ct[q[i]]; } copy(nct, ct);
for(int i = 0; i < BLOCK_LENGTH; i++) { ct[i] = dbox[ct[i]]; } mxor(ct, key, tmp); copy(tmp, ct); } }
int main() { std::unordered_map<std::string, std::string> map; uchar* md5key = new uchar[BLOCK_LENGTH]; uchar tmp[BLOCK_LENGTH]; uchar hexkey[] = {0, 0, 0};
for(int i = 0; i < 256; i++) { for(int j = 0; j < 256; j++) { for(int k = 0; k < 256; k++) { uchar pt[] = "16 bit plaintext"; copy(MD5((char*)hexkey, 3).digest, md5key); fun(md5key, pt, tmp);
std::string s1((char*)pt, 16); std::string s2((char*)hexkey, 3);
map[s1] = s2; hexkey[2] += 1; } hexkey[1] += 1; } std::cout << i << std::endl; hexkey[0] += 1; }
for(int i = 0; i < 256; i++) { for(int j = 0; j < 256; j++) { for(int k = 0; k < 256; k++) { uchar pt[] = "\x04g\xa5*\xfa\x8f\x15\xcf\xb8\xf0\xea@6Zf\x92"; copy(MD5((char*)hexkey, 3).digest, md5key); dec(md5key, pt, tmp);
std::string s1((char*)pt); auto ptr = map.find(s1);
if(ptr != map.end()) { std::cout << "Key found" << std::endl; uchar key[3]; uchar flag[] = "\x04\xb3NZ\xf4\xa1\xf5&\x0f`C\xb8\xb9\xab\xb4\xf8"; key[0] = (*ptr).second.c_str()[0]; key[1] = (*ptr).second.c_str()[1]; key[2] = (*ptr).second.c_str()[2]; copy(MD5((char*)hexkey, 3).digest, md5key); dec(md5key, flag, tmp); copy(MD5((char*)key, 3).digest, md5key); dec(md5key, flag, tmp); std::cout << std::string((char*)flag, 16) << std::endl; return 0; } hexkey[2] += 1; } hexkey[1] += 1; } std::cout << i << std::endl; hexkey[0] += 1; } return 0; }
|