I have been using the encryption class of codeigniter (PHP framework) for a while and need to convert these functions in PHP to C#. This is so that my C# application can decrypt the data on my website database and vise versa.
The problem is that I have recently started with C# so don't really know the function names that would do the same as PHP.
If I can convert these 3 functions, I'm sure I would be able to do their opposite 3 functions myself as they use near enough the same functions.
Note: Please don't try to use these functions other than to play around - they are not strong cryptography (in fact, the method used could even be broken before the invention of computers).
/**
* 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);
}
i'll give you small hint. all C-like constructs and operators go as is, other:
String.Length
String.Substring
+
, .= - +=
(byte)c
(char)i
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.