Question

I've completed my AES Encryption program up to the point where I get the correct answer on the command prompt with this code:

void main()
{
int a;

unsigned char CipherKey[16] = {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
unsigned char PlainText[16] = {0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D, 0x31, 0x31, 0x98, 0xA2, 0xE0, 0x37, 0x07, 0x34};


for(a=0;a<16;a++)
{
    Key[a] = CipherKey[a];
    input[a] = PlainText[a];
}

KeyExpansion();
Cipher();

cout << endl;
cout << "Encryted output text:" << endl;

for(a=0;a<16;a++) // For loop is used to print every character of the output array onto the console
{
    printf("%02x ",output[a]); // printf is used in order to print the encrypted text in hexidecimal
}

cout << endl;

}

However, I now need to get the blocks of data for the CipherKey and PlainText from a data file instead of in the program itself. My data file Input.dat looks like this:

0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C

0x32, 0x43, 0xF6, 0xA8, 0x88, 0x5A, 0x30, 0x8D, 0x31, 0x31, 0x98, 0xA2, 0xE0, 0x37, 0x07, 0x34

Therefore I need to take this information from a file and place it into these arrays:

unsigned char CipherKey[16];
unsigned char PlainText[16];

The program will then use this data as normal to encrypt the PlainText and then create a new output file with the encrypted text stored in it (not worried about this part yet)

Here's my attempt:

void main()
{
int a;

unsigned char CipherKey[16]; 
unsigned char PlainText[16];

ifstream inFile;
ofstream outFile;

inFile.open("Input.dat"); // Opens input file with required info to place into arrays initialised above

for(a=0;a<16;a++)
{
    inFile >>  CipherKey[a]; // The first 16 unsigned chars in the file are placed in the array CipherKey[16]
    // i.e. 0x2B is placed in CipherKey[0], 0xAE is placed in CipherKey[5] etc
}

for(a=0;a<16;a++)
{
    inFile >>  PlainText[a]; // The last 16 unsigned chars in the file are placed in the array PlainText[16]
}

inFile.close();
    ...// Rest of code

The answer should be : 39 25 84 1d 02 dc 09 fb dc 11 85 97 19 6a 0b 32 whereas I am getting : 60 a6 f1 d6 7a 14 32 1a 40 55 50 e8 0c fc 5f cc

I made a small program that uses this method to get the sum integer values in a file to see if this way could work with the AES program and it seems like it can.

Does this look like im along the right lines or way off the mark?

Was it helpful?

Solution

When using an input stream to read a char, it is not read as an integer (hex or otherwise), but as a text character.

Thus, by doing inFile >> Cipherkey[a], you will fill CipherKey with the ascii (well, technically it depends on the encoding of your input file and your c++ compiler) codes of the first 16 bytes of your file:

For example, if it starts with "0x2B", the first four chars of CipherKey would be 0x30 0x78 0x32 0x42.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top