#include \"des56.h\" #include <stdlib.h> #include <stdio.h> #include <string.h>
ID: 3599705 • Letter: #
Question
#include "des56.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//Use these constants to specify ENCRYPT or DECRYPT in the fencrypt function call. (The middle parameter)
#define ENCRYPT 0
#define DECRYPT 1
/*This function takes the XOR of block A and block B and stores the result in block "result"*/
/*Note that it is possible that "result" is the same as "A" (or "B").*/
/*For example:
XORblock(block1,IV,block1);
Would XOR the first block with the IV and put the result in block1*/
void XORblock(char A[8],char B[8],char result[8])
{
int i = 0;
for(;i<8;i++)
result[i] = A[i]^B[i];
}
/*This function takes a block as input and prints the corresponding hex to the terminal*/
void printhex(unsigned char block[8])
{
int i=0;
for(;i<8;i++)
printf(" 0x%x ",block[i]);
puts("");
}
void cbc_enc_three_blocks(char block1[8],char block2[8], char block3[8], char IV[8],keysched *ks)
{
// You write this code
}
void cbc_dec_three_blocks(char block1[8],char block2[8], char block3[8], char IV[8],keysched *ks)
{
// You write this code
}
int main()
{
unsigned char key[8] = {1,2,3,4,5,6,7,8}; //This will be the key
keysched *ks = malloc(sizeof(keysched)); //Initialize the key schedule data structure
fsetkey(key,ks); //Build the key schedule
unsigned char* all_blocks = malloc(3*8*sizeof(char)); //24 bytes of memory, representing 3 DES blocks
bzero(all_blocks,8*3); //Initialize the blocks to all 0s
all_blocks[0] = 'A'; //Put 0x41=='A' into the first byte of the first block
/*The file "plaintext.txt", created below, will be useful for testing.*/
FILE* pt_f = fopen("plaintext.txt","wb");
fwrite(all_blocks,8,3,pt_f); //write 3 blocks, each of length 8 bytes
fclose(pt_f);
unsigned char IV[8] = {0}; //The IV will be all zeros
cbc_enc_three_blocks(all_blocks,all_blocks+8,all_blocks+16,IV,ks); //Do the encryption
FILE* fp = fopen("encryption.des","wb"); //Save the result
fwrite(all_blocks,8,3,fp); //write 3 blocks, each of length 8 bytes
fclose(fp);
cbc_dec_three_blocks(all_blocks,all_blocks+8,all_blocks+16,IV,ks); //Do the decryption
fp = fopen("decryption.txt","wb"); //Save the result
fwrite(all_blocks,8,3,fp); //write 3 blocks, each of length 8 bytes
fclose(fp);
/*Now, from the command line, do
$ openssl enc -des -nopad -in plaintext.txt -out verify.des -K 0102030405060708 -iv 0000000000000000
and use xxd (or diff) to confirm that verify.des = encryption.des byte for byte.
~/Crypto_Fall_17/project_3/DES code/optimized$ xxd encryption.des
00000000: bfac 1238 7123 3ff1 608b 10d9 6406 f238 ...8q#?.`...d..8
00000010: a802 9a03 e8cd 9454 .......T
~/Crypto_Fall_17/project_3/DES code/optimized$ openssl enc -des -nopad -in plaintext.txt -out verify.des -K 0102030405060708 -iv 0000000000000000
~/Crypto_Fall_17/project_3/DES code/optimized$ xxd verify.des
00000000: bfac 1238 7123 3ff1 608b 10d9 6406 f238 ...8q#?.`...d..8
00000010: a802 9a03 e8cd 9454 .......T
Also check that decryption.txt = plaintext.txt by using diff or xxd.
~/Crypto_Fall_17/project_3/DES code/optimized$ xxd decryption.txt
00000000: 4100 0000 0000 0000 0000 0000 0000 0000 A...............
00000010: 0000 0000 0000 0000 ........
~/Crypto_Fall_17/project_3/DES code/optimized$ xxd plaintext.txt
00000000: 4100 0000 0000 0000 0000 0000 0000 0000 A...............
00000010: 0000 0000 0000 0000 ........
*/
}
Explanation / Answer
#include "des56.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//Use these constants to specify ENCRYPT or DECRYPT in the fencrypt function call. (The middle parameter)
#define ENCRYPT 0
#define DECRYPT 1
/*This function takes the XOR of block A and block B and stores the result in block "result"*/
/*Note that it is possible that "result" is the same as "A" (or "B").*/
/*For example:
XORblock(block1,IV,block1);
Would XOR the first block with the IV and put the result in block1*/
void XORblock(char A[8],char B[8],char result[8])
{
int i = 0;
for(;i<8;i++)
result[i] = A[i]^B[i];
}
/*This function takes a block as input and prints the corresponding hex to the terminal*/
void printhex(unsigned char block[8])
{
int i=0;
for(;i<8;i++)
printf(" 0x%x ",block[i]);
puts("");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.