我有一些C#代码用于加密文本,并且使PHP与之等效。 C#方法返回的字符串是 1560 个字节,但是我的PHP方法返回了 2648 个字节,尽管我对AES-使用相同的加密标准,但我不知道为什么256位作为C#App,它们是:
块模式:CBC填充:PKCS7块大小:16字节 加密密钥大小:32个字节初始化向量大小:16个字节
C#代码:
public string AES256Encrypt(string toEncrypt, out string key)
{
try
{
AesManaged AESManaged = new AesManaged();
byte[] AESKey = AESManaged.Key;
byte[] AESIV = AESManaged.IV;
byte[] encryptedData = null;
ICryptoTransform cryptoTransform = AESManaged.CreateEncryptor(AESKey, AESIV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
{
using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(toEncrypt);
}
encryptedData = memoryStream.ToArray();
}
}
byte[] keyBytes = new byte[AESKey.Length + AESIV.Length];
Array.Copy(AESKey, 0, keyBytes, 0, AESKey.Length);
Array.Copy(AESIV, 0, keyBytes, AESKey.Length, AESIV.Length);
key = Convert.ToBase64String(keyBytes);
return Convert.ToBase64String(encryptedData);
}
catch (Exception ex)
{
key = null;
return null;
}
}
我的PHP代码是:
public function AESencrypt($plaintext, &$macKey)
{
$key = random_bytes(32);
//$key = bin2hex(openssl_random_pseudo_bytes(32));
$keyArr = unpack('C*', $key);
$iv = random_bytes(16);
//$iv = bin2hex(openssl_random_pseudo_bytes(16)) ;
$ivArr = unpack('C*', $iv);
$encryptedData = openssl_encrypt(
$plaintext,
'AES-256-CBC',
$key,
OPENSSL_RAW_DATA,
$iv
);
// while ($encryptedData = openssl_error_string())
// echo $encryptedData . "<br />\n";exit;
$keyBytes = array();
//clone the $keyArr to the keyBytes Array
foreach ($keyArr as $key => $item) {
$keyBytes[$key] = $item;
}
//clone the $ivArr to the keyBytes Array from index 32 to 48
foreach ($ivArr as $key => $item) {
$keyBytes[$key+32] = $item;
}
$string = implode(array_map("chr", $keyBytes));
$returned =
array(
'encrypted' => base64_encode($encryptedData),
'randomSecret' => base64_encode($string),
);
//print '<pre>'; print_r($returned);exit;
return $returned;
}
这是传递给两个应用程序进行加密的对象
<?xml version="1.0" encoding="utf-16"?>
<PaymentRequestInitiationReq
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Sender>
<Id>029</Id>
<Name>Biller</Name>
<RandomValue>54b86c4e2df948659b186571a924e516</RandomValue>
<Password>XG94dze10PfZXrfJ7pDnuyR8MBi2SBQq24r6NLjAN0kz3Ay/OoOahqrfHI0n0azg+GkIqvEdK0ddgKip6AAd5TN14MmGL0hgc99/2JFkSrEL8brylG8MxiZIqTKoEb7hOV84V+tHcolbUNux0o1OOGrD4lgrh3BMV/hddwlwb97sXguadOPEdVVCLCEN3qyo15vGtmgTR1dcaWj1Pj3KIEeQvJifA4FjTSqFLEwLDHMnKZEjjkjDmNGc7pYsWxYP8dm6FAiUbYB+G8PsiFhiHsYxNygsYB/dTn2dY7vgV4iBUD9EnNkbb6dUFIZJ0hCYllKWhONWxKwJ4oK3MGlR3g==</Password>
</Sender>
<SettlementAmounts>
<SettlementAmounts>
<SettlementAccountCode>1073</SettlementAccountCode>
<SettlementAmountsDescription />
<Amount>25</Amount>
</SettlementAmounts>
</SettlementAmounts>
<SenderRequestNumber>01a19e11f5bf4349</SenderRequestNumber>
<SenderInvoiceNumber />
<ServiceCode>140</ServiceCode>
<RequestInitiationDescription />
<Currency>818</Currency>
<IP>::1</IP>
<PaymentMechanism>
<Type>NotSet</Type>
<MechanismType>NotSet</MechanismType>
<Channel />
</PaymentMechanism>
<ExpiryDate>2019-07-17</ExpiryDate>
<PaymentConfirmationUrl>http://127.0.0.1/agri360/index.php?route=checkout/success</PaymentConfirmationUrl>
<PaymentConfirmationRedirectUrl>http://127.0.0.1/agri360/index.php?route=checkout/success</PaymentConfirmationRedirectUrl>
<UserUniqueIdentifier>1234456778</UserUniqueIdentifier>
</PaymentRequestInitiationReq>
请有人能帮助我,告诉我为什么会发生这种情况吗?
答案 0 :(得分:0)
最后,我已经解决了。 C#处理的XML对象字符串与PHP不同,因此我将PHP端的字符串放在一行上,“加密对象”的长度也变为相同。