在PHP中将AES加密转换为C#,可以通过以下步骤实现:
function encrypt($data, $key, $iv) {
$encrypted = openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
return base64_encode($encrypted);
}
$key = 'your_key'; // 替换为你的密钥
$iv = 'your_iv'; // 替换为你的初始向量
$data = 'your_data'; // 替换为你要加密的数据
$encryptedData = encrypt($data, $key, $iv);
echo $encryptedData;
using System;
using System.Security.Cryptography;
using System.Text;
public class AesExample {
public static string Decrypt(string encryptedData, string key, string iv) {
byte[] encryptedBytes = Convert.FromBase64String(encryptedData);
using (AesManaged aes = new AesManaged()) {
aes.Key = Encoding.UTF8.GetBytes(key);
aes.IV = Encoding.UTF8.GetBytes(iv);
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
return Encoding.UTF8.GetString(decryptedBytes);
}
}
public static void Main(string[] args) {
string encryptedData = "your_encrypted_data"; // 替换为PHP中加密后的数据
string key = "your_key"; // 替换为你的密钥
string iv = "your_iv"; // 替换为你的初始向量
string decryptedData = Decrypt(encryptedData, key, iv);
Console.WriteLine(decryptedData);
}
}
以上代码示例了如何在PHP中使用AES加密数据,并在C#中解密。你需要将示例代码中的密钥、初始向量、加密数据等替换为你自己的实际值。
领取专属 10元无门槛券
手把手带您无忧上云