There is someone who had written the algorithm for encryption "Vernam encryption" in php?
Thank you very much.
There is someone who had written the algorithm for encryption "Vernam encryption" in php?
Thank you very much.
i think u know the algorithm, however, here's a short manual: http://world.std.com/~franl/crypto/one-time-pad.html .
It should be quite easy to rewrite the algorithm in php, i think i could do it, probably there are already implementations...
Here the basic steps:
Generate random key (mt_rand())
Go through the string to decrypt and xor one character with the corresponding key char.
Here's a tutorial how to do it in asp http://www.4guysfromrolla.com/webtech/110599-1.2.shtml , i guess u can use this as a starting point if u wanna code the algortihm in php.
hth and feel free to ask further questions
I've already translated the code written with vbscript in php, but this don't work properly, and the result of the decryption is a wrong string. I will want to see the algorithm written in php to compare the code...
so plz post your encryption code and i'll check if i find some error. btw, why do u want to use one-time-pad encryption?? u know it only works for keys that are exactly as long as your input data? U can also have a look at www.php.net/mcrypt , it allows u to use lots of block and stream ciphers - maybe they r not as secure as one-time-pad aka Vernam encryption but they r far more easy to handle...
i don't know if i did it right so guys plz correct me if i'm wrong
here is the code:
$str = 'this is string';
echo "<b>Original Text:</b> $str<br>";
$key = Array();
for($i=0; $i<512; $i++)
$key[$i] = rand()*$i;
$c = Array();
echo "<b>Encrypted String: </b>: ";
for($i=0; $i<strlen($str); $i++)
{
$c[$i] = $key[$x] ^ ord($str[$i]);
$x = $x+1;
if($x>512)
$x=0;
echo chr($c[$i]);
}
echo "<p>";
//decrypt
for($i=0; $i<count($c); $i++)
echo chr($key[$i]^$c[$i]);
most of your code is ok, however i think u reuse the key bits every 512 chars... i think an attacker could exploit this. maybe u should better create a keystream which is as long as the data u wanna encrypt. u just need to modify your for loop a little ($i < strlen($str) ) and remove the $x in the encryption loop, just use $i for all strings.
Moreover, u gotta seed the random number generator usin srand(time()), and use the mt_rand function (www.php.net/mt_rand) which will give u better random values (here u have to use mt_srand to initialize).
hth
give me a couple hours I'll throw one together, just have to translate my c++ code to php.
and here it is