与PHP解密AES密码文件

发布于 2025-01-31 09:59:18 字数 2161 浏览 2 评论 0原文

如何用PHP解密AES文件,该文件已加密C#。

private static void AES_Encrypt(string filePath, string passKey)
    {
        string newFilePath = filePath + "." + "aes";
        //byte[] keyPair = System.Text.Encoding.ASCII.GetBytes(keyRSA);
        byte[] salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        System.IO.FileStream fsCrypt = new System.IO.FileStream(newFilePath, System.IO.FileMode.Create);
        byte[] passBytes = System.Text.Encoding.UTF8.GetBytes(passKey);
        System.Security.Cryptography.RijndaelManaged AES = new System.Security.Cryptography.RijndaelManaged();
        AES.KeySize = 128;
        AES.BlockSize = 128;
        AES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
        var key = new System.Security.Cryptography.Rfc2898DeriveBytes(passBytes, salt, 1);
        AES.Key = key.GetBytes(AES.KeySize / 8);
        AES.IV = key.GetBytes(AES.BlockSize / 8);
        AES.Mode = System.Security.Cryptography.CipherMode.CBC;
        fsCrypt.Write(salt, 0, salt.Length);
        System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(fsCrypt, AES.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
        System.IO.FileStream fsIn = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
        fsIn.CopyTo(cs);
        fsIn.Flush();
        fsIn.Close();
        cs.Flush();
        cs.Close();
        fsCrypt.Close();
    }

我可以用C#解密它,但是当我试图用PHP解密时,我会遇到一些错误。

<?php

需要 dir 。 '/vendor/autoload.php';

使用phpseclib \ crypt \ aes;

$ cipher = new AES('CBC');

$ cipher-&gt; setKey(str_repeat('a',16));

$ cipher-&gt; setiv(str_repeat('b',16));

$ file = file_get_contents(“ file.aes”);

echo $ cipher-&gt; decrypt($ file);

$ encrypteddata = openssl_decrypt($ file,'aes_128_cbc',“ 123123”,0);


当我使用$ Cipher-&gt; Decrypt($ file);它不起作用,但没有出现错误。

当我使用$ encryptedData = openssl_decrypt($ file,'aes_128_cbc',“ 123123”,0);

我得到这个错误。

警告:openssl_decrypt():c:\ openServer \ domains \ panel2 \ aes.php在第14行中的未知密码算法

How can I decrypt aes file with php which is encrypted with c#.

private static void AES_Encrypt(string filePath, string passKey)
    {
        string newFilePath = filePath + "." + "aes";
        //byte[] keyPair = System.Text.Encoding.ASCII.GetBytes(keyRSA);
        byte[] salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        System.IO.FileStream fsCrypt = new System.IO.FileStream(newFilePath, System.IO.FileMode.Create);
        byte[] passBytes = System.Text.Encoding.UTF8.GetBytes(passKey);
        System.Security.Cryptography.RijndaelManaged AES = new System.Security.Cryptography.RijndaelManaged();
        AES.KeySize = 128;
        AES.BlockSize = 128;
        AES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
        var key = new System.Security.Cryptography.Rfc2898DeriveBytes(passBytes, salt, 1);
        AES.Key = key.GetBytes(AES.KeySize / 8);
        AES.IV = key.GetBytes(AES.BlockSize / 8);
        AES.Mode = System.Security.Cryptography.CipherMode.CBC;
        fsCrypt.Write(salt, 0, salt.Length);
        System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(fsCrypt, AES.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
        System.IO.FileStream fsIn = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
        fsIn.CopyTo(cs);
        fsIn.Flush();
        fsIn.Close();
        cs.Flush();
        cs.Close();
        fsCrypt.Close();
    }

I can decrypt it with c# but when I am trying to decrypt it with php I get some errors.

<?php

require DIR . '/vendor/autoload.php';

use phpseclib\Crypt\AES;

$cipher = new AES('cbc');

$cipher->setKey(str_repeat('a', 16));

$cipher->setIV(str_repeat('b', 16));

$file = file_get_contents("file.aes");

echo $cipher->decrypt($file);

$encryptedData = openssl_decrypt($file, 'AES_128_CBC', "123123", 0);


When I am using $cipher->decrypt($file); it does not work but no error shows up.

When I am using $encryptedData = openssl_decrypt($file, 'AES_128_CBC', "123123", 0);

I get this error .

Warning: openssl_decrypt(): Unknown cipher algorithm in C:\OpenServer\domains\panel2\aes.php on line 14

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文