Skip to content

Commit

Permalink
Merge pull request #14 from SergeyBel/issue10
Browse files Browse the repository at this point in the history
Issue10
  • Loading branch information
SergeyBel authored Oct 3, 2019
2 parents 6706f92 + 48f4611 commit 3c750a1
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 204 deletions.
329 changes: 161 additions & 168 deletions AES.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "AES.h"
#include "AES.h"

AES::AES(int keyLen)
{
Expand All @@ -19,144 +19,136 @@ AES::AES(int keyLen)
break;
default:
throw "Incorrect key length";
}

}

blockBytesLen = 4 * this->Nb * sizeof(unsigned char);
}

unsigned char * AES::EncryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
unsigned char *out = new unsigned char[outLen];
for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
{
EncryptBlock(alignIn + i, out + i, key);
}

delete[] alignIn;

return out;
}

unsigned char * AES::DecryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
unsigned char *out = new unsigned char[outLen];
for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
{
DecryptBlock(alignIn + i, out + i, key);
}

delete[] alignIn;

return out;
}


unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
unsigned char *out = new unsigned char[outLen];
unsigned char *block = new unsigned char[blockBytesLen];
memcpy(block, iv, blockBytesLen);
for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
{
XorBlocks(block, alignIn + i, block, blockBytesLen);
EncryptBlock(block, out + i, key);
}

delete[] block;
delete[] alignIn;

return out;
}

unsigned char *AES::DecryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
unsigned char *out = new unsigned char[outLen];
unsigned char *block = new unsigned char[blockBytesLen];
memcpy(block, iv, blockBytesLen);
for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
{
DecryptBlock(alignIn + i, out + i, key);
XorBlocks(block, out + i, out + i, blockBytesLen);
}

delete[] block;
delete[] alignIn;

return out;
}

unsigned char *AES::EncryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
unsigned char *out = new unsigned char[outLen];
unsigned char *block = new unsigned char[blockBytesLen];
unsigned char *encryptedBlock = new unsigned char[blockBytesLen];
memcpy(block, iv, blockBytesLen);
for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
{
EncryptBlock(block, encryptedBlock, key);
XorBlocks(alignIn + i, encryptedBlock, out + i, blockBytesLen);
memcpy(block, out + i, blockBytesLen);
}

delete[] block;
delete[] encryptedBlock;
delete[] alignIn;

return out;
}

unsigned char *AES::DecryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
unsigned char *out = new unsigned char[outLen];
unsigned char *block = new unsigned char[blockBytesLen];
unsigned char *encryptedBlock = new unsigned char[blockBytesLen];
memcpy(block, iv, blockBytesLen);
for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
{
EncryptBlock(block, encryptedBlock, key);
XorBlocks(alignIn + i, encryptedBlock, out + i, blockBytesLen);
memcpy(block, alignIn + i, blockBytesLen);
}

delete[] block;
delete[] encryptedBlock;
delete[] alignIn;

return out;
}

unsigned char * AES::PaddingNulls(unsigned char in[], unsigned int inLen, unsigned int alignLen)
{
unsigned char *alignIn = new unsigned char[alignLen];
memcpy(alignIn, in, inLen);
memset(alignIn + inLen, 0x00, alignLen - inLen);
return alignIn;
}

unsigned int AES::GetPaddingLength(unsigned int len)
{
unsigned int lengthWithPadding = (len / blockBytesLen);
if (len % blockBytesLen) {
lengthWithPadding++;
}

lengthWithPadding *= blockBytesLen;

return lengthWithPadding;
}

unsigned char * AES::EncryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
unsigned char *out = new unsigned char[outLen];
for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
{
EncryptBlock(alignIn + i, out + i, key);
}

delete[] alignIn;

return out;
}

unsigned char * AES::DecryptECB(unsigned char in[], unsigned int inLen, unsigned char key[])
{
unsigned char *out = new unsigned char[inLen];
for (unsigned int i = 0; i < inLen; i+= blockBytesLen)
{
DecryptBlock(in + i, out + i, key);
}

return out;
}


unsigned char *AES::EncryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
unsigned char *out = new unsigned char[outLen];
unsigned char *block = new unsigned char[blockBytesLen];
memcpy(block, iv, blockBytesLen);
for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
{
XorBlocks(block, alignIn + i, block, blockBytesLen);
EncryptBlock(block, out + i, key);
memcpy(block, out + i, blockBytesLen);
}

delete[] block;
delete[] alignIn;

return out;
}

unsigned char *AES::DecryptCBC(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv)
{
unsigned char *out = new unsigned char[inLen];
unsigned char *block = new unsigned char[blockBytesLen];
memcpy(block, iv, blockBytesLen);
for (unsigned int i = 0; i < inLen; i+= blockBytesLen)
{
DecryptBlock(in + i, out + i, key);
XorBlocks(block, out + i, out + i, blockBytesLen);
memcpy(block, in + i, blockBytesLen);
}

delete[] block;

return out;
}

