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
|
int main(int argc,char *argv[]){
if(argc != 5) {
cout << "Usage: Encoding: " << argv[0] << " -e [plaintext-file] [key-file] [cipher-file]" << endl;
cout << "Usage: Decoding: " << argv[0] << " -d [cipher-file] [key-file] [plaintext-file]" << endl;
return 0;
}
const char *modes[2] = { "de", "en" };
uint16 i, j;
char *Mode = argv[1];
char *InFile = argv[2];
char *KeyFile = argv[3];
char *OutFile = argv[4];
// If mode is Encryption or Decryption
if ((Mode[0] != '-') || ((Mode[1] != 'e' ) && (Mode[1] != 'd' ))) {
cerr << "Invalid mode: `" << Mode << "'. Please specify `-e' or `-d'." << endl;
exit(1);
}
bool isEncrypt = (Mode[1] == 'e') ? true : false;
cout << "IDEA:: " << modes[isEncrypt] << "crypt" << endl;
// read keyfile
FILE* key_file = fopen(KeyFile,"rb");
if(!key_file){
cout << "Error opening key file: `" << KeyFile << "'" << endl;
return 0;
}
fseek(key_file,0,SEEK_END);
unsigned int key_size = ftell(key_file);
D(cout << "Key File Size:" << key_size << endl;);
unsigned char char_key[16] = { 0 };
if (key_size >= 16) {
rewind(key_file);
fread(char_key,1,16,key_file);
char_key[16]='\0';
fclose(key_file);
}
D(cout << "Key:" << char_key << endl;);
// 128 bit-key (16 characters read)
// converting into 8-length array of 16-bit integers (short)
uint16 *key;
key = reinterpret_cast<uint16 *>(char_key);
D(cout << "Key looks all shiny..Size: " << sizeof(key) << ". (8 means uint16 cast right)" << endl);
int **Encryption_Key = reinterpret_cast<int **>(calloc(9, sizeof(int *)));
int **Decryption_Key = reinterpret_cast<int **>(calloc(9, sizeof(int *)));
for (i = 0; i < 9; i++) {
Encryption_Key[i] = reinterpret_cast<int *>(calloc(6, sizeof(int)));
Decryption_Key[i] = reinterpret_cast<int *>(calloc(6, sizeof(int)));
for (j = 0; j < 6; j++) Encryption_Key[i][j] = Decryption_Key[i][j] = 0;
}
D(cout << "Initialized Encryption and Decryption Key arrays." << endl);
/**** Key Expansion ****/
// Required to expand the key irrespective of encryption or decryption
Build_Encryption_Key(key,Encryption_Key);
D(cout << "Just expanded that key.." << endl);
int char_count;
// Encrypt or Decrypt according to mode
if (isEncrypt) {
D(cout << "Okay, now going to encrypt.." << endl);
char_count = Encrypt(InFile,OutFile,Encryption_Key);
D(cout << "It seems like that encryption worked.." << endl);
} else {
Build_Decryption_Key(Encryption_Key, Decryption_Key);
char_count = Decrypt(InFile,OutFile,Decryption_Key);
}
cout << "IDEA:: Total " << char_count << " characters " << modes[isEncrypt] << "crypted." << endl;
}
|