Encrypting messages in Python. From simple to complex. Caesar's cipher

A little about the project

I, personally, have long been interested in the topic of information encryption, however, each time plunging into this topic, I realized how difficult it is and realized that it is better to start with something simpler. I, personally, plan to write a number of articles on this topic, in which I will show you various encryption algorithms and their implementation in Python , demonstrate and disassemble my project created in this direction. So, let's begin.






To begin with, I would like to tell you what already known algorithms we will consider in my articles. The list is presented to you below:





  • Caesar's cipher





  • Vigenère cipher





  • Replacement cipher





  • Homophonic cipher





  • RSA encryption





Caesar's cipher

So, after a short introduction to the cycle, I propose to move on to the main topic of today's article, namely the Caesar Cipher.





What it is?

The Caesar cipher is a simple type of substitution cipher where each letter of plain text is replaced by a letter with a fixed number of positions down the alphabet. The principle of its operation can be seen in the following illustration:





What features does it have?

In the Caesar Cipher, as in the encryption algorithm, I can distinguish two main features. The first feature is the simplicity and availability of the encryption method, which may help you dive into this topic, the second feature is, in fact, the encryption method itself.





Software implementation

, , , , .





, , . .





alfavit =  'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'
#  
      
      



, , . , , "" , "2", "".





,



smeshenie, , message, , , upper(),



, . itog, . :





smeshenie = int(input(' : '))    #    
message = input("  : ").upper()    # ,    
itog = ''    #     
      
      



, . for



, , , alfavit, ( ):





for i in message:
    mesto = alfavit.find(i)    #    
    new_mesto = mesto + smeshenie    #      smeshenie 
      
      



, if



, itog :





if i in alfavit:
        itog += alfavit[new_mesto] #        
    else:													 #      .
        itog += i
print (itog)

      
      



, . , :





alfavit =  'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'
smeshenie = int(input(' : '))
message = input("  : ").upper()
itog = ''
for i in message:
    mesto = alfavit.find(i)
    new_mesto = mesto + smeshenie
    if i in alfavit:
        itog += alfavit[new_mesto]
    else:
        itog += i
print (itog)

      
      



, , , .





, - . ( ).





, "" . :





alfavit =  'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'
smeshenie = int(input(' : '))
message = input("  : ").upper()    #  ,  
itog = ''
      
      



The rest can be left the same, but if you wish, you can change the names of the variables.





By and large, the 'biggest' changes for us will occur in the part of the code where we have the algorithm.





Outcome

You have successfully written an algorithm for encrypting and decrypting a message in Python using the Caesar method. In the next article, we will look at the Vigenère cipher, as well as analyze its implementation in Python, but for now I suggest you write in the comments options for modernizing the program (code or millet suggestions and wishes). I will definitely consider your opinion.








All Articles