简体   繁体   English

没有IV的Crypt_AES phpseclib等效C#

[英]Crypt_AES phpseclib equivalent C# without IV

I need to integrate an xml service to our application and the web service is using phpseclib AES Encryption and Decryption. 我需要将xml服务集成到我们的应用程序中,并且Web服务正在使用phpseclib AES加密和解密。 So, I need to understand the flow of phpseclib. 因此,我需要了解phpseclib的流程。 We are working on .NET C# environment and we can not analyse that php code. 我们正在.NET C#环境中工作,我们无法分析该php代码。 Please find the sample php code for the implementation and advice me how to convert it to C#. 请找到实现示例的php代码,并建议我如何将其转换为C#。

set_include_path('vendor/pear'); 
require_once '/Crypt/AES.php';
require_once '/SOAP/Client.php'; 

$password = 'ewad3x45efc542b3897e23esgy4s6xnm';
$data = '  
<WorkRequest version="3.0"> 
 <work>WORK NOW!</work>
</WorkRequest> '; 

$crypt = new Crypt_AES();      
$crypt->setKey($password);  
$data = gzencode($data);
$data = $crypt->encrypt($data);

As you can see, there is no IV or such things. 如您所见,没有IV或类似的东西。 Only setKey and encrypt methods are used in example code. 示例代码中仅使用setKey和crypto方法。

phpseclib uses CBC mode by default. phpseclib默认使用CBC模式。 That mode requires an IV. 该模式需要IV。 As some of the other comments have noted, the version of phpseclib that you're using uses an IV of all null bytes when none is present. 正如其他一些注释所指出的那样,当不存在任何NULL字节时,所使用的phpseclib版本将使用所有空字节的IV。

$crypt->setKey($password); 

The fact that the variable name is $password does make me wonder if some sort of PBKDF is being used in your C# implementation. 变量名称为$password的事实确实使我想知道您的C#实现中是否使用了某种PBKDF。 It's hard to tell because you don't have your C# code posted. 很难说,因为您没有发布C#代码。

I am already getting "Padding is invalid and cannot be removed." 我已经收到“填充无效,无法删除”。 error when I try to decrypt an encrypted string 尝试解密加密的字符串时出现错误

phpseclib uses PKCS8 padding by default. phpseclib默认使用PKCS8填充。 ie. 即。 if the plaintext is a multiple of the block length it'll append 16x 0x16 bytes. 如果明文是块长度的倍数,它将附加16x 0x16字节。 If it's two bytes shy of a multiple of the block length it'll append 2x 0x02 bytes. 如果它比块长度的倍数少两个字节,它将附加2x 0x02字节。

Maybe your C# implementation is using a different type of padding. 也许您的C#实现使用的是其他类型的填充。 If so you can implement your own by first disabling PKCS8 padding by doing $crypt->disablePadding(); 如果是这样,您可以通过执行$crypt->disablePadding();首先禁用PKCS8填充,从而实现自己的功能$crypt->disablePadding(); and then applying your own custom padding before you perform the encryption. 然后在执行加密之前应用您自己的自定义填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM