2010-07-18 82 views
1

我一直在使用codeigniter(PHP框架)的加密類,並需要將這些函數在PHP中轉換爲C#。 這使我的C#應用​​程序可以解密我的網站數據庫上的數據,反之亦然。這個PHP加密代碼中使用的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); 
} 
+3

糟糕!你正在實現自己的加密算法嗎? – Thomas 2010-07-18 17:14:30

+2

這是CodeIgnitEr – quantumSoup 2010-07-18 17:16:26

+3

這樣做是通過將文本視爲二進制數據來處理?雙y! – 2010-07-18 17:16:49

回答

4

我給你小提示。所有的C類結構和運營商走的就是其他:

  • strlen的 - String.Length
  • SUBSTR - String.Substring
  • 。 - +,= - 。+=
  • CHR(C) - (byte)c
  • ORD(ⅰ) - (char)i
+0

感謝您在正確的方向! – arbme 2010-07-18 17:21:59

相關問題