DataChunk DESECBEncrypt(DataChunk messageChunk, Block key) { DataChunk cipherChunk = initDataChunk(messageChunk.iLength - messageChunk.iLength % 8 + 16); Block message = initBlock(64), cipher; KeySchedule k; int i; cipher = initBlock(64); k = DESKeySchedule(key); for (i = 0; i < messageChunk.iLength / 8; i++) { memcpy(message, messageChunk.block + 8 * i, 8); cipher = DESEncrypt(message, k); memcpy(cipherChunk.block + 8 * i, cipher, 8); } free(message); // If the length is not an integral multiple of 8. if (messageChunk.iLength % 8) { memcpy((message = initBlock(64)), messageChunk.block + 8 * i, messageChunk.iLength % 8); cipher = DESEncrypt(message, k); memcpy(cipherChunk.block + 8 * i, cipher, 8); } return (cipherChunk); }
int main(void) { DataChunk messageChunk = initDataChunk(150), cipherChunk, decryptChunk; Block message = initBlock(64), cipher; Block key1, key2, IV; KeySchedule k1, k2; int i; key1 = takeHexInput(); key2 = takeHexInput(); k1 = DESKeySchedule(key1); k2 = DESKeySchedule(key2); /* message = inputString(stdin, 150); cipher = DES3Encrypt(message, k1, k2); printf("\n"); for (i = 0; i < 8; i++) printf("%x ", cipher[i]); printf("\n"); fwrite(DES3Decrypt(cipher, k1, k2), 1, 9, stdout); printf("\n"); */ // messageChunk.block = inputString(stdin, 150); messageChunk.iLength = strlen(messageChunk.block) + 1; cipherChunk = DESCBCEncrypt(messageChunk, key1); printf("\n"); for (i = 0; i < cipherChunk.iLength; i++) printf("%x ", cipherChunk.block[i]); printf("\n"); decryptChunk = DESCBCDecrypt(cipherChunk, key1); (decryptChunk.iLength)++; printf("%s", decryptChunk.block); // return (0); }
void testDES() { Block message = initBlock(64); KeySchedule k1; Block key = (unsigned char*)"12345678"; Block cifer; memset(message, 0x01, 8); k1 = DESKeySchedule(key); cifer = DESEncrypt(message, k1); //17 c1 2b 68 ab 3a a1 4c }