Php Delphi XE2中Aes | Rijndael-128哈希文本解码
我们有php代码:Php Delphi XE2中Aes | Rijndael-128哈希文本解码,php,delphi,encryption,delphi-xe2,rijndael,Php,Delphi,Encryption,Delphi Xe2,Rijndael,我们有php代码: define('myaesKey', 'znwoq8fq0jf2qjve8laper9f'); // 192 bits and 25 ch. function encode($CodeTo) { $Type = 'rijndael-128'; $Mode = 'ecb'; $IV = "1234567890123450"; $Object = mcrypt_module_open($Type, '', $Mo
define('myaesKey', 'znwoq8fq0jf2qjve8laper9f'); // 192 bits and 25 ch.
function encode($CodeTo) {
$Type = 'rijndael-128';
$Mode = 'ecb';
$IV = "1234567890123450";
$Object = mcrypt_module_open($Type, '', $Mode, '');
mcrypt_generic_init($Object , myaesKey, $IV);
$Enc2Code = mcrypt_generic($Object , $CodeTo);
mcrypt_generic_deinit($Object);
mcrypt_module_close($Object);
return bin2hex($secEncCode);
}
$CodeTo的长度是5,CodeTo是英文字母表的可读符号,函数发送如下内容
1e49651ba23801907e1d67c5a7c18e06
aefdc02bbcb8ed8e8209a935aa62be53
我试着用不同的方式解码,其中一种:
const
KeySize = 24; // 32 bytes = 256 bits 24 - 192
BlockSize = 16; // 16 bytes = 128 bits
function Decrypt(AText:AnsiString):String;
var
Cipher : TDCP_rijndael; i:Integer;
Data, Key, IV,NewStr : ansistring;
begin
// Pad Key and IV with zeros as appropriate
Key := PadWithZeros(ansistring('znwoq8fq0jf2qjve8laper9f'),KeySize);
IV := PadWithZeros(ansistring('1234567890123450'),BlockSize);
// Decode the Base64 encoded string
NewStr:='';
for i:=1 to (Length(AText) div 2) do
NewStr:=NewStr+chr(byte(StrToInt('$'+Copy(AText,(i-1)*2+1,2))));
Data := NewStr;
// Create the cipher and initialise according to the key length
Cipher := TDCP_rijndael.Create(nil);
if Length(ansistring('znwoq8fq0jf2qjve8laper9f')) <= 16 then
Cipher.Init(Key[1],128,@IV[1])
else if Length(ansistring('znwoq8fq0jf2qjve8laper9f')) <= 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));
Cipher.DecryptECB(Data[1],Data[1]);
// Free the cipher and clear sensitive information
Cipher.Free;
FillChar(Key[1],Length(Key),0);
// Display the result
result:= Data;
end;
const
KeySize=24;//32字节=256位24-192
块大小=16;//16字节=128位
函数解密(AText:AnsiString):字符串;
变量
密码:TDCP_rijndael;i:整数;
数据,键,IV,新闻TR:ansistring;
开始
//根据需要,将键盘和IV用零填充
键:=带零的焊盘(ansistring('znwoq8fq0jf2qjve8laper9f'),键大小);
IV:=带零的焊盘(ansistring('12345678901233450'),块大小);
//解码Base64编码的字符串
NewStr:='';
对于i:=1到(长度(AText)div 2)do
NewStr:=NewStr+chr(字节(stroint('$')+拷贝(AText,(i-1)*2+1,2));
数据:=NewStr;
//创建密码并根据密钥长度初始化
密码:=TDCP_rijndael.Create(nil);
如果长度(ansistring('znwoq8fq0jf2qjve8laper9f'))请尝试使用此选项
function AESDecrypt(AData, AKey: String): string;
var
KeyByte,Data,Dest:TBytes;
KeyBlock:integer;
Cipher:TDCP_rijndael;
begin
KeyByte:=TEncoding.UTF8.GetBytes(AKey);
while (Length(KeyByte) mod 16 <> 0) do begin
SetLength(KeyByte,Length(KeyByte)+1);
KeyByte[Length(KeyByte)-1]:=0;
end;
SetLength(Data,Length(AData) div 2);
SetLEngth(Dest,Length(AData) div 2);
Data:=GetBytesFromHex(AData);
Cipher:= TDCP_rijndael.Create(nil);
KeyBlock:=192; //by PHP code comment
Cipher.Init(KeyByte[0],KeyBlock,nil); //for ECB method IV is optional
try
for i := 1 to (Length(AData) div 16) do
begin
Cipher.DecryptECB(Data[(i-1)*16],Dest[(i-1)*16]);
end;
finally
Cipher.Burn;
end;
AData:=TEncoding.UTF8.GetString(Dest);
Result:=AData;
end;
函数aesdepcrypt(AData,AKey:String):String;
变量
KeyByte,Data,Dest:TBytes;
键块:整数;
密码:TDCP_rijndael;
开始
KeyByte:=TEncoding.UTF8.GetBytes(AKey);
而(长度(KeyByte)mod 16 0)不开始
设置长度(KeyByte,长度(KeyByte)+1);
KeyByte[长度(KeyByte)-1]:=0;
结束;
设置长度(数据,长度(AData)第2部分);
设置长度(目的地、长度(数据)第2部分);
数据:=GetBytesFromHex(AData);
密码:=TDCP_rijndael.Create(nil);
键块:=192//通过PHP代码注释
Cipher.Init(KeyByte[0],KeyBlock,nil)//对于ECB,方法IV是可选的
尝试
对于i:=1到(长度(AData)div 16)do
开始
密码解密ECB(数据[(i-1)*16],目的地[(i-1)*16]);
结束;
最后
燃烧;
结束;
AData:=TEncoding.UTF8.GetString(Dest);
结果:=AData;
结束;
可能的副本您必须注意在两侧使用相同的编码,经过编辑。这两种方法都不成功(AFAIK PHP使用UTF8编码字符串。因此,您使用UTF8密码对一些UTF8字符串进行编码,并希望使用ANSI密码对其进行解码…这只是编码问题,其他任何东西EUTF-8都没有帮助。您可以在此处看到代码