Tuesday 15 March 2011

python - Using caesar cipher to decrypt ciphertext -


I use the code to decrypt ciphertext "htrgti" to plain text I am trying. I am receiving "WigWiki" , which I should not get the message ciphertext I have replaced it with plaintext For plain text (which should be a common word or phrase) and looking at the key, and for every location it is same as plaintext :

  def cesar ( Cybertext, change): Alphabet = ["A", "B", "C" "D", "E", "F", "G", "H", "I", "J", "K" , "L", "m", "n", "o", "p" in the range Plain text = "" for "," q "," q "," r "," s "," t "," u "," v "," w "," x "," y " "(Lin (Ciphertext)): Find the position of letter # numeric number [i] # number of letters. Num_in_alphabet = alphabet.index (letter) # Find the position of the number of ciphers by adding shift = (num_in_alphabet + shift)% LAN (alphabet) # Find plain letters for the weekly number that you calculate plain_letter = alphabet [plain_num] # Add cipher characters in plain text export Ext = plaintext + plain_letter return plain text  

If it says correctly, then your function works @ Lilma indicated with the correct change by Caesar (" HTRGT ", 11) is secret . Your function runs itself correctly Here is a better version of your code, but it is still a problem that is visible to each .index o (alphalen). So, as a bonus, I used to implement str.translate by the implementation of the suggestion @ two bit added by the alchemist.

  import string alphabet = string.ascii_lowercase alphalen = LEN (alphabet) poly = 11 def caesar (cipher, shift): plane = "" for the letter in cipher: num_in_alphabet = alphabet.index ( Letter) plain_num = (num_in_alphabet + shift)% alphalen plain_letter = alphabet [plain_num] plain = plane + plain_letter returned plain text print (caesar ("htrgti", poly)) # secret table = str.maketrans (alphabet, alphabet [ ] + Alphabet [: shift]) print ("htrgti" .translate (table)) # secret  

No comments:

Post a Comment