在PHP中复制C#加密/解密

时间:2016-05-26 07:17:07

标签: c# php encryption

我从第三方获得了一组需要加密/解密的代码,但是他们给我的样本加密代码是在C#中,我主要是一名前端PHP开发人员。

我已经为我提供的代码设置了一个精简的工作示例 here使用A818163DD5E0DE87的示例键。

public static byte[] HexStringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2) {
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return bytes;
}

// Convers a byte array to a HEX string
public static string ByteArrayToHexString(byte[] bytes)
{
StringBuilder hexString = new StringBuilder(bytes.Length * 2);
for (int i = 0; i < bytes.Length; i++)
{
hexString.Append(bytes[i].ToString("X2"));
}
return hexString.ToString();
}

public static byte[] Encrypt()
{
string plainText = "GROW06BP";
DESCryptoServiceProvider desCrypto = new DESCryptoServiceProvider();
desCrypto.Key = HexStringToByteArray("A818163DD5E0DE87");
desCrypto.IV = HexStringToByteArray("A818163DD5E0DE87");
desCrypto.Mode = CipherMode.CBC;
desCrypto.Padding = PaddingMode.Zeros;
// Create a buffer for the Plain Text using ASCIIEncoding
byte[] plaintextBytes = (new ASCIIEncoding()).GetBytes(plainText);
// Create a memory stream for the encrypted bytes
MemoryStream msEncrypt = new MemoryStream();
// Create a CryptoStream using the memory stream and the passed Algorithm
CryptoStream csEncrypt = new CryptoStream(msEncrypt, desCrypto.CreateEncryptor(), CryptoStreamMode.Write);
// Write the plaintext to the CryptoStream
csEncrypt.Write(plaintextBytes, 0, plaintextBytes.Length);
// Close the CryptoStream
csEncrypt.Close();
// Read the Encrypted bytes into our buffer
byte[] encryptedTextBytes = msEncrypt.ToArray();
// Close the Memory Stream
msEncrypt.Close();
// And return the encrypted buffer
return encryptedTextBytes;
}

我已经搜索了堆栈溢出和其他站点,试图在PHP中复制它,但没有任何东西接近正确的输出。我也很困惑我打算使用哪种密码以及如何转换密钥和iv来匹配C#示例。以下是我到目前为止的尝试。

$key = unpack('H*', "A818163DD5E0DE87");
$key = "A818163DD5E0DE87";
$iv = $key;
$plaintext = "GROW06BP";
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext,MCRYPT_MODE_CBC, $iv);
echo base64_encode($ciphertext);

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

您需要考虑的事项:

  • DESCryptoServiceProvider - &gt; mcrypt_module_open('des'
  • desCrypto.Mode = CipherMode.CBC; - &GT; mcrypt_module_open(...,...,'cbc',
  • key,iv和密码输出被HexStringToByteArray()“处理”,pack('H*)可以撤消

因此,给出.net小提琴(7860D97E56DA6A40)的输出导致

<?php
$msgHex = '7860D97E56DA6A40'; 
$keyHex = 'A818163DD5E0DE87';
$ivHex = 'A818163DD5E0DE87';  // really? invalidates the use-case of an iv :-/

// this reverts the effect of HexStringToByteArray() 
$msg = pack('H*', $msgHex);
$key = pack('H*', $keyHex);
$iv = pack('H*', $ivHex);

// add error handing !
$module = mcrypt_module_open('des', '', 'cbc', '');
mcrypt_generic_init($module, $key, $iv);
$plaintext = mdecrypt_generic($module, $msg);
mcrypt_generic_deinit($module);

echo $plaintext;

输出:GROW06BP

答案 1 :(得分:0)

正如我在评论中已经提到的那样,您在PHP代码中使用了错误的算法,因为它是Rijndael。您应该使用的是MCRYPT_DES

$key = "A818163DD5E0DE87";
// Here you need pack instead of unpack
$packKey = pack("H*",$key);
// you should use the key as the initialization vector
// use something like mcrypt_create_iv to generate an IV
$iv = $packKey;
$plaintext = "GROW06BP";
// replaced MCRYPT_RIJNDAEL_128 with MCRYPT_DES
$ciphertext = mcrypt_encrypt(MCRYPT_DES, $packKey, $plaintext,MCRYPT_MODE_CBC, $iv);
echo base64_encode($ciphertext);

这将产生与C#代码相同的输出