2

我正在尝试在 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

任何想法为什么?

4

1 回答 1

1

你为什么做这个?如果这是为了实际使用而不仅仅是学术练习,您可以使用几种单向和双向加密解决方案:

单向双向crypt()
mcrypt

加密几乎是一个已解决的问题。

于 2012-09-24T21:22:29.997 回答