1

我有以下 C# 代码,但我很难为 PHP mcrypt_decrypt() 添加参数。

  private string Decrypt(string strText, string sDecrKey)
    {
        string message;
        byte[] rgbKey = new byte[0];
        byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
        byte[] buffer = new byte[strText.Length + 1];
        try
        {
            rgbKey = Encoding.UTF8.GetBytes(Strings.Left(sDecrKey, 8));
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            buffer = Convert.FromBase64String(strText);
            MemoryStream stream2 = new MemoryStream();
            CryptoStream stream = new CryptoStream(stream2, provider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            stream.Write(buffer, 0, buffer.Length);
            stream.FlushFinalBlock();
            message = Encoding.UTF8.GetString(stream2.ToArray());
        }
        catch (Exception exception1)
        {
            ProjectData.SetProjectError(exception1);
            Exception exception = exception1;
            message = exception.Message;
            ProjectData.ClearProjectError();
            return message;
            ProjectData.ClearProjectError();
        }
        return message;
    }

字符串 mcrypt_decrypt ( 字符串 $cipher , 字符串 $key , 字符串 $data , 字符串 $mode [, 字符串 $iv ] )

我的数据是-:"iHcKDE0aeDZU3q31ULZcvQ=="

钥匙-: ”&%#@?,:*”

我想使用 PHP mcrypt_decrypt () 函数解密这些数据。我对 C# 的了解很少,但 PHP 很好。请帮帮我

<?php
$key = pack("nvc*",0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef);
$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CFB)
$ciphertext_dec = base64_decode("iHcKDE0aeDZU3q31ULZcvQ==");
$iv_dec = substr($ciphertext_dec, 0, $iv_size);
$retval = mcrypt_decrypt(MCRYPT_3DES, $key, $ciphertext_dec, MCRYPT_MODE_CFB, $iv_dec);
echo $retval;
?>
4

1 回答 1

0

从外观上看,您需要创建一个iv,而不是substr$keysubstr的密钥上运行mcrypt_get_key_size(cipher, mode),这似乎您正在使用iv.

$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CFB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

并且iv应该来自随机来源,而不是$key削减。虽然,iv是可选的。这是我能看到的唯一问题。

这是旧的,但是当我写它时它对我来说效果很好。密码.php

<?php
function encrypt($data, $key = null, $iv = null) {
    $key = (strlen($key) == 0) ? $key = null : $key;

    $key_size = mcrypt_get_key_size(MCRYPT_3DES, MCRYPT_MODE_CFB); 
    $key = hash("sha256", $key, true); 
    $key = substr($this->key, 0, $key_size);

    if ($iv == null) {
        $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CFB); 
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    }

    $out = mcrypt_encrypt(MCRYPT_3DES, $key, $data, MCRYPT_MODE_CFB, $iv); 
    return base64_encode($out);
}

function decrypt($data, $key = null, $iv = null) {
    $key = (strlen($key) == 0) ? $key = null : $key;

    $key_size = mcrypt_get_key_size(MCRYPT_3DES, MCRYPT_MODE_CFB); 
    $key = hash("sha256", $key, true); 
    $key = substr($this->key, 0, $key_size);

    if ($iv == null) {
        $iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CFB); 
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    }

    $data = base64_decode($data); 
    $out = mcrypt_decrypt(MCRYPT_3DES, $key, $data, MCRYPT_MODE_CFB, $iv);
}

$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CFB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

$encrypted = encrypt('my data.', 'my secret key', $iv);
$decrypted = decrypt($encrypted, 'my secret key', $iv);

# $decrypted = 'my data.'
?>

更新

我更新了 Cipher.php以便于交互。您不再需要专门化ivCipher::encrypt设置它并将其附加到加密数据。Cipher::decrypt然后在解密加密数据之前提取它。

IV 仅用于为加密例程提供替代种子。这个 IV 根本不需要保密,尽管它可能是可取的。您甚至可以将它与您的密文一起发送而不会失去安全性。

于 2013-06-24T12:28:30.307 回答