unit Cript_Sis;

interface

uses
  System.SysUtils, DCPcrypt2, DCPblockciphers, DCPrijndael, DCPbase64, System.Hash;

type
  TCript_Sis = class
  private
    function LPadding( sField : string; iLength : integer; sChar: string ) : string;
  public
    function Encriptar ( sDados : string ) : string;
    function Descriptar( sDados : string ) : string;
  end;

const
  Chave_Simsoft = '(- SIMSOFT TECNOLOGIA -) :: (-SOFTWARE-) :: [ 2020 ]';
  Iv_Simsoft    = '1234567890123456';

// ----------------------------------------------------------
//  Biblioteca para criptografia (AES 256 bits) padronizada
// ----------------------------------------------------------
//
implementation
//
{ EXEMPLO:
  Cript_Sis := TCript_Sis.Create();
  sCriptografado := Cript_Sis.Encriptar('SEUS DADOS VAI AQUI');
  Memo1.Lines.Add( sCriptografado );
  //
  sTexto := Cript.Descriptar( sCriptografado );
  Memo1.Lines.Add( sTexto );
  Cript.Free;
}
//
Function TCript_Sis.Encriptar( sDados : string ) : string;
var Cipher : TDCP_rijndael;
    sData_Cript, sIv : Ansistring;
    Hash_S256 : THashSHA2;
    Hash__MD5 : THashMD5;
    bKey_S256 : TBytes;
    aKey_S256 : array[0..31] of byte;
    sHash_Txt : string;
    iContador, iLarguraReal, iLarguraC, iPad : Integer;
begin
  sIv := Iv_Simsoft+'';
  //
  Hash_S256 := THashSHA2.Create(SHA256);
  bKey_S256 := Hash_S256.GetHashBytes( Chave_Simsoft );
  //
  Hash__MD5 := THashMD5.Create();
  sHash_Txt := Hash__MD5.GetHashString(sDados);
  //
  sData_Cript := LPadding( IntToStr(Length(sDados)), 3, '0' )+'|'+sDados+'|MD5|'+sHash_Txt;
  //
  Cipher := TDCP_rijndael.Create(nil);
  try
    Cipher.Init( bKey_S256[0], 256, @sIv[1] );
    {start padding}
    iLarguraReal := Length(sData_Cript);
    iLarguraC := (Cipher.BlockSize div 8);
    iPad := iLarguraC - (iLarguraReal mod iLarguraC);
    for iContador := 1 to iPad do
      sData_Cript := sData_Cript+chr(iPad);
    {end padding}
    Cipher.EncryptCBC(sData_Cript[1],sData_Cript[1],Length(sData_Cript));
  finally
    Cipher.Free;
  end;
  sData_Cript := DCPBase64.Base64EncodeStr(sData_Cript);
  //
  Result := sData_Cript+'';
end;
//
//
Function TCript_Sis.Descriptar( sDados : string ) : string;
var Cipher : TDCP_rijndael;
    sData_Cript, sIv : Ansistring;
    sDados_Texto : string;
    Hash_S256 : THashSHA2;
    bKey_S256 : TBytes;
    iContador, iLarguraReal, iLarguraC, iPad : Integer;
begin
  sIv := Iv_Simsoft+'';
  //
  Hash_S256 := THashSHA2.Create(SHA256);
  bKey_S256 := Hash_S256.GetHashBytes( Chave_Simsoft );
  //
  sData_Cript := sDados+'';
  sData_Cript := DCPBase64.Base64DecodeStr( sData_Cript );
  Cipher := TDCP_rijndael.Create(nil);
  Cipher.Init( bKey_S256[0], 256, @sIv[1] );
  Cipher.DecryptCBC( sData_Cript[1], sData_Cript[1], Length(sData_Cript) );
  Cipher.Free;
  //
  iLarguraReal := StrToInt( Copy( sData_Cript, 1, 3) );
  sDados_Texto := Copy( sData_Cript, 5, iLarguraReal );
  //
  Result := sDados_Texto;
  //
end;
//
//
function TCript_Sis.LPadding( sField : string; iLength : integer; sChar: string ) : string;
begin
  while Length(sField) < iLength do
  begin
    sField := sChar + sField;
  end;
  Result := Copy(sField, 1, iLength);
end;
//
//
end.
