📄 aes.cpp
字号:
/***********************************************************
* FILE NAME: aes.cpp
*
* DESCRIPTION: this file define various functions that are used to
* encrypt or decrypt a message using Advance Standard Encryption.
*
* By: Khoa Vu
* Date 2/22/03
************************************************************/
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <exception>
#include "aes.h"
#include "aesTable.h"
//#define DEBUG 1
/**************************
* FUNCTION: Constructor
*
* DESCRIPTION: initial message block and key block and cipher block
**************************/
Aes::Aes()
{
memset(&mesg, 0, sizeof(Block));
memset(&key, 0, sizeof(Block));
memset(&cipher, 0, sizeof(Block));
memset(&rndKeys, 0, sizeof(Block)*NUM_RND);
}
/**************************
* FUNCTION: Destructor
*
* DESCRIPTION: initial message block and key block and cipher block
**************************/
Aes::~Aes()
{
//delete [] key;
//delete [] mesg;
//delete [] cihper;
}
/**************************
* FUNCTION: aesEncrypt(char *pMesg, char *pKey, char *pCipher)
*
* PARAMETERS: pMesg: pointer to message
* pKey: pointer to key
* pCihper: pointer to cipher text
*
* DESCRIPTION: This function encrypt the message using AES algorithm
**************************/
void Aes::aesEncrypt(unsigned char *pMesg,
unsigned char* pKey,
unsigned char* pCipher)
{
int i,j, msgLength, cpSize;
Block iv, tmp;
memset(pCipher, 0, TXT_SIZE); // clear output entry
memcpy(&key.blkByte, pKey, BLK_SIZE); // copy key to block of 16 bytes
//pMesg += i;
makeKeys(key);
msgLength = strlen((const char *)pMesg);
// XOR the first message block with IV
memcpy(&mesg.blkByte, pMesg, BLK_SIZE); // copy messeage to block of 16 bytes
iv = aesGetIV();
for(j = 0; j < BLK_SIZE; j++)
{
mesg.blkByte[j] ^= iv.blkByte[j];
}
blockEnc(&mesg, key);
memcpy(pCipher, &mesg.blkByte, BLK_SIZE);
pCipher += BLK_SIZE;
pMesg += BLK_SIZE;
for(i = BLK_SIZE; i < msgLength; i+= BLK_SIZE)
{
if ( strlen((const char *)pMesg) > BLK_SIZE)
cpSize = BLK_SIZE;
else
cpSize = strlen((const char *)pMesg);
memset(&tmp, 0, sizeof(Block)); // clear output entry
memcpy(&tmp.blkByte, pMesg, cpSize); // copy messeage to block of 16 bytes
#if 1
for(j = 0; j < BLK_SIZE; j++)
{
//mesg.blkByte[j] ^= *pMesg;
//pMesg++;
mesg.blkByte[j] ^= tmp.blkByte[j];
}
#endif
blockEnc(&mesg, key);
memcpy(pCipher, &mesg.blkByte, BLK_SIZE);
pCipher += BLK_SIZE;
pMesg += BLK_SIZE;
}
}
/**************************
* FUNCTION: aesDecrypt(char *pMesg, char *pKey, char *pCipher)
*
* PARAMETERS: pMesg: pointer to message
* pKey: pointer to key
* pCihper: pointer to cipher text
*
* DESCRIPTION: This function encrypt the message using AES algorithm
**************************/
void Aes::aesDecrypt(unsigned char *pMesg,
unsigned char* pKey,
unsigned char* pResult)
{
int i, j, msgLength;
unsigned char *pTmp;
Block iv;
pTmp = pMesg;
// memset(pCipher, 0, TXT_SIZE); // clear output entry
memcpy(&key.blkByte, pKey, BLK_SIZE); // copy key to block of 16 bytes
makeKeys(key);
msgLength = strlen((const char *)pMesg);
// Decrypt the first message block
memcpy(&mesg.blkByte, pMesg, BLK_SIZE); // copy messeage to block of 16 bytes
blockDec(&mesg, key);
// XOR the first message block with IV
memset(&iv, 0, sizeof(Block));
iv = aesGetIV();
for(j = 0; j < BLK_SIZE; j++)
{
mesg.blkByte[j] ^= iv.blkByte[j];
}
memcpy(pResult, &mesg.blkByte, BLK_SIZE);
pResult += BLK_SIZE;
pMesg += BLK_SIZE;
for(i = BLK_SIZE; i < msgLength; i+= BLK_SIZE)
{
memcpy(&mesg.blkByte, pMesg, BLK_SIZE); // copy messeage to block of 16 bytes
blockDec(&mesg, key);
#if 1
for(j = 0; j < BLK_SIZE; j++)
{
mesg.blkByte[j] ^= *pTmp;
pTmp++;
}
#endif
//pTmp = pMesg;
//pMesg = pTmp;
memcpy(pResult, &mesg.blkByte, BLK_SIZE);
pResult += BLK_SIZE;
pMesg += BLK_SIZE;
}
}
// <<<< -----------------------------------
/**************************
* FUNCTION: aesSetIV(unsigned char *pIV)
*
* PARAMETERS:
*
*
* DESCRIPTION: set Initial vector
**************************/
void Aes::aesSetIV(unsigned char *pIV)
{
int ivSize = 0;
ivSize = strlen((const char *)pIV); //get IV size
memset(&initVec, 0x00, sizeof(Block)); // clear Init Vector
// Do not set initVec if its size is 0
if (ivSize)
memcpy(&initVec.blkByte, pIV, ivSize); // copy key to block of 16 bytes
}
/**************************
* FUNCTION: aesGetIV(unsigned char *pIV)
*
* PARAMETERS:
*
*
* DESCRIPTION: return Initial vector
**************************/
Block Aes::aesGetIV()
{
return initVec;
}
// ----------------------------------- >>>>
/**************************
* FUNCTION: blockEnc
*
* PARAMETERS:
*
* DESCRIPTION:
**************************/
void Aes::blockEnc(Block *pMesg, Block key)
{
Uint8 *pResult;
Block rndkey;
int i,j;
// add round key
for(i = 0; i < BLK_SIZE; i++)
{
pMesg->blkByte[i] ^= key.blkByte[i];
}
// next nine rounds
for(i = 1; i < 10; i++)
{
getRndKeys(i,&rndkey);
byteSub(pMesg);
shiftRow(pMesg);
mixColumn(pMesg);
// add round key
for(j = 0; j < BLK_SIZE; j++)
{
pMesg->blkByte[j] ^= rndkey.blkByte[j];
}
}
// final round
getRndKeys(10,&rndkey);
byteSub(pMesg);
shiftRow(pMesg);
// add round key
for(i = 0; i < BLK_SIZE; i++)
{
pMesg->blkByte[i] ^= rndkey.blkByte[i];
}
//memcpy(pCipher, pResult, BLK_SIZE);
}
/**************************
* FUNCTION: blockDec
*
* PARAMETERS:
*
* DESCRIPTION: Decrypt block of 128 bits
**************************/
void Aes::blockDec(Block *pMesg, Block key)
{
Uint8 *pResult;
Block rndkey;
int i,j;
// check if we have a new key. If so then
// make new key set else use old key
// ------
// ------
// ------
getRndKeys(10, &rndkey);
// add round key
for(i = 0; i < BLK_SIZE; i++)
{
pMesg->blkByte[i] ^= rndkey.blkByte[i];
}
invShiftRow(pMesg);
invByteSub(pMesg);
// next nine rounds
for(i = 9; i > 0; i--)
{
getRndKeys(i,&rndkey);
// add round key
for(j = 0; j < BLK_SIZE; j++)
{
pMesg->blkByte[j] ^= rndkey.blkByte[j];
}
invMixColumn(pMesg);
invShiftRow(pMesg);
invByteSub(pMesg);
}
// final round
getRndKeys(0,&rndkey);
// add round key
for(i = 0; i < BLK_SIZE; i++)
{
pMesg->blkByte[i] ^= rndkey.blkByte[i];
}
//memcpy(pCipher, pResult, BLK_SIZE);
}
/**************************
* FUNCTION: byteSub
*
* PARAMETERS:
*
* DESCRIPTION:
**************************/
void Aes::byteSub(Block *pBlk)
{
int i;
for(i = 0; i < BLK_SIZE; i++)
{
pBlk->blkByte[i] = SBox[pBlk->blkByte[i]];
}
}
/**************************
* FUNCTION: invByteSub
*
* PARAMETERS:
*
* DESCRIPTION:
**************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -