简体   繁体   English

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

[英]Replicate C# encryption/decryption in PHP

I have been given a set of codes from a third party that need encrypting/decrypting however the sample encryption code they gave me was in C# and I am primarily a front-end PHP developer. 我从第三方获得了一组需要加密/解密的代码,但是他们给我的样本加密代码是在C#中,我主要是一名前端PHP开发人员。

I have set-up a slimmed down working example of the code I was provided here using the sample key of A818163DD5E0DE87. 我已经使用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;
}

I have scoured stack overflow and other sites in an attempt to replicate this in PHP but nothing comes close to the correct output. 我已经搜索了堆栈溢出和其他站点,试图在PHP中复制它,但没有任何东西接近正确的输出。 I'm also confused by which cipher I am meant to be using and how to convert the key and iv to match the C# example. 我也很困惑我打算使用哪种密码以及如何转换密钥和iv来匹配C#示例。 Below is what I have attempted so far. 以下是我到目前为止所尝试的内容。

$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);

Any help would be appreciated. 任何帮助,将不胜感激。

Things you need to consider: 你需要考虑的事情:

  • DESCryptoServiceProvider -> mcrypt_module_open('des' DESCryptoServiceProvider - > mcrypt_module_open('des'
  • desCrypto.Mode = CipherMode.CBC; desCrypto.Mode = CipherMode.CBC; -> mcrypt_module_open(...,..., 'cbc', - > mcrypt_module_open(...,...,'cbc',
  • key,iv and the cipher output are "treated" with HexStringToByteArray(), pack('H*) can undo that key,iv和密码输出用HexStringToByteArray()“处理”, pack('H *)可以撤消

So, given the output of the .net fiddle ( 7860D97E56DA6A40 ) that leads to 因此,考虑到导致的.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;

output: GROW06BP 输出: GROW06BP

As I've already mentioned in my comment, you're using the wrong algorithm in your PHP code since it's Rijndael. 正如我在评论中已经提到的那样,你在PHP代码中使用了错误的算法,因为它是Rijndael。 What you should use is MCRYPT_DES . 您应该使用的是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);

This will produce the same output as the C# code 这将产生与C#代码相同的输出

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

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