Created
November 18, 2017 16:36
-
-
Save anonymous/1995e7bdb5c1f35e0740691bad7f2974 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program FPC_AES_GUI; | |
{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64 OWNNORMTOUPPER | |
{$IFOPT D+} {$DEFINE DEBUG} {$ENDIF} | |
{$ASSERTIONS ON} | |
uses | |
{$I SynDprUses.inc} | |
Interfaces, // this includes the LCL widgetset | |
Forms, uMainForm | |
{ you can add units after this }; | |
{$R *.res} | |
begin | |
RequireDerivedFormResource:=True; | |
Application.Initialize; | |
Application.CreateForm(TMainForm, MainForm); | |
Application.Run; | |
end. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program Test_SynCrypto_AES_GUI; | |
{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64 OWNNORMTOUPPER | |
{$IFOPT D+} {$DEFINE DEBUG} {$ENDIF} | |
{$ASSERTIONS ON} | |
uses | |
{$I SynDprUses.inc} | |
Forms, | |
uMainForm in 'uMainForm.pas' {MainForm}; | |
{$R *.res} | |
begin | |
Application.Initialize; | |
Application.MainFormOnTaskbar := True; | |
Application.CreateForm(TMainForm, MainForm); | |
Application.Run; | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MainForm: TMainForm | |
Left = 0 | |
Top = 0 | |
Caption = 'MainForm' | |
ClientHeight = 603 | |
ClientWidth = 730 | |
Color = clBtnFace | |
Font.Charset = DEFAULT_CHARSET | |
Font.Color = clWindowText | |
Font.Height = -11 | |
Font.Name = 'Tahoma' | |
Font.Style = [] | |
OldCreateOrder = False | |
OnCreate = FormCreate | |
PixelsPerInch = 96 | |
TextHeight = 13 | |
object lblPlain: TLabel | |
Left = 96 | |
Top = 24 | |
Width = 22 | |
Height = 13 | |
Caption = 'Plain' | |
end | |
object lblCipher: TLabel | |
Left = 288 | |
Top = 24 | |
Width = 31 | |
Height = 13 | |
Caption = 'Cipher' | |
end | |
object lblCodePage: TLabel | |
Left = 520 | |
Top = 24 | |
Width = 49 | |
Height = 13 | |
Caption = 'CodePage' | |
end | |
object btnEncryptCorrect: TButton | |
Left = 8 | |
Top = 451 | |
Width = 185 | |
Height = 113 | |
Caption = 'Encrypt correct' | |
TabOrder = 0 | |
OnClick = btnEncryptCorrectClick | |
end | |
object btnDecryptCorrect: TButton | |
Left = 215 | |
Top = 451 | |
Width = 186 | |
Height = 113 | |
Caption = 'Decrypt correct' | |
TabOrder = 1 | |
OnClick = btnDecryptCorrectClick | |
end | |
object mmoPlain: TMemo | |
Left = 8 | |
Top = 43 | |
Width = 185 | |
Height = 249 | |
TabOrder = 2 | |
end | |
object mmoCipher: TMemo | |
Left = 215 | |
Top = 43 | |
Width = 186 | |
Height = 249 | |
TabOrder = 3 | |
end | |
object mmoCodePage: TMemo | |
Left = 432 | |
Top = 43 | |
Width = 281 | |
Height = 249 | |
Lines.Strings = ( | |
'mmoCodePage') | |
TabOrder = 4 | |
end | |
object btnEncryptWrong: TButton | |
Left = 8 | |
Top = 307 | |
Width = 185 | |
Height = 113 | |
Caption = 'Encrypt wrong' | |
TabOrder = 5 | |
OnClick = btnEncryptWrongClick | |
end | |
object btnDecryptWrong: TButton | |
Left = 215 | |
Top = 307 | |
Width = 186 | |
Height = 113 | |
Caption = 'Decrypt wrong' | |
TabOrder = 6 | |
OnClick = btnDecryptWrongClick | |
end | |
object btnResetPlain: TButton | |
Left = 432 | |
Top = 307 | |
Width = 281 | |
Height = 257 | |
Caption = 'Reset plain' | |
TabOrder = 7 | |
OnClick = btnResetPlainClick | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit uMainForm; | |
{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64 OWNNORMTOUPPER | |
{$IFOPT D+} {$DEFINE DEBUG} {$ENDIF} | |
{$ASSERTIONS ON} | |
interface | |
uses | |
Windows, Messages, | |
SysUtils, Variants, Classes, | |
Graphics, Controls, Forms, Dialogs, StdCtrls; | |
type | |
TMainForm = class(TForm) | |
btnEncryptCorrect: TButton; | |
btnDecryptCorrect: TButton; | |
mmoPlain: TMemo; | |
mmoCipher: TMemo; | |
mmoCodePage: TMemo; | |
btnEncryptWrong: TButton; | |
btnDecryptWrong: TButton; | |
btnResetPlain: TButton; | |
lblPlain: TLabel; | |
lblCipher: TLabel; | |
lblCodePage: TLabel; | |
procedure btnEncryptCorrectClick(Sender: TObject); | |
procedure btnDecryptCorrectClick(Sender: TObject); | |
procedure btnEncryptWrongClick(Sender: TObject); | |
procedure btnDecryptWrongClick(Sender: TObject); | |
procedure FormCreate(Sender: TObject); | |
procedure btnResetPlainClick(Sender: TObject); | |
private | |
{ Private declarations } | |
public | |
{ Public declarations } | |
end; | |
var | |
MainForm: TMainForm; | |
implementation | |
{$R *.dfm} | |
uses | |
SynCommons, SynLog, SynCrypto; | |
const | |
// PLAIN: string = '测试明文第1行' + sLineBreak + '测试明文第2行'; // Does not matter | |
PLAIN: string = '测试明文第1行' + sLineBreak + '测试明文第2行' + sLineBreak; | |
AES_KEY: string = 'TEST_AES_KEY'; | |
procedure TMainForm.FormCreate(Sender: TObject); | |
begin | |
mmoPlain.Text := PLAIN; | |
end; | |
procedure TMainForm.btnResetPlainClick(Sender: TObject); | |
begin | |
mmoPlain.Text := PLAIN; | |
end; | |
procedure TMainForm.btnEncryptCorrectClick(Sender: TObject); | |
var | |
Key: TSHA256Digest; | |
plain, cipherBase64Encoded: string; | |
plainUTF8, cipherBase64EncodedUTF8: RawUTF8; | |
plainRaw, cipherRaw: RawByteString; | |
begin | |
SHA256Weak(CurrentAnsiConvert.UTF8ToAnsi(StringToUTF8(AES_KEY)), Key); | |
plain := mmoPlain.Text; | |
mmoCodePage.Lines.Add(Format('encrypt - plain - %d', [StringCodePage(plain)])); | |
plainUTF8 := StringToUTF8(plain); | |
mmoCodePage.Lines.Add(Format('encrypt - plainUTF8 - %d', [StringCodePage(plainUTF8)])); | |
plainRaw := CurrentAnsiConvert.UTF8ToAnsi(plainUTF8); | |
mmoCodePage.Lines.Add(Format('encrypt - plainRaw - %d', [StringCodePage(plainRaw)])); | |
cipherRaw := SynCrypto.AES(Key, 256, plainRaw, true); | |
mmoCodePage.Lines.Add(Format('encrypt - cipherRaw - %d', [StringCodePage(cipherRaw)])); | |
cipherBase64EncodedUTF8 := BinToBase64(cipherRaw); | |
mmoCodePage.Lines.Add(Format('encrypt - cipherBase64EncodedUTF8 - %d', [StringCodePage(cipherBase64EncodedUTF8)])); | |
cipherBase64Encoded := UTF8ToString(cipherBase64EncodedUTF8); | |
mmoCodePage.Lines.Add(Format('encrypt - cipherBase64Encoded - %d', [StringCodePage(cipherBase64Encoded)])); | |
mmoCipher.Text := cipherBase64Encoded; | |
end; | |
procedure TMainForm.btnDecryptCorrectClick(Sender: TObject); | |
var | |
Key: TSHA256Digest; | |
plain, cipherBase64Encoded: string; | |
plainUTF8, cipherBase64EncodedUTF8: RawUTF8; | |
plainRaw, cipherBase64EncodedRaw, cipherRaw: RawByteString; | |
begin | |
SHA256Weak(CurrentAnsiConvert.UTF8ToAnsi(StringToUTF8(AES_KEY)), Key); | |
cipherBase64Encoded := mmoCipher.Text; | |
mmoCodePage.Lines.Add(Format('decrypt - cipherBase64Encoded - %d', [StringCodePage(cipherBase64Encoded)])); | |
cipherBase64EncodedUTF8 := StringToUTF8(cipherBase64Encoded); | |
mmoCodePage.Lines.Add(Format('decrypt - cipherBase64EncodedUTF8 - %d', [StringCodePage(cipherBase64EncodedUTF8)])); | |
cipherBase64EncodedRaw := CurrentAnsiConvert.UTF8ToAnsi(cipherBase64EncodedUTF8); | |
mmoCodePage.Lines.Add(Format('decrypt - cipherBase64EncodedRaw - %d', [StringCodePage(cipherBase64EncodedRaw)])); | |
cipherRaw := Base64ToBin(cipherBase64EncodedRaw); | |
mmoCodePage.Lines.Add(Format('decrypt - cipherRaw - %d', [StringCodePage(cipherRaw)])); | |
plainRaw := SynCrypto.AES(Key, 256, cipherRaw, false); | |
mmoCodePage.Lines.Add(Format('decrypt - plainRaw - %d', [StringCodePage(plainRaw)])); | |
plainUTF8 := CurrentAnsiConvert.AnsiToUTF8(plainRaw); | |
mmoCodePage.Lines.Add(Format('decrypt - plainUTF8 - %d', [StringCodePage(plainUTF8)])); | |
plain := UTF8ToString(plainUTF8); | |
mmoCodePage.Lines.Add(Format('decrypt - plain - %d', [StringCodePage(plain)])); | |
mmoPlain.Text := plain; | |
end; | |
// https://synopse.info/forum/viewtopic.php?id=4235 | |
// https://gist.github.com/anonymous/fa917ec43bfaa56f2f2154447137bcbb | |
procedure TMainForm.btnEncryptWrongClick(Sender: TObject); | |
var | |
Key: TSHA256Digest; | |
plain, cipher: RawByteString; | |
begin | |
SHA256Weak(CurrentAnsiConvert.UTF8ToAnsi(StringToUTF8(AES_KEY)), Key); | |
plain := StringToUTF8(mmoPlain.Text); | |
cipher := SynCrypto.AES(Key, 256, plain, true); | |
// cipher := SynCrypto.AES(Key, 256, CurrentAnsiConvert.UTF8ToAnsi(plain), true); | |
mmoCipher.Text := UTF8ToString(BinToBase64(cipher)); | |
end; | |
procedure TMainForm.btnDecryptWrongClick(Sender: TObject); | |
var | |
Key: TSHA256Digest; | |
plain, cipher: RawByteString; | |
begin | |
SHA256Weak(CurrentAnsiConvert.UTF8ToAnsi(StringToUTF8(AES_KEY)), Key); | |
cipher := Base64ToBin(StringToUTF8(mmoCipher.Text)); | |
plain := SynCrypto.AES(Key, 256, cipher, false); | |
// plain := CurrentAnsiConvert.AnsiToUTF8(SynCrypto.AES(Key, 256, cipher, false)); // Calling of CurrentAnsiConvert routines has to be "symmetric" | |
mmoPlain.Text := UTF8ToString(plain); | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment