📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "XAes"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
BYTE key[32],IV[16];
char encBuf[64]; // 待加密的数据
char strBuf[128]; // 数据转换用
// 判断密钥长度
if( RB16->Checked )
XAes1->KeyLen = 16;
else if( RB24->Checked )
XAes1->KeyLen = 24;
else XAes1->KeyLen = 32;
// 检查密钥是否输入完整
if( Edit2->Text.Length() < XAes1->KeyLen * 2 )
{
ShowMessage("密钥长度不对");
return;
}
XAes1->StrToHex(Edit2->Text.c_str(),XAes1->KeyLen*2,key,XAes1->KeyLen);
// 检查数据
int inlen = Edit1->Text.Length();
if( inlen % 32 )
{
ShowMessage("数据长度不对");
return;
}
if( XAes1->StrToHex(Edit1->Text.c_str(),inlen,encBuf,inlen/2) == false )
{
ShowMessage("数据格式不对");
return;
}
inlen = inlen/2;
// IV -- 固定为16 byte
XAes1->StrToHex(Edit3->Text.c_str(),32,IV,16);
// 加密
XAes1->AESEncBuf( encBuf, inlen,encBuf, key ,IV);
Edit4->Text = AnsiString(XAes1->HexToStr(encBuf,strBuf,inlen));
// 解密
XAes1->AESDecBuf( encBuf, inlen,encBuf, key ,IV);
Edit5->Text = AnsiString(XAes1->HexToStr(encBuf,strBuf,inlen));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnFileClick(TObject *Sender)
{
BYTE Key[16];
memset(Key,1,16); // 密钥为全0x1
XAes1->KeyLen = 16;
if( XAes1->AESEncFile( "test1.txt", "test1.aes",Key ) != 1 )
{
ShowMessage("error");
return;
}
if( XAes1->AESDecFile( "test1.aes", "test2.txt",Key ) != 1 )
{
ShowMessage("error");
return;
}
ShowMessage("加解密完成!\r源文件:test1.txt\r密文:test1.aes\r解密后文件:test2.txt");
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -