本文介绍了PHP加密& VB.net解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力做一个简单的任务。在PHP中使用一个值并在我的VB.net应用程序中解密它。
我想我会使用tripleDES或Rijdael 128或256



我虽然这应该很简单。任何人都可以指向正确的方向?



谢谢

解决方案

我们有一些在.NET和PHP之间的C#工作的密码。我不熟悉VB.net。我假设它使用相同的加密库 System.Security.Cryptography



在PHP方面,我们从mcrypt切换到OpenSSL,因为mcrypt不支持某些模式和填充。



只要您使用相同的算法(DES,AES等),相同的模式(CBC,ECB等),相同的填充(PKCS1,PKCS5),密码应在两个平台上工作。



使用mcrypt在PHP端使用AES-128进行加密的示例

  $ iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC ); 
$ iv = mcrypt_create_iv($ iv_size,MCRYPT_RAND);
$ blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128);
$ data = $ this-> paddingAlgorithm-> padData($ data,$ blockSize);
返回$ iv。 mcrypt_encrypt($ this-> MCRYPT_DES,$ keyBytes,$ data,MCRYPT_MODE_CBC,$ iv);

请注意,我们使用PKCS7填充,但是mcrypt不支持它,所以我们必须写填充算法。我们还将IV(初始向量)添加到密文中。



这里是相应的C#代码设置密码解密,

  //使用给定名称创建底层对称算法
algorithm =(SymmetricAlgorithm)CryptoConfig.CreateFromName(RIJNDAEL);
//设置密码模式
algorithm.Mode = CipherMode.CBC;
//设置填充模式
algorithm.Padding = PaddingMode.PKCS7;


I'm trying to do a simple task. Encypt a value in PHP and Decrypt it in my VB.net app.I figure I'd use tripleDES or Rijdael 128 or 256

I though this should be simple. Can anyone point me in the right direction?

Thank you

解决方案

We have some ciphers working between C# on .NET and PHP. I am not familiar with VB.net. I assume it uses the same crypto library System.Security.Cryptography.

On PHP side, we switched from mcrypt to OpenSSL because some modes and paddings are not supported by mcrypt.

As long as you use same algorithm (DES, AES etc), same mode (CBC, ECB etc), same padding (PKCS1, PKCS5), the cipher should work on both platforms.

Example of encryption using AES-128 on PHP side using mcrypt,

    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128);
    $data = $this->paddingAlgorithm->padData($data, $blockSize);
    return $iv . mcrypt_encrypt($this->MCRYPT_DES, $keyBytes, $data, MCRYPT_MODE_CBC, $iv);

Please note that we use PKCS7 padding but mcrypt doesn't support it so we have to write the padding algorithm. We also prepend the IV (Initial Vector) to the cipher text. You might store it somewhere else but you need that to decrypt.

Here is the corresponding C# code to setup the cipher to decrypt,

    // create the underlying symmetric algorithm with the given name
    algorithm = (SymmetricAlgorithm)CryptoConfig.CreateFromName("RIJNDAEL");
    // set cipher mode
    algorithm.Mode = CipherMode.CBC;
    // set padding mode
    algorithm.Padding = PaddingMode.PKCS7;

这篇关于PHP加密& VB.net解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:47