- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个用 Delphi 2007 开发的应用程序,其中一个值由 PHP 加密并在应用程序中解密。加密算法是 RIJNDAEL 128。当我移动 XE2 并安装最新版本的 DCPcrypt 时,应用程序运行但不再能够从 PHP 解密加密的字符串。结果看起来像汉字,所以我想知道是否需要修改我对加密 key 、向量或加密字符串的处理,以说明 XE2 使用 Unicode 字符这一事实。
PHP 加密是通过以下方式执行的: (mcrypt_cbc(MCRYPT_RIJNDAEL_128,$key,$date_str,MCRYPT_ENCRYPT,$iv))
两个相关的 Delphi 函数在这里:
function PadWithZeros(const str : string; size : integer) : string;
var
origsize, i : integer;
begin
Result := str;
origsize := Length(Result);
if ((origsize mod size) <> 0) or (origsize = 0) then
begin
SetLength(Result,((origsize div size)+1)*size);
for i := origsize+1 to Length(Result) do
Result[i] := #0;
end;
end;
procedure TfrmMain.btnDecryptClick(Sender: TObject);
var
Cipher : TDCP_rijndael;
Data, Key, IV : string;
begin
// Pad Key and IV with zeros as appropriate
Key := PadWithZeros(boxKey.Text,KeySize);
IV := PadWithZeros(boxIV.Text,BlockSize);
// Decode the Base64 encoded string
Data := Base64DecodeStr(boxCipherTextIn.Text);
// Create the cipher and initialise according to the key length
Cipher := TDCP_rijndael.Create(Self);
if Length(boxKey.Text) <= 16 then
Cipher.Init(Key[1],128,@IV[1])
else if Length(boxKey.Text) <= 24 then
Cipher.Init(Key[1],192,@IV[1])
else
Cipher.Init(Key[1],256,@IV[1]);
// Decrypt the data
Cipher.DecryptCBC(Data[1],Data[1],Length(Data));
// Free the cipher and clear sensitive information
Cipher.Free;
FillChar(Key[1],Length(Key),0);
// Display the result
boxPlainTextOut.Text := Data;
end;
最佳答案
问题与字符编码有关,但并不是因为 DCPCrypt 无法处理 UTF-16。
PHP 字符串是 UTF-8。因此,您将密码('Key')作为 base64 编码字符串从 PHP 传递到 Dephi。奇怪的是,您将解码后的 key 存储在 UTF-16LE 字符串中。更合适的是 rawbytestring 或 TBytes 或 TMemoryStream。 Key 有效载荷的二进制布局现在不同于编码端的布局,因为它被键入为 UTF16-LE(在不正确的 Delphi 术语中,'unicode string' - Microsoft 和 Embarcadero 的不当行为)。
此外,由于显而易见的原因,这行代码在“unicode”(原文如此)编译器中是错误的...
FillChar(Key[1],Length(Key),0);
我是 TurboPower LockBox 3 的作者,我在论坛中遇到的第一个问题是混淆了 ansistring、utf-8 字符串和 utf-16le 字符串。许多人认为 PHP 理解的密码“abc”与 Delphi 2010 中的“abc”是相同的密码。密码库从密码字符串中使用的是字符串编码产生的二进制有效负载,而不是其语义意思是一个字符串。
关于php - 使用 DCPcrypt 的 Delphi 程序在升级到 XE2 后无法从 PHP 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12000124/
我正在使用 DCPcrypt 和 SHA512 来哈希字符串。 我正在使用 Warren Postma https://bitbucket.org/wpostma/dcpcrypt2010 的版本
我正在使用 Delphi 2007 中的 DCPcrypt 库来加密内部应用程序的文本。 我当前正在使用以下代码(不是我的实际 key ): Cipher := TDCP_rijndael.Cre
我有一个当前仅使用 SHA1 对文件进行哈希处理的程序。没有其他选择。它使用 Lazarus 和 Free Pascal 编译器中的 SHA1 散列函数对它们进行散列。 我已经通过使用 DCPCryp
我有 DCPCrypt 包(最新版本),并且正在尝试使用来自 NIST 分发的 AES 已知答案测试 (KAT) 向量的测试值在 Delphi2007 中进行 AES/Rijndael CBC 编码(
我在 PHP (openssl_encrypt/'aes-256-cbc') 中加密文本,然后尝试在 Delphi 7 (DCPCrypt/TDCP_rijndael) 中解密它。 PHP 脚本文件以
我在使用 DCP Crypt 库加密和解密某些信息时遇到问题。我正在使用 Delphi XE 和发布在 PHP to Delphi and back Encryption-Decryption usi
我使用的是Delphi 10.3.2我正在尝试将 DCPCrypt 单元与 firemonkey 框架结合使用来加密字符串。它在 Win32、Win64 和 macOS 32 目标上 100% 工作,
我有一个用 Delphi 2007 开发的应用程序,其中一个值由 PHP 加密并在应用程序中解密。加密算法是 RIJNDAEL 128。当我移动 XE2 并安装最新版本的 DCPcrypt 时,应用程
我是一名优秀的程序员,十分优秀!