Reputation: 71
Is there a lib for this kind of crypt/decrypt in python? I am trying to generate encrypted ascii text which changes on each generation.
If no lib for this, would you please advice a working alternative. Already tried to convert this PHP code to python but I failed.
PHP what I use at the moment:
function keyED($txt,$encrypt_key)
{
$ctr=0;
$tmp = "";
$txt_len=strlen($txt);
for ($i=0;$i<$txt_len;$i++)
{
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
$ctr++;
}
return $tmp;
}
function encrypt($txt,$key)
{
srand((double)microtime()*1000000);
$encrypt_key = md5(rand(0,32000));
$ctr = 0;
$tmp = "";
$txt_len = strlen($txt);
for ($i=0;$i < $txt_len;$i++)
{
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
$ctr++;
}
return keyED($tmp,$key);
}
function decrypt($txt,$key)
{
$txt = keyED($txt,$key);
$tmp = "";
$txt_len=strlen($txt);
for ($i=0;$i<$txt_len;$i++)
{
$md5 = substr($txt,$i,1);
$i++;
$tmp.= (substr($txt,$i,1) ^ $md5);
}
return $tmp;
}
$x = encrypt("test", "123");
echo decrypt($x, "123") // -> "test"
Upvotes: 0
Views: 1323
Reputation: 3310
NEVER write your own crypto algorithms! There are enough existing ones which have already been reviewed over and over again.
That said, there is an example how to use the AES algorithm with the Python Crypto module in this answer: https://stackoverflow.com/a/12525165
Upvotes: 1