Skip to content

Instantly share code, notes, and snippets.

@adi928
Created September 20, 2018 14:42
Show Gist options
  • Save adi928/3ec669821f7c3cd7481b8e2768303f4e to your computer and use it in GitHub Desktop.
Save adi928/3ec669821f7c3cd7481b8e2768303f4e to your computer and use it in GitHub Desktop.
Key Expansion included AES encrypt
#include <iostream>
#include "AESConstants.h"
#include <fstream>
#include <string>
using namespace std;
// Input the plaintext and the key
void inputData(char* plainText, char* key)
{
// Variable to contain user's file name
string fileName, keyFileName;
// Variable to contain file length
int fileLength;
// Variable to access file content
ifstream plainTextFile;
ifstream keyFile;
// User is prompted to input file name
cout << "Enter the plaintext file: ";
getline(cin, fileName);
cout << "Enter the key file: ";
getline(cin, keyFileName);
// File given by user is open
plainTextFile.open(fileName);
keyFile.open(keyFileName);
// Checks if failure occurred during opening of file
if (plainTextFile.fail() || keyFile.fail())
{
cout << "Failed to open file." << endl;
return ;
}
// Length of file is obtained
plainTextFile.seekg(0, plainTextFile.end);
fileLength = plainTextFile.tellg();
plainTextFile.seekg(0, plainTextFile.beg);
keyFile.seekg(0, keyFile.end);
fileLength = keyFile.tellg();
keyFile.seekg(0, keyFile.beg);
// Content of file is being stored in a character
// array
int counter = 0;
while (!plainTextFile.eof())
{
plainTextFile.get(plainText[counter]);
counter++;
}
counter = 0;
while (!keyFile.eof())
{
keyFile.get(key[counter]);
counter++;
}
// Closes file being read
plainTextFile.close();
keyFile.close();
}
// Implementing the key expansion algorithm
void keyExpansionCore(char * input, int rConIteration)
{
//Rotate
char t = input[0];
input[0] = input[1];
input[1] = input[2];
input[2] = input[3];
input[3] = t;
//SBox replace
input[0] = SBOX[input[0]];
input[1] = SBOX[input[1]];
input[2] = SBOX[input[2]];
input[3] = SBOX[input[3]];
//XOR first byte with RCON
input[0] = input[0]^RCON[rConIteration];
}
void keyExpansion(char* key, char* expandedKey)
{
for (int i=0;i<16;i++)
expandedKey[i] = key[i];
int bytesGenerated = 16;
char tmp[4];
int rconIteration = 1;
while(bytesGenerated < 176)
{
for (int i=0;i<4;i++)
tmp[i] = expandedKey[i+bytesGenerated-4];
if (bytesGenerated%16 == 0)
keyExpansionCore(tmp, rconIteration++);
for (int j=0;j<4;j++)
{
expandedKey[bytesGenerated] = expandedKey[bytesGenerated-16] ^ tmp[j];
bytesGenerated++;
}
}
}
// TODO: XOR the given state with a round key
// from the key expansion algorithm.
char* addRoundKey() { return NULL; }
// TODO: Substitute bytes of the given state using S-Box
void substituteBytes(char* state)
{
for (int i=0; i<16;i++)
{
state[i] = SBOX[state[i]];
}
}
// TODO: Implementing shift left on the given state
void shiftRows() {}
// TODO: Matrix multiplication with the provided matrix.
void multiplyColumns() {}
// Encrypts 16 bytes of data at a time
char* encryptData(char* Plaintext, char* expandedKey)
{
char state[16];
// Making a copy of the message as the initial state.
for(int j=0;j<16;j++)
{
state[j] = Plaintext[j];
}
char* ciphertext;
addRoundKey();
int nr_Rounds = 10; // for 128bit encryption
for(int i=0;i<nr_Rounds;i++)
{
substituteBytes(state);
shiftRows();
multiplyColumns();
addRoundKey();
}
//Final Round
substituteBytes(state);
shiftRows();
ciphertext = addRoundKey();
return ciphertext;
}
int main()
{
char key[16];
char plainText[100];
inputData(plainText, key);
char expandedKey[176];
keyExpansion(key, expandedKey);
// In the last phase of the project, we need to loop for
// every 16 bytes of the message since the message can
// be more than 16 bytes long.
// char* cipherText = encryptData(plainText, key);
//cout << &cipherText;
// I include this since I'm running it on Microsoft Visual Studios
// to be able to see the output in the console
// feel free to remove it when you test it on your computer
system("pause");
cout << plainText <<endl;
cout<< key <<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment