📄 aes.cpp
字号:
// AES.cpp: implementation of the AES class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "AES.h"
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// AES.cpp : Defines the entry point for the console application.
//
//-----------------------------------------------------------------------------
//功能:8位有限域乘法
// g(x)=x**8+x**4+x**3+x**1+1
//
//输入:
// Mtpcand1 被乘数
//输出:
// Mtper1 乘数
//返回:
// 乘法结果
unsigned char FieldMultiply_08( unsigned char Mtpcand,
unsigned char Mtper)
{
//1.Declaration of local variables
unsigned char Result = 0; //The returned value
unsigned char i;
//2.multiplex
//Mtper在本算法中最大只有4bit,所以可以只用循环4次
for(i=0; i<4; i++)
{
if(Mtper & 0x1)
{
Result ^= Mtpcand;
}
if(Mtpcand & 0x80)
{
Mtpcand <<= 1;
Mtpcand ^= 0x1B;
}
else
{
Mtpcand <<= 1;
}
Mtper >>= 1;
}
return Result;
}
//-----------------------------------------------------------------------------
//功能:循环左移1字节
//
//输入:
// pOperand 操作数
//输出:
// pOperand 操作数变换结果
//返回:
// 正常 0
// 不正常 非0
void RotBytes(unsigned long *pOperand)
{
unsigned char cTemp;
cTemp = ((unsigned char*)pOperand)[0];
((unsigned char*)pOperand)[0] = ((unsigned char*)pOperand)[1];
((unsigned char*)pOperand)[1] = ((unsigned char*)pOperand)[2];
((unsigned char*)pOperand)[2] = ((unsigned char*)pOperand)[3];
((unsigned char*)pOperand)[3] = cTemp;
}
//-----------------------------------------------------------------------------
//功能:加密S盒代换
//
//输入:
// pSBox 加密S盒数据
// pState 当前的明文变换状态
// cNum 进行代换的字节个数
//输出:
// pState 当前的明文变换状态
//返回:
// 正常 0
// 不正常 非0
void SubBytes( unsigned char *pSBox,
unsigned char *pState,
unsigned char cNum)
{
unsigned char i;
for(i=0; i<cNum; i++)
{
pState[i] = pSBox[pState[i]];
}
}
//-----------------------------------------------------------------------------
//功能:解密S盒代换
//
//输入:
// pSBox 加密S盒数据
// pState 当前的明文变换状态
// cNum 进行代换的字节个数
//输出:
// pState 当前的明文变换状态
//返回:
// 正常 0
// 不正常 非0
void InvSubBytes( unsigned char *pSBox,
unsigned char *pState,
unsigned char cNum)
{
unsigned char i;
for(i=0; i<cNum; i++)
{
pState[i] = pSBox[pState[i]];
}
}
//-----------------------------------------------------------------------------
//功能:加密ShiftRows变换
//
//输入:
// pState 当前的明文变换状态
//输出:
// pState 当前的明文变换状态
//返回:
// 正常 0
// 不正常 非0
void ShiftRows(unsigned char *pState)
{
unsigned char cTemp;
cTemp = pState[1];
pState[1] = pState[5];
pState[5] = pState[9];
pState[9] = pState[13];
pState[13] = cTemp;
cTemp = pState[2];
pState[2] = pState[10];
pState[10] = cTemp;
cTemp = pState[6];
pState[6] = pState[14];
pState[14] = cTemp;
cTemp = pState[15];
pState[15] = pState[11];
pState[11] = pState[7];
pState[7] = pState[3];
pState[3] = cTemp;
}
//-----------------------------------------------------------------------------
//功能:解密ShiftRows变换
//
//输入:
// pState 当前的明文变换状态
//输出:
// pState 当前的明文变换状态
//返回:
// 正常 0
// 不正常 非0
void InvShiftRows(unsigned char *pState)
{
unsigned char cTemp;
cTemp = pState[13];
pState[13] = pState[9];
pState[9] = pState[5];
pState[5] = pState[1];
pState[1] = cTemp;
cTemp = pState[2];
pState[2] = pState[10];
pState[10] = cTemp;
cTemp = pState[6];
pState[6] = pState[14];
pState[14] = cTemp;
cTemp = pState[3];
pState[3] = pState[7];
pState[7] = pState[11];
pState[11] = pState[15];
pState[15] = cTemp;
}
//-----------------------------------------------------------------------------
//功能:加密MixColumn变换
//
//输入:
// pState 当前的明文变换状态
//输出:
// pState 当前的明文变换状态
//返回:
// 正常 0
// 不正常 非0
void MixColumn(unsigned char *pState)
{
unsigned char cPos;
unsigned char cData0, cData1, cData2, cData3;
unsigned char i;
for(i=0; i<4; i++)
{
cPos = 4*i;
cData0 = pState[cPos];
cData1 = pState[cPos+1];
cData2 = pState[cPos+2];
cData3 = pState[cPos+3];
pState[cPos] = FieldMultiply_08(cData0, 2) ^ FieldMultiply_08(cData1, 3)
^ cData2 ^ cData3;
pState[cPos+1] = FieldMultiply_08(cData1, 2) ^ FieldMultiply_08(cData2, 3)
^ cData3 ^ cData0;
pState[cPos+2] = FieldMultiply_08(cData2, 2) ^ FieldMultiply_08(cData3, 3)
^ cData0 ^ cData1;
pState[cPos+3] = FieldMultiply_08(cData3, 2) ^ FieldMultiply_08(cData0, 3)
^ cData1 ^ cData2;
}
}
//-----------------------------------------------------------------------------
//功能:解密MixColumn变换
//
//输入:
// pState 当前的明文变换状态
//输出:
// pState 当前的明文变换状态
//返回:
// 正常 0
// 不正常 非0
void InvMixColumn(unsigned char *pState)
{
unsigned char cPos;
unsigned char cData0, cData1, cData2, cData3;
unsigned char i;
for(i=0; i<4; i++)
{
cPos = 4*i;
cData0 = pState[cPos];
cData1 = pState[cPos+1];
cData2 = pState[cPos+2];
cData3 = pState[cPos+3];
pState[cPos] = FieldMultiply_08(cData0, 0xe) ^ FieldMultiply_08(cData1, 0xb)
^ FieldMultiply_08(cData2, 0xd) ^ FieldMultiply_08(cData3, 0x9);
pState[cPos+1] = FieldMultiply_08(cData0, 0x9) ^ FieldMultiply_08(cData1, 0xe)
^ FieldMultiply_08(cData2, 0xb) ^ FieldMultiply_08(cData3, 0xd);
pState[cPos+2] = FieldMultiply_08(cData0, 0xd) ^ FieldMultiply_08(cData1, 0x9)
^ FieldMultiply_08(cData2, 0xe) ^ FieldMultiply_08(cData3, 0xb);
pState[cPos+3] = FieldMultiply_08(cData0, 0xb) ^ FieldMultiply_08(cData1, 0xd)
^ FieldMultiply_08(cData2, 0x9) ^ FieldMultiply_08(cData3, 0xe);
}
}
//-----------------------------------------------------------------------------
//功能:一个轮密钥的加法
//
//输入:
// pState 当前的明文变换状态
// pRoundKey 本轮用到的轮密钥
//输出:
// pState 当前的明文变换状态
//返回:
// 正常 0
// 不正常 非0
void AddRoundKey( unsigned char *pState,
unsigned char *pRoundKey)
{
unsigned char i;
for(i=0; i<16; i++)
{
pState[i] = pState[i] ^ pRoundKey[i];
}
}
//-----------------------------------------------------------------------------
//功能:产生加密轮密钥
//
//输入:
// pSBox 加密S盒数据
// pCipherKey 用户的加密密钥
// keySize 密钥字节长度
//输出:
// pExpandedKey 扩展出的轮密钥
//返回:
// 正常 0
// 不正常 非0
char KeyExpansion( unsigned char *pExpandedKey,
unsigned char *pSBox,
unsigned char *pCipherKey,
unsigned char keySize)
{
char cErrCode=0;
unsigned char Rcon[31] = {0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36,
0x6c,0xd8,0xab,0x4d,0x9a,0x2f,0x5e,0xbc,0x63,0xc6,
0x97,0x35,0x6a,0xd4,0xb3,0x7d,0xfa,0xef,0xc5,0x91};
unsigned long lTemp=0;
unsigned char cNk=0, cNr=0, cTemp;
unsigned char cPos, cPos1;
unsigned char i;
switch (keySize)
{
case 16:
cNk = 4;
cNr = 10;
break;
case 24:
cNk = 6;
cNr = 12;
break;
case 32:
cNk = 8;
cNr = 14;
break;
default:
break;
}
cTemp = 4*cNk;
for(i=0; i<cTemp; i++)
{
pExpandedKey[i] = pCipherKey[i];
}
cTemp = 4 * (cNr+1);
for(i=cNk; i<cTemp; i++)
{
cPos = 4*i;
((unsigned char*)&lTemp)[0] = pExpandedKey[cPos-4];
((unsigned char*)&lTemp)[1] = pExpandedKey[cPos-3];
((unsigned char*)&lTemp)[2] = pExpandedKey[cPos-2];
((unsigned char*)&lTemp)[3] = pExpandedKey[cPos-1];
if(i%cNk == 0)
{
RotBytes(&lTemp);
SubBytes(pSBox, (unsigned char*)&lTemp, 4);
((unsigned char*)&lTemp)[0] ^= Rcon[i/cNk];
}
else if(cNk>6 && (i%cNk == 4))
{
SubBytes(pSBox, (unsigned char*)&lTemp, 4);
}
cPos1 = cPos - 4*cNk; //4*(i-cNk)
pExpandedKey[cPos+0] = pExpandedKey[cPos1] ^ ((unsigned char*)&lTemp)[0];
pExpandedKey[cPos+1] = pExpandedKey[cPos1+1] ^ ((unsigned char*)&lTemp)[1];
pExpandedKey[cPos+2] = pExpandedKey[cPos1+2] ^ ((unsigned char*)&lTemp)[2];
pExpandedKey[cPos+3] = pExpandedKey[cPos1+3] ^ ((unsigned char*)&lTemp)[3];
}
return cErrCode;
}
//-----------------------------------------------------------------------------
//功能:产生解密轮密钥
//
//输入:
// pCipherKey 用户的解密密钥
// keySize 密钥字节长度
//输出:
// pExpandedKey 扩展出的轮密钥
//返回:
// 正常 0
// 不正常 非0
//char InvKeyExpansion( unsigned char *pExpandedKey,
// unsigned char *pCipherKey,
// unsigned char keySize)
//{
// char cErrCode=0;
// int i;
//
// KeyExpansion(pExpandedKey, pCipherKey, cKeyType);
// for(i=1; i<10; i++)
// {
// InvMixColumn(pExpandedKey + 16*i);
// }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -