我想将C#函数转换为PHP函数。这里是C#中函数的链接:https://stackoverflow.com/a/19441805/3581428
public static string Encrypt(string clearText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new
Rfc2898DeriveBytes(EncryptionKey, new byte[]
{ 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
return clearText;
}
public static string Decrypt(string cipherText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new
Rfc2898DeriveBytes(EncryptionKey, new byte[]
{ 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
我尝试了下面的函数,但它创建了不同的加密字符串:
function encrypt_decrypt($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = '3sc3RLrpd17';
$secret_iv=chr(0x49).chr(0x76).chr(0x61).chr(0x6e).chr(0x20).chr(0x4d).chr(0x65).chr(0x64).chr(0x76).chr(0x65).chr(0x64).chr(0x65).chr(0x76);
$key = substr(hash('sha1', $secret_key), 0, 32);;
//echo "key: ".$key."<br>";
$iv = substr(hash('sha1', $secret_iv), 0, 16);
//echo "iv key: ".$iv."<br>";
if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
$plain_txt = "1122334411223344";
echo "Plain Text = $plain_txt\n"."<br/>";
$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);
echo "Encrypted Text = $encrypted_txt\n"."<br/>";
$decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);
echo "Decrypted Text = $decrypted_txt\n"."<br/>";
if( $plain_txt === $decrypted_txt ) echo "SUCCESS"."<br/>";
else echo "FAILED"."<br/>";
echo "\n"."<br/>";
exit;
使用C#方法加密字符串:
thkTvpUmSWV9lKAOfWNIIu9n7jHcku7C6WDD/hsvll+xjqOWdk3fyI+eRhBbvJlX
长度为64
使用PHP方法加密的字符串:
OTlIYXJDcTl0SVpKRlhaV0l3dFk1ZjFYM3FPcHB3ckdTRERITGhHVEVoTT0=
长度为60
如何生成与C#函数相同的加密字符串?如何复制C#函数用于PHP?
能够使用以下示例创建长度为64的加密文本链接:AES ENCRYPTION 64 length encrypted string
3条答案
按热度按时间moiiocjp1#
试试这个(beta):
PHP语言
C#
4bbkushb2#
作为@odan给出的,对于那些如何需要PHP的微小变化,openssl_encrypt with AES-128-CBC,key and IV,below PHP code
和C#代码链接在fiddle中也是:https://dotnetfiddle.net/LE8lTx
o75abkj43#
我建议你使用我已经用C#和PHP写过的类似的类:
PHP中的
Cryptography
类这是一个类似的C#类
参考:https://gist.github.com/Oranzh/2520823f9d1cea603e60b8e8f3fe1d36
好好享受...