2016-08-05 1 views
0

Il existe quelques variantes de cette question, mais je n'ai pas été en mesure d'épingler le problème. Essayer de crypter/décrypter en PHP et Delphi Je suppose que je l'ai manqué quelques réglages à Delphes et son rapport avec UTF-8Delphi/PHP LockBox Encryption AES ECB

en utilisant http://aesencryption.net/ comme PHP Exemple le résultat que nous essayons d'obtenir. Image Coup
Password = 123
Key = Test
128 bits
Crypte à uuIikEZSC9Sa1HAt/XKfGQ ==

Je veux être en mesure de déchiffrer ce Delphi
J'utilise Delphi XE5
avec https://github.com/SeanBDurkin/tplockbox
je peux obtenir chiffrer/décrypter travailler à l'intérieur Delphi, mais la chaîne de version cryptée PHP est différent

Delphi crypte 123 à vpdeLlfnxTGrSsa2TpbFvg ==

Voici un exemple rapide de Delphi Crypter

function TForm3.EncryptV2(plainText: UTF8String): String; 
var CipherText : string; 
    FLibrary: TCryptographicLibrary; 
    FCodec: TCodec; 
begin 
    mmo1.Lines.Add('plaintext = ' + plainText); 

FLibrary := TCryptographicLibrary.Create(Self); 
    try 
    FCodec := TCodec.Create(Self); 
    try 
     FCodec.CryptoLibrary := FLibrary; 
     FCodec.StreamCipherId := BlockCipher_ProgId; 
     FCodec.BlockCipherId := Format(AES_ProgId, [256]); 
     FCodec.ChainModeId := ECB_ProgId; ; 
     FCodec.UTF8Password := 'test'; 
     FCodec.EncryptString(plainText, CipherText, Tencoding.UTF8); 
     FCodec.Burn; 

     result := CipherText; 
    finally 
     FCodec.Free; 
    end; 
    finally 
    FLibrary.Free; 
    end; 
end; 

Décrypter

function TForm3.DecryptV2(encryptedText: UTF8String): String; 
    var plainText : string; 
    FLibrary: TCryptographicLibrary; 
    FCodec: TCodec; 
begin 
    FLibrary := TCryptographicLibrary.Create(Self); 
    try 
    FCodec := TCodec.Create(Self); 
    try 
     FCodec.CryptoLibrary := FLibrary; 
     FCodec.StreamCipherId := BlockCipher_ProgId; 
     FCodec.BlockCipherId := Format(AES_ProgId, [256]); 
     FCodec.ChainModeId := ECB_ProgId; ; 
     FCodec.UTF8Password := 'test'; 

     mmo1.Lines.Add('Encrypted Text = ' + encryptedText); 
     FCodec.DecryptString(plainText, encryptedText,Tencoding.UTF8); 
     mmo1.Lines.Add('DeCrypted Text = ' + plainText); 
     result := plainText; 
    finally 
     FCodec.Free; 
    end; 
    finally 
    FLibrary.Free; 
    end; 
end; 

Quelqu'un at-il des suggestions?

enter image description here

+1

S'il vous plaît ne pas utiliser aesencryption.net comme référence pour le chiffrement, car il utilise un mauvais mode de (BCE), un mauvais remplissage (padding zéro) et ne prend pas en charge l'authentification . –

+1

** N'utilisez jamais [mode ECB] (http://crypto.stackexchange.com/q/14487/13022) **. C'est déterministe et donc pas sémantiquement sécurisé. Vous devriez à tout le moins utiliser un mode aléatoire comme [CBC] (http://crypto.stackexchange.com/q/22260/13022) ou [CTR] (http://crypto.stackexchange.com/a/2378/ 13022). Il est préférable d'authentifier vos textes chiffrés afin que les attaques comme une [attaque oracle padding] (http://crypto.stackexchange.com/q/18185/13022) ne soient pas possibles. Cela peut être fait avec des modes authentifiés comme GCM ou EAX, ou avec un schéma [encrypt-then-MAC] (http://crypto.stackexchange.com/q/202/13022). –

+0

Je ne pouvais pas obtenir Cbc ou d'autres pour correspondre non plus. Je pense que j'essaierais de commencer par le plus simple puis de voir si c'est la même raison que les autres ne correspondent pas – Dangas56

Répondre

0

Je ne sais pas ce qui ne va pas avec lockbox, mais voici le code qui correspond à aesencryption en utilisant OpenSSL, unité OverbyteIcsLibeay est de la bibliothèque ICS http://wiki.overbyte.be/wiki/index.php/ICS_Download

{$APPTYPE CONSOLE} 
program aestest; 

uses System.SysUtils, System.NetEncoding, OverbyteIcsLibeay; 

type 
    TKey128 = packed array [0..15] of byte; 
    TIV128 = packed array [0..15] of byte; 

function AES128EncryptDecrypt(var Source: TBytes; const Key: TKey128; 
    const InitializationVector: TIV128; Encrypt: boolean): boolean; 
var 
    IV: TIV128; 
    CipherCtx: PEVP_CIPHER_CTX; 
    Dest: TBytes; 
    OutLen: Integer; 
begin 
    Result := False; 
    IV := InitializationVector; 
    LoadLibeayEx; 
    SetLength(Dest, Length(Source) + Length(Key)); 
    CipherCtx := f_EVP_CIPHER_CTX_new; 
    try 
    f_EVP_CIPHER_CTX_init(CipherCtx); 
    if Encrypt then 
    begin 
     if f_EVP_EncryptInit_ex(CipherCtx, f_EVP_aes_128_ecb(), nil, @Key[0], @IV[0]) then 
     begin 
     Result := f_EVP_EncryptUpdate(CipherCtx, @Dest[Low(Dest)], OutLen, @Source[Low(Source)], Length(Source)); 
     if Result then 
      Source := Copy(Dest, Low(Dest), OutLen); 
     end; 
    end 
    else 
    begin 
     if f_EVP_DecryptInit_ex(CipherCtx, f_EVP_aes_128_ecb(), nil, @Key[0], @IV[0]) then 
     begin 
     SetLength(Source, Length(Source) + Length(Key)); 
     Result := f_EVP_DecryptUpdate(CipherCtx, @Dest[Low(Dest)], OutLen, @Source[Low(Source)], Length(Source)); 
     if Result then 
      Source := Copy(Dest, Low(Dest), OutLen); 
     end; 
    end; 
    f_EVP_CIPHER_CTX_cleanup(CipherCtx); 
    finally 
    f_EVP_CIPHER_CTX_free(CipherCtx); 
    end; 
end; 

function AES128Encrypt(var Source: TBytes; const Key: TKey128; 
    const InitializationVector: TIV128): boolean; 
begin 
    Result := AES128EncryptDecrypt(Source, Key, InitializationVector, True); 
end; 

function AES128Decrypt(var Source: TBytes; const Key: TKey128; 
    const InitializationVector: TIV128): boolean; 
begin 
    Result := AES128EncryptDecrypt(Source, Key, InitializationVector, False); 
end; 

const 
    DefaultInitializationVector: TIV128 = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 

var 
    B: TBytes; 
    KeyBytes: TBytes; 
    KeyPass: TKey128; 
begin 
    // padding text with zeroes up to 16 bytes 
    B := TEncoding.UTF8.GetBytes('123'#0#0#0#0#0#0#0#0#0#0#0#0#0); 

    // encrypting 
    KeyBytes := TEncoding.UTF8.GetBytes('test'); 
    Move(KeyBytes[0], KeyPass[0], Length(KeyBytes)); 
    AES128Encrypt(B, KeyPass, DefaultInitializationVector); 
    Writeln(TNetEncoding.Base64.EncodeBytesToString(B)); 

    // decrypting 
    AES128Decrypt(B, KeyPass, DefaultInitializationVector); 
    Writeln(TEncoding.UTF8.GetString(B)); 
end. 

également la fonction f_EVP_aes_128_ecb actuellement pas inclus dans OverbyteIcsLibeay , donc vous devrez ajouter cette ligne à la section interface

f_EVP_aes_128_ecb   : function: PEVP_CIPHER; cdecl = nil; 

et ces lignes à la procédure LoadLibeay

f_EVP_aes_128_ecb := GetProcAddress(GLIBEAY_DLL_Handle, 'EVP_aes_128_ecb'); 
if not Assigned(f_EVP_aes_128_ecb) then 
    raise Exception.Create(Msg + 'EVP_aes_128_ecb');