php - 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 中使用河豚加密字符串并在 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, $cipher, $key, $options=OPENSSL_RAW_DATA, $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;
它仍然无法正常工作:(
解决方案
推荐阅读
- php-curl - 使用从 cURL 返回的值作为授权标头进行后续调用
- android - Android 数据绑定到自定义视图
- c - 有没有办法从 bash 脚本中执行编译的 C 代码?
- c# - 在 IIS .net 核心 wwwroot 上找不到 CSS 文件夹
- c - “数组”函数正在阻止程序正常运行
- java - 如何从 sql 查询构建嵌套对象
- python - Python 列表索引必须是整数或切片,而不是 str 在制作列表时
- angular - 如何正确测试角度服务流
- c# - 为什么元数据文件中存在某些类型?
- vim - 我在哪里可以重置 VIM 中 cErrInParen 和 cErrInBracket 的突出显示?