📄 aesencryption.cpp
字号:
// AesEncryption.cpp : implementation file
//
#include "stdafx.h"
#include "EncryptionSystem.h"
#include "AesEncryption.h"
#include "aes.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAesEncryption dialog
AES_KEY *keyPara;
CAesEncryption::CAesEncryption(CWnd* pParent /*=NULL*/)
: CDialog(CAesEncryption::IDD, pParent)
{
//{{AFX_DATA_INIT(CAesEncryption)
m_Aes_Key = _T("");
m_Aes_IV = _T("");
m_Aes_NewPlain = _T("");
m_Aes_Plain = _T("");
m_Aes_Cipher = _T("");
m_Aes_KeyLength = _T("");
//}}AFX_DATA_INIT
}
CAesEncryption::~CAesEncryption()
{
// delete keyPara;
// delete []aesplain11;
// delete []aescipher11;
}
void CAesEncryption::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAesEncryption)
DDX_Text(pDX, IDC_EDIT_AES_KEY, m_Aes_Key);
DDX_Text(pDX, IDC_EDIT_AES_IV, m_Aes_IV);
DDX_Text(pDX, IDC_EDIT_AES_NewPLAIN, m_Aes_NewPlain);
DDV_MaxChars(pDX, m_Aes_NewPlain, 1024);
DDX_Text(pDX, IDC_EDIT_AES_PLAIN, m_Aes_Plain);
DDV_MaxChars(pDX, m_Aes_Plain, 1024);
DDX_Text(pDX, IDC_EDIT_AES_CIPHER, m_Aes_Cipher);
DDV_MaxChars(pDX, m_Aes_Cipher, 1024);
DDX_Text(pDX, IDC_EDIT_AES_KEYLENGTH, m_Aes_KeyLength);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAesEncryption, CDialog)
//{{AFX_MSG_MAP(CAesEncryption)
ON_BN_CLICKED(ID_AES_ENCRYPT, OnAesEncrypt)
ON_BN_CLICKED(ID_AES_DECRYPT, OnAesDecrypt)
ON_BN_CLICKED(IDC_AES_RESET, OnAesReset)
ON_BN_CLICKED(IDC_AES_EXIT, OnAesExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAesEncryption message handlers
void CAesEncryption::OnAesEncrypt()
{
// TODO: Add your control notification handler code here
#ifdef _DEBUG
CMemoryState oldMem,newMem,diffMem;
oldMem.Checkpoint();
#endif
//初始化数组
for(i=0;i<1024;i++)
{
aesplain11[i] = '\0';
aescipher11[i] = '\0';
}
aesenc=1;
if (UpdateData(TRUE) == 0)
{
MessageBox("错误!!","提示");
return;
}
switch(Key_Length)
{
case 128:
if(m_Aes_Key.GetLength() != 16)
{
MessageBox("密钥长度应为16,请重新输入^_^","提示");
return;
}
break;
case 192:
if(m_Aes_Key.GetLength() != 24)
{
MessageBox("密钥长度应为24,请重新输入^_^","提示");
return;
}
break;
case 256:
if(m_Aes_Key.GetLength() != 32)
{
MessageBox("密钥长度应为32,请重新输入^_^","提示");
return;
}
break;
default:
if(m_Aes_Key.GetLength() != 16)
{
MessageBox("密钥长度应为16,请重新输入^_^","提示");
return;
}
}
if(m_Aes_Plain=="")
{
MessageBox("明文为空,请输入!","提示");
return;
}
aeskey = (unsigned char *)m_Aes_Key.GetBuffer(8);
m_Aes_Key.ReleaseBuffer();
AES_set_encrypt_key(aeskey, Key_Length,keyPara);
Plain_Length = m_Aes_Plain.GetLength();
N=Plain_Length/16;
if(Plain_Length%16==0)
{
N=N;
}
else
{
Plain_Length=Plain_Length+16-Plain_Length%16;
N=N+1;
}
aesplain=aesplain11;
aescipher=aescipher11;
aesplain = (unsigned char *)m_Aes_Plain.GetBuffer(8);
m_Aes_Plain.ReleaseBuffer();
switch(Aes_Mode_Num)
{
//ECB工作模式加密代码
case 0:
if(m_Aes_Plain.GetLength()%16!=0)
{
for(i=0;i<(int)(16-m_Aes_Plain.GetLength()%16);i++)
{
aesplain[Plain_Length-16+m_Aes_Plain.GetLength()%16+i]='\0';
}
}
for(i=0;i<N;i++)
{
AES_ecb_encrypt(aesplain, aescipher, keyPara, aesenc);
aesplain+=16;
aescipher+=16;
}
break;
//CBC工作模式加密代码
case 1:
if(m_Aes_IV.GetLength() != 16)
{
MessageBox("初始向量长度应为16,请重新输入^_^","提示");
return;
}
for(i=0;i<16;i++)
AesCBCIV[i] = m_Aes_IV[i];
templength = m_Aes_Plain.GetLength();
AES_cbc_encrypt(aesplain, aescipher,templength, keyPara,AesCBCIV, aesenc) ;
break;
//CFB工作模式加密代码
case 2:
if(m_Aes_IV.GetLength() != 16)
{
MessageBox("初始向量长度应为16,请重新输入^_^","提示");
return;
}
for(i=0;i<16;i++)
AesCBCIV[i] = m_Aes_IV[i];//初始化IV
if(m_Aes_Plain.GetLength()%16!=0)
{
for(i=0;i<(int)(16-m_Aes_Plain.GetLength()%16);i++)
{
aesplain[Plain_Length-16+m_Aes_Plain.GetLength()%16+i]='\0';
}
}
for(i=0;i<N;i++)
{
AES_cfb8_encrypt(aesplain, aescipher,16, keyPara,AesCBCIV,0, aesenc);
aesplain+=16;
aescipher+=16;
}
break;
//OFB工作模式加密代码
case 3:
if(m_Aes_IV.GetLength() != 16)
{
MessageBox("初始向量长度应为16,请重新输入^_^","提示");
return;
}
for(i=0;i<16;i++)
AesCBCIV[i] = m_Aes_IV[i];
aesnum = new int;
*aesnum = 0;
if(m_Aes_Plain.GetLength()%16!=0)
{
for(i=0;i<(int)(16-m_Aes_Plain.GetLength()%16);i++)
{
aesplain[Plain_Length-16+m_Aes_Plain.GetLength()%16+i]='\0';
}
}
for(i=0;i<N;i++)
{
AES_ofb128_encrypt(aesplain, aescipher,16, keyPara,AesCBCIV,aesnum);
aesplain+=16;
aescipher+=16;
}
// delete aesnum;
// aesnum = NULL;
break;
//CTR工作模式加密代码
case 4:
if(m_Aes_IV.GetLength() != 16)
{
MessageBox("初始向量长度应为16,请重新输入^_^","提示");
return;
}
for(i=0;i<16;i++)
AesCBCIV[i] = m_Aes_IV[i];
for(i=0;i<16;i++)
AesCTREcount[i] = 0;
aesctrnum = new unsigned int;
*aesctrnum = 0;
if(m_Aes_Plain.GetLength()%16!=0)
{
for(i=0;i<(int)(16-m_Aes_Plain.GetLength()%16);i++)
{
aesplain[Plain_Length-16+m_Aes_Plain.GetLength()%16+i]='\0';
}
}
for(i=0;i<N;i++)
{
AES_ctr128_encrypt(aesplain, aescipher,16, keyPara,AesCBCIV,AesCTREcount,aesctrnum);
aesplain+=16;
aescipher+=16;
}
// delete aesctrnum;
// aesctrnum = NULL;
break;
}
aesplain = aesplain11;
aescipher = aescipher11;
m_Aes_Cipher=aescipher11;
UpdateData(FALSE);
#ifdef _DEBUG
newMem.Checkpoint();
if(diffMem.Difference(oldMem,newMem))
TRACE("内存泄露\n");
#endif
}
void CAesEncryption::OnAesDecrypt()
{
// TODO: Add your control notification handler code here
AES_set_decrypt_key(aeskey, Key_Length,keyPara);
aesplain = (unsigned char *)m_Aes_Cipher.GetBuffer(8);
m_Aes_Cipher.ReleaseBuffer();
aesenc=0;
Cipher_Length = m_Aes_Cipher.GetLength();
N = Cipher_Length/16;
if(Cipher_Length%16==0)
{
N=N;
}
else
{
Plain_Length=Cipher_Length+16-Cipher_Length%16;
N=N+1;
}
switch(Aes_Mode_Num)
{
//ECB工作模式解密代码
case 0:
for(i=0;i<N;i++)
{
AES_ecb_encrypt(aesplain, aescipher, keyPara, aesenc);
aesplain+=16;
aescipher+=16;
}
break;
//CBC工作模式解密代码
case 1:
for(i=0;i<16;i++)
AesCBCIV[i] = m_Aes_IV[i];
AES_cbc_encrypt(aesplain, aescipher,Plain_Length, keyPara,AesCBCIV, aesenc) ;
break;
//CFB工作模式解密代码
case 2:
for(i=0;i<16;i++)
AesCBCIV[i] = m_Aes_IV[i];
AES_set_encrypt_key(aeskey, Key_Length,keyPara);
for(i=0;i<N;i++)
{
AES_cfb8_encrypt(aesplain, aescipher,16, keyPara,AesCBCIV,0, aesenc);
aesplain+=16;
aescipher+=16;
}
break;
//OFB工作模式解密代码
case 3:
for(i=0;i<16;i++)
AesCBCIV[i] = m_Aes_IV[i];
*aesnum = 0;
AES_set_encrypt_key(aeskey, Key_Length,keyPara);
for(i=0;i<N;i++)
{
AES_ofb128_encrypt(aesplain, aescipher,16, keyPara,AesCBCIV,aesnum);
aesplain+=16;
aescipher+=16;
}
break;
//CTR工作模式解密代码
case 4:
for(i=0;i<16;i++)
AesCBCIV[i] = m_Aes_IV[i];
*aesctrnum = 0;
AES_set_encrypt_key(aeskey, Key_Length,keyPara);
for(i=0;i<N;i++)
{
AES_ctr128_encrypt(aesplain, aescipher,16, keyPara,AesCBCIV,AesCTREcount,aesctrnum);
aesplain+=16;
aescipher+=16;
}
break;
}
aesplain = aesplain11;
aescipher = aescipher11;
m_Aes_NewPlain=aescipher;
UpdateData(FALSE);
}
BOOL CAesEncryption::OnInitDialog()
{
CDialog::OnInitDialog();
keyPara=new AES_KEY;
for (i=0; i<16; i++)
{
AesCBCIV[i] = '\0';
AesCTREcount[i] = '\0';
}
for (i=0; i<1024; i++)
{
aesplain11[i] = '\0';
aescipher11[i] = '\0';
}
// TODO: Add extra initialization here
if (Key_Length != 128 && Key_Length != 192 && Key_Length != 256)
Key_Length = 128;
if(Aes_Mode !="ECB" && Aes_Mode !="CBC" && Aes_Mode !="CFB" && Aes_Mode !="OFB" && Aes_Mode !="CTR")
Aes_Mode= "ECB";
CEdit *pBoxOne1;
pBoxOne1 = (CEdit*) GetDlgItem(IDC_EDIT_AES_IV);
if(Aes_Mode == "CBC" || Aes_Mode == "CFB" || Aes_Mode == "OFB" || Aes_Mode == "CTR" )
pBoxOne1->EnableWindow (true);
else
pBoxOne1->EnableWindow (false);
switch(Key_Length)
{
case 128:
m_Aes_KeyLength = "密钥为16";
break;
case 192:
m_Aes_KeyLength = "密钥为24";
break;
case 256:
m_Aes_KeyLength = "密钥为32";
break;
default:
m_Aes_KeyLength = "密钥为16";
}
m_Aes_Key = "";
m_Aes_IV = "";
m_Aes_Plain = "";
m_Aes_Cipher = "";
m_Aes_NewPlain = "";
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CAesEncryption::OnAesReset()
{
// TODO: Add your control notification handler code here
m_Aes_Key = "";
m_Aes_IV = "";
m_Aes_Plain = "";
m_Aes_Cipher = "";
m_Aes_NewPlain = "";
UpdateData(FALSE);
}
void CAesEncryption::OnAesExit()
{
// TODO: Add your control notification handler code here
if (keyPara != NULL)
{
delete keyPara;
keyPara = NULL;
}
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -