0
在python中有這種crypt/decrypt的lib嗎?我正在嘗試生成每一代都會更改的加密ascii文本。Python crypt/decrypt strings
如果沒有lib的這個,你會請建議一個工作的替代品。已經嘗試將此PHP代碼轉換爲Python,但我失敗了。
PHP是我此刻的使用:
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"
你能告訴你試着寫的Python代碼(失敗)? –
你檢查了[docs ...](http://docs.python.org/2/library/crypto.html)嗎? – wflynny