C#到PHP AES解密
嗨,我有c#代码示例,但我无法将其转换为php。 İ试图重写代码,但我做不到。 在我的项目中,另一台服务器使用c#加密数据,我必须使用PHP对其进行解密 我有密码和salt值 这里是C#代码,包含加密和解密功能C#到PHP AES解密,php,encryption,aes,rfc2898,Php,Encryption,Aes,Rfc2898,嗨,我有c#代码示例,但我无法将其转换为php。 İ试图重写代码,但我做不到。 在我的项目中,另一台服务器使用c#加密数据,我必须使用PHP对其进行解密 我有密码和salt值 这里是C#代码,包含加密和解密功能 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO; na
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace EncryptionSample
{
public static class CipherUtility
{
public static string Encrypt(string plainText, string password, string salt)
{
if (plainText == null || plainText.Length <= 0)
{
throw new ArgumentNullException("plainText");
}
if (String.IsNullOrEmpty(password))
{
throw new ArgumentNullException("password");
}
if (String.IsNullOrEmpty(salt))
{
throw new ArgumentNullException("salt");
}
byte[] encrypted;
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
using (Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes(password, saltBytes))
{
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.Key = derivedBytes.GetBytes(32);
aesAlg.IV = derivedBytes.GetBytes(16);
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
}
return Convert.ToBase64String(encrypted);
}
public static string Decrypt(string cipherValue, string password, string salt)
{
byte[] cipherText = Convert.FromBase64String(cipherValue);
if (cipherText == null
|| cipherText.Length <= 0)
{
throw new ArgumentNullException("cipherValue");
}
if (String.IsNullOrWhiteSpace(password))
{
throw new ArgumentNullException("password");
}
if (String.IsNullOrWhiteSpace(password))
{
throw new ArgumentNullException("salt");
}
string plaintext = null;
byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
using (Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, saltBytes))
{
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.Key = deriveBytes.GetBytes(32);
aesAlg.IV = deriveBytes.GetBytes(16);
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
}
return plaintext;
}
}
}
我不是密码专家,但我认为您可能会发现有用。我不是密码专家,但我认为您可能会发现有用。乍一看,您的密钥将有所不同。您的C#代码使用
Rfc2898DeriveBytes
生成密钥,这是一个基于PBKDF2的密钥生成器。另一方面,您的php代码使用SHA256生成密钥。它们将返回不同的值。使用不同的键,您甚至可以在开始之前完成
另外,我不知道CryptoStream
将在密文的开头附加IV,也不知道在密文的结尾附加MAC值。如果你的明文能够解密的话,去掉这些文本会使它变得乱码。请注意,在C#decryption方法中,您根据密钥派生对象派生IV(这不是智能的,因为相同的密钥将为每条消息生成相同的IV,这会降低密文第一块的安全性,但这是一个完全独立的问题)
您知道C#服务器生成的密文与您的代码示例完全相同吗?您需要知道服务器端使用的加密的确切参数
我建议您实际研究和理解C#将要发出的密文的格式,然后找出如何在PHP中使用它。密码学可能非常棘手,尤其是在尝试集成异构系统时。乍一看,我可以看出您的密钥将有所不同。您的C#代码使用
Rfc2898DeriveBytes
生成密钥,这是一个基于PBKDF2的密钥生成器。另一方面,您的php代码使用SHA256生成密钥。它们将返回不同的值。使用不同的键,您甚至可以在开始之前完成
另外,我不知道CryptoStream
将在密文的开头附加IV,也不知道在密文的结尾附加MAC值。如果你的明文能够解密的话,去掉这些文本会使它变得乱码。请注意,在C#decryption方法中,您根据密钥派生对象派生IV(这不是智能的,因为相同的密钥将为每条消息生成相同的IV,这会降低密文第一块的安全性,但这是一个完全独立的问题)
您知道C#服务器生成的密文与您的代码示例完全相同吗?您需要知道服务器端使用的加密的确切参数
我建议您实际研究和理解C#将要发出的密文的格式,然后找出如何在PHP中使用它。密码学可能非常棘手,尤其是在尝试集成异构系统时。您的php代码在哪里?我也添加了我的php代码。哈希-然后-加密不是安全的MAC。在加密然后mac方案中使用HMAC。密码应该安全地(缓慢地)散列,而不是加密,当然也不能使用MD5/SHA2。你的php代码在哪里?我也添加了我的php代码。散列然后加密不是安全的mac。在先加密后mac的方案中使用HMAC。密码应该安全地(缓慢地)散列,而不是加密,当然也不能使用MD5/SHA2
function decrypt($encrypted, $password, $salt) {
// Build a 256-bit $key which is a SHA256 hash of $salt and $password.
$key = hash('SHA256', $salt . $password, true);
// Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
$iv = base64_decode(substr($encrypted, 0, 22) . '==');
// print_r($iv);die();
// Remove $iv from $encrypted.
$encrypted = substr($encrypted, 22);
//print_r($encrypted);die();
// Decrypt the data. rtrim won't corrupt the data because the last 32 characters are the md5 hash; thus any \0 character has to be padding.
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");
// Retrieve $hash which is the last 32 characters of $decrypted.
$hash = substr($decrypted, -32);
// Remove the last 32 characters from $decrypted.
$decrypted = substr($decrypted, 0, -32);
// Integrity check. If this fails, either the data is corrupted, or the password/salt was incorrect.
if (md5($decrypted) != $hash) return false;
return $decrypted;
}