微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP河豚加密字符串,如何在Lazarus中解密

如何解决PHP河豚加密字符串,如何在Lazarus中解密

我在 PHP 中有代码来使用河豚和 sha256 哈希加密字符串:

<?PHP
    $plaintext = "Secret text";
    $ivlen = openssl_cipher_iv_length($cipher="BF-CBC");
    $iv = '1234567812345678';
    $key = 'password';
    $ciphertext_raw = openssl_encrypt($plaintext,$cipher,$key,$options=OPENSSL_RAW_DATA,$iv);
    $ciphertext = base64_encode( $ciphertext_raw );
    echo "encrypted string:<br>$ciphertext2<br><br>";
?>

我在 Lazarus 中有解密这个字符串的代码

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes,SysUtils,Forms,Controls,Graphics,Dialogs,StdCtrls,DCPblowfish,Dcpsha256,base64;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    DCP_blowfish1: TDCP_blowfish;
    DCP_sha256_1: TDCP_sha256;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;
  

implementation

{$R *.frm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
    Cipher: TDCP_blowfish;
    iv: string;
    KeyStr: string;
    encrypted_string: string;
    decrypted_string: string;
begin
    memo1.lines.clear;
    encrypted_string := 'text from PHP';
    KeyStr := 'password';   
    iv := '1234567812345678';
    Cipher := TDCP_blowfish.Create(Self);
    Cipher.InitStr(KeyStr,TDCP_sha256);        
    cipher.SetIV(iv);
    decrypted_string := Cipher.DecryptString(DecodeStringBase64(encrypted_string));
    memo1.lines.add(decrypted_string);
    Cipher.Burn;
    Cipher.Free;
    Dest.Free;
        
end;

end.

但是解密后的字符串不是我所期望的:( 我的错误在哪里?或者在PHP中使用blowfish加密字符串并在Lazarus中解密(使用DCPCrypt?)的最简单方法是什么? 谢谢

更新:

我已更改代码以使用文件而不是纯文本字符串 PHP

<?PHP
    $plaintext = "secret";
    $ivlen = openssl_cipher_iv_length($cipher="BF-CBC");
    $iv = '12345678';
    $key = hash('sha256','password');
    $ciphertext_raw = openssl_encrypt($plaintext,$iv);
    file_put_contents('file.enc',$ciphertext_raw);
?>

拉撒路:

procedure TForm1.Button2Click(Sender: TObject);
var
    Cipher: TDCP_blowfish;
    Source,Dest: TFileStream;
    iv: string;
  begin
    stream:= TStream.create;
    KeyStr := 'password';
    iv := '12345678';
    Source:= TFileStream.Create('file.enc',fmOpenRead);   
        Dest:= TFileStream.Create(Edit2.Text,fmCreate);   
        Cipher := TDCP_blowfish.Create(Self);
        Cipher.InitStr(KeyStr,TDCP_sha256);    
        cipher.SetIV(iv);
        Cipher.DecryptStream(Source,Dest,Source.Size); 

        Cipher.Burn;
        Cipher.Free;
        Dest.Free;
        Source.Free;
  end;       

它仍然不起作用:(

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。