Skip to content

Instantly share code, notes, and snippets.

Created November 18, 2017 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/fa917ec43bfaa56f2f2154447137bcbb to your computer and use it in GitHub Desktop.
Save anonymous/fa917ec43bfaa56f2f2154447137bcbb to your computer and use it in GitHub Desktop.
program Test_SynCrypto_AES_GUI;
uses
Vcl.Forms,
uMainForm in 'uMainForm.pas' {MainForm};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
object MainForm: TMainForm
Left = 0
Top = 0
Caption = 'MainForm'
ClientHeight = 603
ClientWidth = 635
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object btnEncrypt: TButton
Left = 64
Top = 376
Width = 153
Height = 113
Caption = 'btnEncrypt'
TabOrder = 0
OnClick = btnEncryptClick
end
object btnDecrypt: TButton
Left = 312
Top = 376
Width = 193
Height = 113
Caption = 'btnDecrypt'
TabOrder = 1
OnClick = btnDecryptClick
end
object mmoPlain: TMemo
Left = 48
Top = 64
Width = 217
Height = 273
Lines.Strings = (
#27979#35797#26126#25991#31532'1'#34892
#27979#35797#26126#25991#31532'2'#34892)
TabOrder = 2
end
object mmoCipher: TMemo
Left = 271
Top = 64
Width = 290
Height = 273
TabOrder = 3
end
end
unit uMainForm;
interface
uses
Windows, Messages,
SysUtils, Variants, Classes,
Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TMainForm = class(TForm)
btnEncrypt: TButton;
btnDecrypt: TButton;
mmoPlain: TMemo;
mmoCipher: TMemo;
procedure btnEncryptClick(Sender: TObject);
procedure btnDecryptClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
uses
SynCommons, SynLog, SynCrypto;
const
TEST_KEY: string = 'TEST_KEY';
procedure TMainForm.btnEncryptClick(Sender: TObject);
var
Key: TSHA256Digest;
plain, cipher: RawByteString;
begin
SHA256Weak(TEST_KEY, Key);
plain := StringToUTF8(mmoPlain.Text);
// mmoPlain.Text := UTF8ToString(plain);
// mmoPlain.Text := UTF8ToString(AnyAnsiToUTF8(plain));
// Exit;
cipher := SynCrypto.AES(Key, 256, plain, true);
mmoCipher.Text := UTF8ToString(BinToBase64(cipher));
end;
type
ChineseString = type Ansistring(936); // https://stackoverflow.com/questions/7222615/how-can-i-convert-string-encoded-with-windows-codepage-1251-to-a-unicode-string
procedure TMainForm.btnDecryptClick(Sender: TObject);
var
Key: TSHA256Digest;
plain, cipher: RawByteString;
ChineseStr: ChineseString;
begin
SHA256Weak(TEST_KEY, Key);
cipher := Base64ToBin(StringToUTF8(mmoCipher.Text));
plain := SynCrypto.AES(Key, 256, cipher, false);
mmoPlain.Text := UTF8ToString(AnyAnsiToUTF8(plain)); // Does not work
// mmoPlain.Text := UTF8ToString(CurrentAnsiConvert.AnsiToUTF8(plain)); // Does not work
// mmoPlain.Text := UTF8ToUnicodeString(CurrentAnsiConvert.AnsiToUTF8(plain)); // Does not work
// ChineseStr := UTF8ToString(AnyAnsiToUTF8(plain)); // Does not work
// mmoPlain.Text := ChineseStr;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment