繁体   English   中英

如何使用我在 c# 上加密的 PHP 解密字节数组? 如以下代码所示

[英]How to decrypt byte array using PHP which i has encrypted on c# ? As show on below code

public string Encrypt(string text, string pass)
    {
        byte[] hash = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(pass));
        byte[] iv = new byte[16];
        return this.EncryptString(text, hash, iv);
    }

    public string EncryptString(string plainText, byte[] key, byte[] iv)
    {
        Aes aes = Aes.Create();
        aes.Mode = CipherMode.CBC;
        byte[] numArray = new byte[32];
        Array.Copy(key, 0, numArray, 0, 32);
        aes.Key = numArray;
        aes.IV = iv;
        MemoryStream memoryStream = new MemoryStream();
        ICryptoTransform encryptor = aes.CreateEncryptor();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
        byte[] bytes = Encoding.UTF8.GetBytes(plainText);
        cryptoStream.Write(bytes, 0, bytes.Length);
        cryptoStream.FlushFinalBlock();
        byte[] array = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close();
        return Convert.ToBase64String(array, 0, array.Length);
    }

    public string DecryptString(string cipherText, byte[] key, byte[] iv)
    {
        Aes aes = Aes.Create();
        aes.Mode = CipherMode.CBC;
        byte[] numArray = new byte[32];
        Array.Copy(key, 0, numArray, 0, 32);
        aes.Key = numArray;
        aes.IV = iv;
        MemoryStream memoryStream = new MemoryStream();
        ICryptoTransform decryptor = aes.CreateDecryptor();
        CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write);
        string empty = string.Empty;
        try
        {
            byte[] buffer = Convert.FromBase64String(cipherText);
            cryptoStream.Write(buffer, 0, buffer.Length);
            cryptoStream.FlushFinalBlock();
            byte[] array = memoryStream.ToArray();
            return Encoding.UTF8.GetString(array, 0, array.Length);
        }
        finally
        {
            memoryStream.Close();
            cryptoStream.Close();
        }
    }

这些是我创建的用于在 c# 上加密和解密的 function 但是如何使用 PHP 解密加密数据? 我经历过:

https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-a-php-string/

https://www.php.net/manual/en/function.openssl-encrypt.php

 private void btnEncrypt_Click(object sender, EventArgs e)
    {
        string text = richTextBoxEncrypt.Text;
        string pass = "DecyptionPass";
        richTextBoxEncrypt.Text= Encrypt(text, pass);
    }

    private void btnDecrypt_Click(object sender, EventArgs e)
    {
        byte[] hash = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes("DecyptionPass"));
        byte[] iv = new byte[16];
        richTextBoxDecrypt.Text = DecryptString(richTextBoxDecrypt.Text, hash, iv);
    }

这是使用的纯文本、加密文本和 IV 的示例

plaintext = "This is what i encrypted." 
Encrypted Text = "COz8V8eqkCS2U9TrH6FDA4ZD648m1WSB7QUoDoGyr7k="
Key = "ivdecrypt" 

更新:这是我正在尝试的 PHP 代码,但我不知道 IV 的实际值

<?php

// Store a string into the variable which
// need to be Encrypted
$simple_string = "This is what i encrypted.";

// Display the original string
echo "Original String: " . $simple_string;

// Store the cipher method
$ciphering = "AES-128-CBC";

// Use OpenSSl Encryption method
$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;

// Non-NULL Initialization Vector for encryption
$encryption_iv = 'I dont know';

// Store the encryption key
$encryption_key = "ivdecrypt";

// Use openssl_encrypt() function to encrypt the data
$encryption = openssl_encrypt($simple_string, $ciphering,
    $encryption_key, $options, $encryption_iv);

// Display the encrypted string
echo "Encrypted String: " . $encryption . "\n";

// Non-NULL Initialization Vector for decryption
$decryption_iv = 'I dont know';

// Store the decryption key
$decryption_key = "ivdecrypt";

// Use openssl_decrypt() function to decrypt the data
$decryption=openssl_decrypt ($encryption, $ciphering,
    $decryption_key, $options, $decryption_iv);

// Display the decrypted string
echo "Decrypted String: " . $decryption;

?>

我编写了一个示例程序来展示如何在 PHP 中进行加密和解密,结果(加密字符串)与 C# 相似。

在您的 PHP 源中,您使用的是 AES 128 CBC,但 C# 使用的是 AES 256 CBC。 您使用字符串“我不知道”作为初始化向量 (IV) 的源,但在 C# 中,它是一个 16 长度字节数组,其中填充了 16 个“x00”。

结果如下:

pass:ivdecrypt
password:7b39fbd3f7a82a38dc565a10c236267a977e19fb0ba063f1e882cb6faaadf16f
plaintext=This is what i encrypted.
cipher=aes-256-cbc
encrypted to: COz8V8eqkCS2U9TrH6FDA4ZD648m1WSB7QUoDoGyr7k=
enc expected: COz8V8eqkCS2U9TrH6FDA4ZD648m1WSB7QUoDoGyr7k=
decrypted to: This is what i encrypted.

和代码。 请注意,没有适当的错误处理。 加密警告:static IV 的使用是不安全的。

<?php
// https://stackoverflow.com/questions/63033173/how-to-decrypt-byte-array-using-php-which-i-has-encrypted-on-c-sharp-as-show-o
// How to decrypt byte array using PHP which i has encrypted on c# ? As show on below code
$plaintext = 'This is what i encrypted.';
$pass = 'ivdecrypt';
$method = 'aes-256-cbc';

// Must be exact 32 chars (256 bit)
$password = substr(hash('sha256', $pass, true), 0, 32);
echo "pass:" . $pass . "\n";
echo "password:" . bin2hex($password) . "\n";

// IV must be exact 16 chars (128 bit)
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);

// encryption
$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $password, OPENSSL_RAW_DATA, $iv));
// decryption
$decrypted = openssl_decrypt(base64_decode($encrypted), $method, $password, OPENSSL_RAW_DATA, $iv);

// output
echo 'plaintext: ' . $plaintext . "\n";
echo 'cipher: ' . $method . "\n";
echo 'encrypted to: ' . $encrypted . "\n";
echo 'enc expected: ' . "COz8V8eqkCS2U9TrH6FDA4ZD648m1WSB7QUoDoGyr7k=" . "\n";
echo 'decrypted to: ' . $decrypted . "\n\n";
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM