Advertisement
Guest User

Untitled

a guest
Sep 27th, 2022
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.51 KB | Software | 0 0
  1. procedure TForm371.Button1Click(Sender: TObject);
  2. var
  3.   KeyArray: TBytes;
  4.   iv: TBytes;
  5.   mmIV: TAESBlock;
  6.   clearText: TBytes;
  7.   aad:  TBytes;
  8.   ciphertext: TBytes;
  9.   checktag : TAESBlock;
  10.   checktag2 : TAESBlock;
  11.   checktag4 : TAESBlock;
  12.   checktag3 : TAESBlock;
  13.   rawClear : RawByteString;
  14.   rawciphertext : RawByteString;
  15.   aad2 : TAESKey;
  16.  begin
  17.   KeyArray := [$11, $11, $11, $11, $11, $11, $11, $11, $11, $11, $11, $11, $11, $11, $11, $11];
  18.  
  19.   iv := [$4D, $45, $54, $00, $00, $00, $00, $01, $00, $00, $00, $00];
  20.   FillChar(mmIv, 16, 0);
  21.   System.Move(iv[0], mmIv, System.Length(iv) );
  22.  
  23.   cleartext := [$0f, $c0, $00, $00, $01, $0c, $07, $e6, $09, $1a, $01, $03, $27, $2e, $5a, $ff, $88, $80, $02, $02, $09, $06, $00, $01, $19, $09, $00, $ff, $01, $01, $02, $06, $09, $0c, $07, $e6, $09, $1a, $01, $03, $23, $00, $00, $ff, $88, $80, $11, $5a, $17, $49, $cb, $25, $1d, $17, $49, $cb, $25, $1d, $17, $49, $cb, $25, $1d, $17, $49, $cb, $25, $1d];
  24.   aad := [$30, $33, $33, $33, $33, $33, $33, $33, $33, $33, $33, $33, $33, $33, $33, $33, $33];
  25.  
  26.   setlength(ciphertext, length(cleartext));
  27.  
  28.  
  29.  
  30.   var mAES := TAESGCM.Create(KeyArray); try
  31.     mAES.IV := mmIv;
  32.     mAES.Encrypt(nil, nil, 0);
  33.     mAES.AesGcmAad(Pointer(aad), System.Length(aad));
  34.     rawciphertext := mAES.EncryptPkcs7(Convert(cleartext));
  35.     Move(rawciphertext[1], ciphertext[0], length(rawciphertext));
  36.  
  37.     FillChar(checktag, 16, 255);
  38.     mAES.AesGcmFinal(checktag);
  39.   finally
  40.     mAES.Free;
  41.   end;
  42.  
  43.   var mAES2 := TAESGCM.Create(KeyArray);
  44.   try
  45.     mAES2.IV := mmIv;
  46.     mAES2.Encrypt(nil, nil, 0);
  47.     rawciphertext := mAES.EncryptPkcs7(Convert(cleartext));
  48.     FillChar(checktag2, 16, 255);
  49.     mAES2.AesGcmFinal(checktag2);
  50.   finally
  51.     mAES2.Free;
  52.   end;
  53.  
  54.   var mAES3 := TAESGCM.Create(KeyArray);
  55.   try
  56.    mAES3.IV := mmIv;
  57.    mAES3.Encrypt(nil, nil, 0);
  58.  
  59.    System.Move(aad[0], aad2[0], 12);
  60.    // Use MacSetNonce instead of AesGcmAAD
  61.    mAES3.MacSetNonce(False, aad2);
  62.    rawciphertext := mAES3.EncryptPkcs7(Convert(cleartext));
  63.    FillChar(checktag3, 16, 255);
  64.    mAES3.AesGcmFinal(checktag3);
  65.   finally
  66.     mAES3.Free;
  67.   end;
  68.  
  69.   var mAES4 := TAESGCM.Create(KeyArray);
  70.   try
  71.    mAES4.IV := mmIv;
  72.    mAES4.Encrypt(nil, nil, 0);
  73.    rawClear := Convert(iv) + Convert([$00, $00, $00, $00]) + Convert(cleartext);
  74.    rawciphertext := mAES4.MacAndCrypt(rawClear, True, True, '3033333333333333333333333333333333');
  75.    FillChar(checktag4, 16, 255);
  76.    mAES4.AesGcmFinal(checktag4);
  77.   finally
  78.     mAES4.Free;
  79.   end;
  80. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement