2013-06-25 2 views
0

Je dois crypter une chaîne en Delphi avec l'algorithme Twofish/CBC, l'envoyer au serveur et décrypter là. J'ai testé le code ci-dessous, et le processus de codage/décodage B64 fonctionne, mais je suis bloqué au chiffrement/décryptage de chiffrement. J'utilise DEC 5.2 pour Delphi.Delphi DEC Twofish Cryptage et décryptage côté serveur en PHP

Voici le code Delphi qui fait le cryptage:

class function TEncryption.EncryptStream(const AInStream: TStream; const AOutStream: TStream; const APassword: String): Boolean; 
var 
    ASalt: Binary; 
    AData: Binary; 
    APass: Binary; 
begin 
    with ValidCipher(TCipher_Twofish).Create, Context do 
    try 
    ASalt := RandomBinary(16); 
    APass := ValidHash(THash_SHA1).KDFx(Binary(APassword), ASalt, KeySize); 
    Mode := cmCBCx; 
    Init(APass); 

    EncodeStream(AInStream, AOutStream, AInStream.Size); 
    result := TRUE; 
    finally 
    Free; 
    ProtectBinary(ASalt); 
    ProtectBinary(AData); 
    ProtectBinary(APass); 
    end; 
end; 

class function TEncryption.EncryptString(const AString, APassword: String): String; 
var 
    instream, outstream: TStringStream; 
begin 
    result := ''; 
    instream := TStringStream.Create(AString); 
    try 
    outstream := TStringStream.Create; 
    try 
     if EncryptStream(instream, outstream, APassword) then 
     result := outstream.DataString; 
    finally 
     outstream.Free; 
    end; 
    finally 
    instream.Free; 
    end; 
end; 

et la fonction PHP qui est censé décrypter les données envoyées:

function decrypt($input, $key) { 
    $td = mcrypt_module_open('twofish', '', 'cbc', ''); 
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
    mcrypt_generic_init($td, $key, $iv); 

    $decrypted_data = mdecrypt_generic($td, base64_decode_urlsafe($input)); 

    mcrypt_generic_deinit($td); 
    mcrypt_module_close($td); 

    return $decrypted_data; 
} 

Je beleive je dois jouer du violon un peu plus de sel et les vecteurs d'initialisation, cependant je n'ai aucune idée de comment. D'après ce que je comprends, la fonction KDFx() rend SHA1 hashed mot de passe du mot de passe de l'utilisateur et du sel, mais je suis bloqué à ce point.

Répondre