【发布时间】:2021-10-10 21:17:25
【问题描述】:
这是我用于 openssl_encrypt 的 php:
$text = "does this work";
$key = "1234567890123456789012345678901234567890123456789012345678901234";
$method = "AES-256-CBC";
$cipher_length = openssl_cipher_iv_length($method); //16
$iv_new = openssl_random_pseudo_bytes($cipher_length);
$bin2hex = bin2hex($iv_new); //"a64d63874ba9ee8f5a5028cb40ab70a4"
$openssl = openssl_encrypt($text, $method, $key, 0, $iv_new);
$encrypted = $bin2hex . $openssl;
$encrypted 的值为:
"a64d63874ba9ee8f5a5028cb40ab70a4yFFQwLuOHeWouyfp0dyrnw==" //encrypted in DecryptPHP
这是我用于 openssl_decrypt 的 php:
$iv_strlen = 32;
preg_match("/^(.{" . $iv_strlen . "})(.+)$/", $encrypted, $regs);
list(, $_iv, $_crypted_string) = $regs;
$_hex2bin = hex2bin($_iv);
$ctype_xdigit = ctype_xdigit($_iv);
$remainder = strlen($_iv) % 2 == 0;
$new_text = openssl_decrypt($_crypted_string, $method, $key, 0, $_hex2bin);
$new_text 值为:
"does this work"
所以这很好,但我怎样才能得到这个字符串:
"a64d63874ba9ee8f5a5028cb40ab70a4yFFQwLuOHeWouyfp0dyrnw=="
并在 C# 中将其转换为“这行得通”吗?我已经尝试了很多事情并不断提出不良数据。我什至没有运气尝试过这个。任何帮助将不胜感激。
Decrypt string in C# that was encrypted with PHP openssl_encrypt
这是我的 C# 代码:
string DecryptPHP()
{
//var _key = System.Text.Encoding.UTF8.GetBytes(key);
//var _iv = System.Text.Encoding.UTF8.GetBytes(iv);
var key = key = "1234567890123456789012345678901234567890123456789012345678901234";
var _key = FromHex(key);
var iv = "a64d63874ba9ee8f5a5028cb40ab70a4";
var _iv = FromHex(iv);
//pad key out to 32 bytes (256bits) if its too short
if (_key.Length < 32)
{
var paddedkey = new byte[32];
Buffer.BlockCopy(_key, 0, paddedkey, 0, key.Length);
_key = paddedkey;
}
//get the encrypted data and decrypt
var data_encrypted_string = "yFFQwLuOHeWouyfp0dyrnw==";
byte[] encryptedBytes = Convert.FromBase64String(data_encrypted_string);
return DecryptStringFromBytesAes(encryptedBytes, _key, _iv);
}
byte[] FromHex(string hex)
{
hex = hex.Replace("-", "");
byte[] raw = new byte[hex.Length / 2];
for (int i = 0; i < raw.Length; i++)
{
raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return raw;
}
static string DecryptStringFromBytesAes(byte[] cipherText, byte[] key, byte[] iv)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("iv");
// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;
// Declare the string used to hold
// the decrypted text.
string plaintext;
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.None, KeySize = 256, BlockSize = 128, Key = key, IV = iv };
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
srDecrypt.Close();
}
}
}
return plaintext;
}
【问题讨论】:
-
你能发布你在 C# 中尝试过的内容吗?
-
我添加了我所拥有的。明文总是返回乱码
标签: c# php encryption openssl