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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #include "stdlib.h" #include <stdio.h> #include <string.h>
#define ROUND 16
int sBox[16] = {2, 10, 4, 12, 1, 3, 9, 14, 7, 11, 8, 6, 5, 0, 15, 13}; int rBox[16] = {13, 4, 0, 5, 2, 12, 11, 8, 10, 6, 1, 9, 3, 15, 7, 14};
void hex_to_bytes(const char *hex_str, unsigned char *bytes, size_t bytes_len) { size_t hex_len = strlen(hex_str); if (hex_len % 2 != 0 || hex_len / 2 > bytes_len) { fprintf(stderr, "Invalid hex string length.\n"); return; }
for (size_t i = 0; i < hex_len / 2; i++) { sscanf(hex_str + 2 * i, "%2hhx", &bytes[i]); } }
void derive_round_key(unsigned int key, unsigned char *round_key, int length) {
unsigned int tmp = key; for (int i = 0; i < length / 16; i++) { memcpy(round_key + i * 16, &tmp, 4); tmp++; memcpy(round_key + i * 16 + 4, &tmp, 4); tmp++; memcpy(round_key + i * 16 + 8, &tmp, 4); tmp++; memcpy(round_key + i * 16 + 12, &tmp, 4); tmp++; } }
int rb[256]; void reverseBits(unsigned char *state) { unsigned char temp[16]; for (int i = 0; i < 16; i++) { temp[15 - i] = rb[state[i]]; } for (int i = 0; i < 16; i++) { state[i] = temp[i]; } }
void sBoxTransform(unsigned char *state) { for (int i = 0; i < 16; i++) { int lo = sBox[state[i] & 0xF]; int hi = sBox[state[i] >> 4]; state[i] = (hi << 4) | lo; } } void rBoxTransform(unsigned char *state) { for (int i = 0; i < 16; i++) { int lo = rBox[state[i] & 0xF]; int hi = rBox[state[i] >> 4]; state[i] = (hi << 4) | lo; } }
void leftShiftBytes(unsigned char *state) { unsigned char temp[16]; for (int i = 0; i < 16; i += 4) { temp[i + 0] = state[i + 2] >> 5 | (state[i + 1] << 3); temp[i + 1] = state[i + 3] >> 5 | (state[i + 2] << 3); temp[i + 2] = state[i + 0] >> 5 | (state[i + 3] << 3); temp[i + 3] = state[i + 1] >> 5 | (state[i + 0] << 3); } for (int i = 0; i < 16; i++) { state[i] = temp[i]; } }
void rightShiftBytes(unsigned char *state) { unsigned char temp[16]; for (int i = 0; i < 16; i += 4) { temp[i + 0] = (state[i + 2] << 5) | (state[i + 3] >> 3); temp[i + 1] = (state[i + 0] >> 3) | (state[i + 3] << 5); temp[i + 2] = (state[i + 0] << 5) | (state[i + 1] >> 3); temp[i + 3] = (state[i + 1] << 5) | (state[i + 2] >> 3); } for (int i = 0; i < 16; i++) { state[i] = temp[i]; } }
void addRoundKey(unsigned char *state, unsigned char *roundKey, unsigned int round) { for (int i = 0; i < 16; i++) state[i] ^= roundKey[i + round * 16]; }
void dump(unsigned char *d) { for (int i = 0; i < 16; i++) { printf("%02X", d[i]); } printf("\n"); }
void encrypt(unsigned char *password, unsigned int key, unsigned char *ciphertext) { unsigned char roundKeys[16 * ROUND] = {};
derive_round_key(key, roundKeys, 16 * ROUND);
unsigned char state[16]; memcpy(state, password, 16);
for (int round = 0; round < ROUND; round++) { reverseBits(state); sBoxTransform(state); leftShiftBytes(state); addRoundKey(state, roundKeys, round); }
memcpy(ciphertext, state, 16); }
void decrypt(unsigned char *ciphertext, unsigned int key, unsigned char *password) { unsigned char roundKeys[16 * ROUND] = {}; derive_round_key(key, roundKeys, 16 * ROUND); unsigned char state[16]; memcpy(state, ciphertext, 16); for (int round = 15; round >= 0; round--) { addRoundKey(state, roundKeys, round); rightShiftBytes(state); rBoxTransform(state); reverseBits(state); } memcpy(password, state, 16); }
int main() { for (int i = 0; i <= 255; i++) { for (int u = 0; u < 8; u++) rb[i] |= ((i >> u) & 1) << (7 - u); } unsigned char password[] = "pwd:1234xxxxabcd"; unsigned int key = 0xF0FFFFFF; unsigned char ciphertext[16]; unsigned char passback[17] = {0}; printf("Password: \n"); printf("%s\n", password);
encrypt(password, key, ciphertext); decrypt(ciphertext, key, passback); printf("%s\n", passback);
memcpy(ciphertext, "\x99\xF2\x98\x0A\xAB\x4B\xE8\x64\x0D\x8F\x32\x21\x47\xCB\xA4\x09", 16); unsigned int s = 0xf0000000; unsigned int e = 0x00000000; for (unsigned int key = s; key != e; key++) { if ((key & ((1 << 14) - 1)) == 0) { printf("key = %x (%.2lf%%)\n", key, (key - s) * 100. / (e - s)); } decrypt(ciphertext, key, passback); if (passback[0] == 'p' && passback[1] == 'w' && passback[2] == 'd' && passback[3] == ':') { printf("%x\n", key); printf("%16s\n", passback); break; } } }
|