从C#代码(AES-256解密)在PHP中查找类似代码
我一直在尝试在PHP中使用类似的模式来编写以下c#代码。我花了7天时间,试图在我的epinapi应用程序中实现一个简单的加密通信。问题是,php脚本的响应给了我空白从C#代码(AES-256解密)在PHP中查找类似代码,c#,php,encryption,aes,C#,Php,Encryption,Aes,我一直在尝试在PHP中使用类似的模式来编写以下c#代码。我花了7天时间,试图在我的epinapi应用程序中实现一个简单的加密通信。问题是,php脚本的响应给了我空白 public static string AESDecryptText(string input, string key) { // Get the bytes of the string byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
public static string AESDecryptText(string input, string key)
{
// Get the bytes of the string
byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
keyBytes = SHA256.Create().ComputeHash(keyBytes);
byte[] bytesDecrypted = AESDecrypt(bytesToBeDecrypted, keyBytes);
string result = Encoding.UTF8.GetString(bytesDecrypted);
return result;
}
public static byte[] AESDecrypt(byte[] bytesToBeDecrypted, byte[] keyBytes)
{
byte[] decryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(keyBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
我的PHP代码来自其他线程
function DecryptString($content, $password =
'4J2lh3Lz4q6ACo16VrL1oLDnh3k7G1KaXliUPVPV8o0='){
$password = mb_convert_encoding($password, "utf-16le");
$padding = 32 - (strlen($password) % 32);
$password .= str_repeat("\0", $padding);
$iv = substr($password, 0, 8);
$data = base64_decode($content);
$decrypted = openssl_decrypt($data, 'AES-256-CBC', $password,
OPENSSL_RAW_DATA, $iv);
$decrypted = mb_convert_encoding($decrypted, "utf-8", "utf-16le");
return $decrypted;
}
function decryption(){
$password = "4J2lh3Lz4q6ACo16VrL1oLDnh3k7G1KaXliUPVPV8o0=";
$content = "wKamNpehMEqJQ4NcUueNuXq1PbupsxwEvwcJ0CeI+8Q=";
echo $this->DecryptString($content, $password);
}
每次我打印,显示空白。请在这方面提供帮助在您的C#代码中,您使用SHA256
对密码进行哈希运算,并使用Rfc2898DeriveBytes
创建密钥和IV。PHP类似函数包括(使用SHA256)和
与C#方法相当的PHP:
function AESDecryptText($content, $password){
$password = hash('sha256', $password, true);
$salt = pack("C*",1,2,3,4,5,6,7,8);
$bytes = hash_pbkdf2("sha1", $password, $salt, 1000, 48, true);
$key = substr($bytes, 0, 32);
$iv = substr($bytes, 32, 16);
return openssl_decrypt($content, 'AES-256-CBC', $key, 0, $iv);
}
一些注意事项:
- Salt对于每个密码都应该是唯一的李>
- IV应该是随机的,与密码或密钥无关。
您可以使用创建随机字节李>
- 盐和IV不一定要保密,你可以把它们储存在密文旁边李>
你应该考虑使用HMAC来认证你的密文。
你可以用它来达到这个目的
1)$password
似乎是base64编码的字节,我认为您应该在使用它之前对其进行解码。2) 你应该用这把钥匙。3) 在我的回答中,我使用了mb_convert_编码
来解码c#Unicode编码的字符串,您不需要它。4) 不要从internet复制粘贴代码,希望它能自动工作。5) 函数DecryptString($content,$password='4j2lh3lz4q6aco16vrl1oldnh3k7g1kaxliupv8o0='){/$password=mb_convert_encoding($password,“utf-16le”);$password=base64_decode($password);$padding=32-(strlen($password)%32)$password.=stru repeat(“\0”,$padding);$iv=substr($password,0,16);$data=base64_decode($content);$decrypted=openssl_decrypt($data,'AES-256-CBC',$password,openssl_RAW_data | openssl_ZERO_padding,$iv);var_dump($decrypted);}。。仍然得到不可理解的输出。请帮我@t.m.adamGreat。救命,先生