unsigned char *AES::EncryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv, unsigned int &outLen)
{
outLen = GetPaddingLength(inLen);
unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
unsigned char *out = new unsigned char[outLen];
unsigned char *block = new unsigned char[blockBytesLen];
unsigned char *encryptedBlock = new unsigned char[blockBytesLen];
memcpy(block, iv, blockBytesLen);
for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
{
EncryptBlock(block, encryptedBlock, key);
XorBlocks(alignIn + i, encryptedBlock, out + i, blockBytesLen);
memcpy(block, out + i, blockBytesLen);
}

delete[] block;
delete[] encryptedBlock;
delete[] alignIn;

return out;
}

unsigned char *AES::DecryptCFB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned char * iv)
{
unsigned char *out = new unsigned char[inLen];
unsigned char *block = new unsigned char[blockBytesLen];
unsigned char *encryptedBlock = new unsigned char[blockBytesLen];
memcpy(block, iv, blockBytesLen);
for (unsigned int i = 0; i < inLen; i+= blockBytesLen)
{
EncryptBlock(block, encryptedBlock, key);
XorBlocks(in + i, encryptedBlock, out + i, blockBytesLen);
memcpy(block, in + i, blockBytesLen);
}

delete[] block;
delete[] encryptedBlock;

return out;
}

unsigned char * AES::PaddingNulls(unsigned char in[], unsigned int inLen, unsigned int alignLen)
{
unsigned char *alignIn = new unsigned char[alignLen];
memcpy(alignIn, in, inLen);
memset(alignIn + inLen, 0x00, alignLen - inLen);
return alignIn;
}

unsigned int AES::GetPaddingLength(unsigned int len)
{
unsigned int lengthWithPadding = (len / blockBytesLen);
if (len % blockBytesLen) {
lengthWithPadding++;
}

lengthWithPadding *= blockBytesLen;

return lengthWithPadding;
}

void AES::EncryptBlock(unsigned char in[], unsigned char out[], unsigned char key[])
{
unsigned char *w = new unsigned char[4 * Nb * (Nr + 1)];
Expand Down Expand Up @@ -203,8 +195,8 @@ void AES::EncryptBlock(unsigned char in[], unsigned char out[], unsigned char k
delete[] state[0];
delete[] state;
delete[] w;
}

}

void AES::DecryptBlock(unsigned char in[], unsigned char out[], unsigned char key[])
{
unsigned char *w = new unsigned char[4 * Nb * (Nr + 1)];
Expand Down Expand Up @@ -249,8 +241,8 @@ void AES::DecryptBlock(unsigned char in[], unsigned char out[], unsigned char k
delete[] state[0];
delete[] state;
delete[] w;
}

}


void AES::SubBytes(unsigned char **state)
{
Expand Down Expand Up @@ -400,7 +392,7 @@ void AES::Rcon(unsigned char * a, int n)

void AES::KeyExpansion(unsigned char key[], unsigned char w[])
{
unsigned char *temp = new unsigned char[4];
unsigned char *temp = new unsigned char[4];
unsigned char *rcon = new unsigned char[4];

int i = 0;
Expand All @@ -419,9 +411,9 @@ void AES::KeyExpansion(unsigned char key[], unsigned char w[])
temp[3] = w[i - 4 + 3];

if (i / 4 % Nk == 0)
{
RotWord(temp);
SubWord(temp);
{
RotWord(temp);
SubWord(temp);
Rcon(rcon, i / (Nk * 4));
XorWords(temp, rcon, temp);
}
Expand All @@ -435,9 +427,9 @@ void AES::KeyExpansion(unsigned char key[], unsigned char w[])
w[i + 2] = w[i + 2 - 4 * Nk] ^ temp[2];
w[i + 3] = w[i + 3 - 4 * Nk] ^ temp[3];
i += 4;
}

delete []rcon;
}

delete []rcon;
delete []temp;

}
Expand Down Expand Up @@ -485,25 +477,26 @@ void AES::InvShiftRows(unsigned char **state)
ShiftRow(state, 1, Nb - 1);
ShiftRow(state, 2, Nb - 2);
ShiftRow(state, 3, Nb - 3);
}

void AES::XorBlocks(unsigned char *a, unsigned char * b, unsigned char *c, unsigned int len)
{
for (unsigned int i = 0; i < len; i++)
{
c[i] = a[i] ^ b[i];
}
}

void AES::printHexArray (unsigned char a[], unsigned int n)
{
for (int i = 0; i < n; i++) {
printf("%02x ", a[i]);
}

}




}

void AES::XorBlocks(unsigned char *a, unsigned char * b, unsigned char *c, unsigned int len)
{
for (unsigned int i = 0; i < len; i++)
{
c[i] = a[i] ^ b[i];
}
}

void AES::printHexArray (unsigned char a[], unsigned int n)
{
for (int i = 0; i < n; i++) {
printf("%02x ", a[i]);
}
}







Loading

0 comments on commit 3c750a1

Please sign in to comment.