Simple Cryptography Program

1. #Reverse Cypher
2. message='Puchu can keep a secret if you give him fish.'
3. translated=''
4. 
5. i=len(message) -1
6. while i >=0:
7.     translated = translated + message[i]
8.     i = i -1
9.
10. print(translated)

Line #2 – saves the message into the variable ‘message’.

Line #3 – creates a variable named ‘translated’ with an empty value. This variable will hold the encrypted message.

Line #5 – len(message) gets the exact number of characters of the message, however it needs to be subtracted with 1 because the indexes starts with zero. So for example, if a string’s length is 5, the last index number is 4 ([0],[1]…,[4]).

Line #6 – A while loop. A while loop is basically consisted of 4 parts: (1) the while keyword; (2) the condition; (3) colon; (4) a block of code.
• The condition is an expression used in a while statement, which is the basis for the block of code to continue to run, until the condition is no longer met.
• In simple language, while the condition is true, execute.

Boolean Data Type – True or False
• Case Sensitive
• These are not string values. They do not need to have a single or double quotes when stored in a variable

Comparison Operators
These are operators used in an expression to evaluate a certain value, and give as a True or a False Boolean value.
< Less Than > Greater Than
<= Less Than or Equal To >= Greater Than or Equal To
== Equal To
!= Not Equal To

In Line #6, it states that while i is greater than or equal to 0, execute the block of code.

Line #7 and #8 – These contains the block of code for the while loop.

Block – is one or more line of codes grouped together with the same minimum amount of indentation. A block begins when a line is indented by four spaces. Following line indented by at least four spaces is part of the block. If the following line is indented with 8 spaces, then it’s a block of code within a block of code. The block will only end if the following line will have lesser (for inner block) or no indentation (for outermost block).

In Line #7, translated = translated + message[i]. It means that translated will be added with the value of the current translated’s value plus the value of message[i]. Since the value of i is the last index number of the message, the very first character that will be returned is the last character of the original message. When the loop returns to top, the next value obtained will be appended to translated.

In Line #8, the value of i is deducted by one. Meaning, the next value of i when the loop returns to top is the second to the last index, then third to the last, and so on. This results to the string inside the message to be returned in reverse using the while loop. And the reversed message is stored in translated.

Line #10 outputs the final value of translated, which is the actual reverse of the variable message.

Output of Encrypted Message

Upgraded Code
• Asking for input
• Added insertion of fixed characters to make the display look like rubbish

#Reverse Cypher

print('\n')
print('Enter your message:')
message=input()
translated=''

i=len(message) -1
while i >=0:
    translated = translated + message[i]+'xox'
    i = i -1
    
print('\n')
print('Original Message: \n'+message)
print('\n')
print('Encrypted Message: \n' + translated)

Sample output for encrypted message with insertion of fixed characters.