2
我试图在PHP中实现加密和解密函数,但它并不仅仅是正确的。这里是算法:使用PHP加密和解密
- 加密密钥是一个主代码:
1234567890
。 - 加密字母表是
a-z, A-Z, 0-9, =
- 加密属于加密字母表的签名后的每个符号。跳过其他人。
- 符号通过加密字母表中的移位(向右)进行加密。移位量由加密密钥的当前数字给出。加密密钥的当前数字在每次使用后向右移动,并从结束循环到开始。
这里是我当前的代码:
function tnsencrypt($master_code,$text) { //the text to be encrypted $plain_text= $text;
//letters of alphabet array $alphabet=array('0','1','2','3','4','5','6','7','8','9','=','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
//$alphabet_len = count($alphabet); //$signature_len = 17; // signature=TnpMsgE //$master_code_len = 10; $mcursor = 0;
//positions of the letters in alphabet : The array_flip() function returns an array with all the original keys as values, and all original values as keys. $flip=array_flip($alphabet); //plaintext array $plain_text=str_split($plain_text); $master_code=str_split($master_code);
$n=count($plain_text); $encrypted_text=''; for ($i=0; $i }
}
//echo $encrypted_text; return $encrypted_text; }
function tnsdecrypt($master_code,$text) { //the text to be decrypted $encrypted_text= $text;
//letters of alphabet array $alphabet=array('0','1','2','3','4','5','6','7','8','9','=','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); //positions of the letters in alphabet : The array_flip() function returns an array with all the original keys as values, and all original values as keys. $flip=array_flip($alphabet);
//plaintext array $encrypted_text=str_split($encrypted_text); $master_code=str_split($master_code);
$n=count($encrypted_text); $decrypted_text=''; for ($i=0; $i
} else {
$decrypted_text.= $encrypted_text[$i];
}
//move mcursor
$mcursor = ($mcursor+1)%10;
}
//echo $encrypted_text; return $decrypted_text;
}
if(isset($_POST["text"])) { $text = $_POST["text"]; $shifttext = $_POST["shifttext"]; echo "
Encrypted Text: ".tnsencrypt($shifttext,$text); echo "
Decrypted Text: ".tnsdecrypt($shifttext,tnsencrypt($shifttext,$text));
}
?>
这里是我的结果:
原始文本:val1=1234567 val2=abcdef val3=ABCDEF
加密文本:vjt8F666666F 3hr7Dddddnn 2gq7CCCCMMM
加密文本:vjt8F666666F hr7Dddddnn gq7CCCCMMM
解密文本:val1=1234567 al2=abcdef al3=ABCDEF
正如你可以看到,几个字符缺失的加密和影响解密也是如此。缺少的是第2行上的v和第3行上的v
任何想法为什么?
这是为了好玩还是为了真实?它的加密输出看起来应该是随机的。 – drew010
如果是为了学习/好玩,然后继续。如果它用于生产用途,那么您绝对不应该使用您自己的加密算法。许多现有的经过战斗测试的算法的使用。 – xbonez