Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中复制C#加密/解密_C#_Php_Encryption - Fatal编程技术网

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

在PHP中复制C#加密/解密,c#,php,encryption,C#,Php,Encryption,我从第三方获得了一组需要加密/解密的代码,但是他们给我的示例加密代码是C#,我主要是前端PHP开发人员 我已经为我提供的代码建立了一个精简的工作示例 使用A818163DD5E0DE87的示例键 public static byte[] HexStringToByteArray(String hex) { int NumberChars = hex.Length; byte[] bytes = new byte[NumberChars / 2]; for (int i = 0; i < N

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

我已经为我提供的代码建立了一个精简的工作示例 使用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;
}

任何帮助都将不胜感激。

您需要考虑的事项:

  • DESCryptoServiceProvider->mcrypt_模块_打开('des'
  • descrypt.Mode=CipherMode.CBC;->mcrypt_模块_打开(…,…,'CBC'
  • 密钥iv和密码输出使用HexStringToByteArray()进行“处理”,可以撤消该操作
因此,给定.net FIDLE(
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;

正如我在评论中提到的,您在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#中使用DES但在PHP中使用Rijndael(AES)的C#代码相同的输出.它们是不同的加密算法!谢谢你的帮助Nasreddine.你的代码对我来说很好,但似乎给C代码输出了一个不同的结果,这正是我对此感到困惑的原因。非常感谢VolkerK,这让我能够成功地解密代码。我不认为我会得到这样的结果“在搜索此文件时,根本不会遇到这些函数。
$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);