Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Ryan Nichols
- * 2/19/17
- * CEG4750 - Undergraduate
- * Project 1 - CBC Mode DES Decoder
- */
- #include<iostream>
- #include<fstream>
- #include<sstream>
- #include<string>
- #include<cstring>
- using namespace std;
- #include"cryptopp/cryptlib.h"
- #include"cryptopp/hex.h"
- #include"cryptopp/filters.h"
- #include"cryptopp/des.h"
- #include"cryptopp/modes.h"
- using namespace CryptoPP;
- void des_decryption_8(unsigned char *input, unsigned char *key, unsigned char *output)
- {
- DESDecryption desDecryptor;
- unsigned char xorBlock[8];
- memset(xorBlock,0,8);
- desDecryptor.SetKey(key,8);
- desDecryptor.ProcessAndXorBlock(input,xorBlock,output);
- }
- int main(int argc, char * argv[])
- {
- //Assign init vector
- unsigned char iv[DES::BLOCKSIZE] = {0x4c, 0xa0, 0x0f, 0xd6, 0xdb, 0xf1, 0xfb, 0x28}; //Test case 1
- //unsigned char iv[DES::BLOCKSIZE] = {0x0f, 0xf4, 0xc8, 0xd6, 0x1e, 0x80, 0x06, 0x18}; //Test case 2
- //Files
- fstream pFile; //Output plaintext file
- fstream cFile; //Input ciphertext file
- fstream kFile; //Input key file
- //Incorrect commandline usage
- if(argc!=4)
- {
- cout<<"usage:cbc_des_encode infile outfile keyfile"<<endl;
- }
- //Open files
- cFile.open(argv[1],ios::in);
- pFile.open(argv[2],ios::out);
- kFile.open(argv[3],ios::in);
- //Read ciphertext
- stringstream buffer;
- buffer << cFile.rdbuf();
- string cString(buffer.str());
- cout<< "Ciphertext: " << cString << endl;
- //Read Key
- stringstream buffer2;
- buffer2 << kFile.rdbuf();
- string kString(buffer2.str());
- cout << "Key: " << kString;
- unsigned char key[DES::DEFAULT_KEYLENGTH];
- for (int i = 0; i < DES::DEFAULT_KEYLENGTH; i++) {
- key[i] = kString.at(i);
- }
- //Get block count
- int numblocks = (cString.size() / DES::BLOCKSIZE);
- cout << "number of blocks: " << numblocks << endl;
- unsigned char cipherblock[numblocks][DES::BLOCKSIZE];
- unsigned char plainblock [numblocks][DES::BLOCKSIZE];
- unsigned char plaintext [numblocks * DES::BLOCKSIZE];
- unsigned char thisblock[DES::BLOCKSIZE];
- for (int block = numblocks - 1; block >= 0; block--) {
- for (int i = block * 8, j = 0; j < 8; i++, j++) {
- cipherblock[block][j] = (unsigned char)cString.at(i); //Get a block
- }
- unsigned char tempblock[DES::BLOCKSIZE];
- for(int i = 0; i < 8; i++)
- {
- tempblock[i] = cipherblock[block][i]; //Fill a temporary array which is passed to the decoding function
- }
- des_decryption_8(tempblock, key, thisblock); //decrpyt block, plaintext written to this block. Still need xored
- for(int i = 0; i < 8; i++) {
- plainblock[block][i] = thisblock[i]; //Save decrypted blocks to xor them after
- }
- }
- for(int i = numblocks-1; i > 0; i--) { //For all but the first block
- for (int j = 0; j < 8; j++){
- plainblock[i][j] ^= cipherblock[i-1][j]; //xor with previous ciphertext for CBC implementation
- }
- }
- for (int j = 0; j < 8; j++){
- plainblock[0][j] ^= iv[j]; //xor first block with the IV
- }
- for (int block = 0; block < numblocks; block++) {
- for(int i = block *8, j =0; j<8; i++, j++)
- {
- plaintext[i]=plainblock[block][j]; //Recombine the plaintext
- }
- }
- //Remove padding
- int padAmount = plaintext[numblocks * DES::BLOCKSIZE -1];
- cout << "Plaintext: ";
- for (int i =0; i < (numblocks * DES::BLOCKSIZE) - padAmount; i++) {
- pFile<< plaintext [i]; //write plaintext to file
- cout << plaintext[i]; //write plaintext to console
- }
- cout <<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement