簡體   English   中英

這個PHP加密代碼中使用的函數的C#等價物是什么?

[英]What are the C# equivalents of the functions used in this PHP encryption code?

我一直在使用codeigniter(PHP框架)的加密類,需要將PHP中的這些函數轉換為C#。 這樣我的C#應用​​程序就可以解密我網站數據庫中的數據,反之亦然。

問題是我最近開始使用C#,所以不要真正知道與PHP相同的函數名稱。

如果我可以轉換這3個函數,我相信我能夠自己完成相反的3個函數,因為它們使用的功能相近。

注意: 請不要嘗試使用這些功能而不是玩游戲 - 它們不是強密碼術(事實上,在計算機發明之前,使用的方法甚至可以被破壞)。

/**
 * XOR Encode
 *
 * Takes a plain-text string and key as input and generates an
 * encoded bit-string using XOR
 *
 * @access  private
 * @param   string
 * @param   string
 * @return  string
 */
function _xor_encode($string, $key)
{
    $rand = '';
    while (strlen($rand) < 32)
    {
        $rand .= mt_rand(0, mt_getrandmax());
    }

    $rand = $this->hash($rand);

    $enc = '';
    for ($i = 0; $i < strlen($string); $i++)
    {           
        $enc .= substr($rand, ($i % strlen($rand)), 1).(substr($rand, ($i % strlen($rand)), 1) ^ substr($string, $i, 1));
    }

    return $this->_xor_merge($enc, $key);
}

    /**
 * XOR key + string Combiner
 *
 * Takes a string and key as input and computes the difference using XOR
 *
 * @access  private
 * @param   string
 * @param   string
 * @return  string
 */
function _xor_merge($string, $key)
{
    $hash = $this->hash($key);
    $str = '';
    for ($i = 0; $i < strlen($string); $i++)
    {
        $str .= substr($string, $i, 1) ^ substr($hash, ($i % strlen($hash)), 1);
    }

    return $str;
}

/**
 * Adds permuted noise to the IV + encrypted data to protect
 * against Man-in-the-middle attacks on CBC mode ciphers
 * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV
 *
 * Function description
 *
 * @access  private
 * @param   string
 * @param   string
 * @return  string
 */
function _add_cipher_noise($data, $key)
{
    $keyhash = $this->hash($key);
    $keylen = strlen($keyhash);
    $str = '';

    for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
    {
        if ($j >= $keylen)
        {
            $j = 0;
        }

        $str .= chr((ord($data[$i]) + ord($keyhash[$j])) % 256);
    }

    return $str;
}


/**
 * Hash encode a string
 *
 * @access  public
 * @param   string
 * @return  string
 */ 
function hash($str)
{
    return ($this->_hash_type == 'sha1') ? sha1($str) : md5($str);
}

我會給你一些小提示。 所有類似C的構造和運算符都按原樣,其他:

  • strlen - String.Length
  • substr - String.Substring
  • - + ,。= - +=
  • chr(c) - (byte)c
  • ord(i) - (char)i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM