본문 바로가기

Delphi/꿀팁

[LockBox] AES 암/복호화 하기

uses 
  .., uTPLb_Constants, uTPLb_Codec, uTPLb_CryptographicLibrary;
function EncString(AStr: string): string;
var
  tmpCodec: TCodec;
  tmpLibrary: TCryptographicLibrary;
begin
  Result := '';

  tmpLibrary := TCryptographicLibrary.Create(nil);
  tmpCodec := TCodec.Create(nil);
  
  try
    tmpCodec.CryptoLibrary := tmpLibrary;
	tmpCodec.StreamCipherId := BlockCipher_ProgId;
	tmpCodec.BlockCipherId := Format(AES_ProgId, [256]); //Key Size
	tmpCodec.ChainModeId := CBC_ProgId;
    tmpCodec.Password := 'CustomKey';
    tmpCodec.EncryptString(AStr, Result, TEncoding.UTF8);
  finally
    tmpCodec.Free;
    tmpLibrary.Free;
  end;
end;
function DecString(AStr: string): string;
var
  tmpCodec: TCodec;
  tmpLibrary: TCryptographicLibrary;
begin
  Result := '';

  tmpLibrary := TCryptographicLibrary.Create(nil);
  tmpCodec := TCodec.Create(nil);
  
  try
    tmpCodec.CryptoLibrary := Library;
    tmpCodec.StreamCipherId := BlockCipher_ProgId;
    tmpCodec.BlockCipherId := Format(AES_ProgId, [256]); //Key Size
    tmpCodec.ChainModeId := CBC_ProgId;
    tmpCodec.Password := 'CustomKey';
    tmpCodec.DecryptString(Result, AStr, TEncoding.UTF8);
  finally
    tmpCodec.Free;
    tmpLibrary.Free;
  end;
end;
반응형