In: Computer Science
in Python3
The ord function in Python takes a character and return an
integer that represents that character.
It does not matter what the integer representing the character
actually is, but what matters is this:
ord('a') is 1 less than ord('b'), so that:
x=ord('a')
thisLetter = x+1 # thisLetter is the ord('b')
This is a powerful fact that is used in encryption techniques, so data transferred over the web is 'ciphered so it is unreadable to others.
To decipher data, we take a string and change it into another
string by adding a constant (called the key) to each of its
letters' ord value.
See the book's Case study: word play.
To cipher and decipher data, we need two functions: one to take a string, and change it to something else. For this we need to use the ord function, and add the 'key' to result in a new string. For example, say x is one letter to be ciphered, and the key is 3. We can use:
Newx=ord(x)+3
Newx will be an integer. To find out what letter that integer
represents you can use the chr function as in:
actualLetter = chr(x)
Write a function named cipher that takes a string.
The function returns that string in a ciphered form by using the
ord of the first letter of the string to cipher each letter
including the first letter. Hence for abc, use the ord of 'a', and
add it to the ord of 'a' and convert that result to a character use
the chr function. This character should be concatenated to the same
action on the letter b and so on. Hence the function returns:
chr(ord('a')+ord('a')) + chr(ord('a')+ord('b')) +
chr(ord('a')+ord('c')).
Obviously you need a loop to iterate on each letter.
Write another function to decipher (do the opposite of the previous function), given a string and returns the deciphered string. Obviously, the first letter's ord is halved to find the first letter, and that value is used to decipher the remaining letters.
From main, write code to get a string, as input, then call the
cipher function and print its output.
Then call the decipher function and display its output. The
decipher output should match the original string.
For help on this see the book's Case study: word play.
Add comments as needed but make sure to add top level comments.
#source code in python:
def cipher(strc,keyc):
c=""
for i in strc: #iterate over the charecter
v=ord(i)+keyc #add key to the character
c=c+chr(v) #here convert to char
return c #here return the cipher text
def decipher(strd,keyd):
d=""
for i in strd: #iterate over the character
v=ord(i)-keyd #here substract key
d=d+chr(v) #here convert to char
return d #here return the decrypted text
if __name__=="__main__":
strn=input("Enter String Here:")
k=int(input("Enter Key Here:"))
cipher_text=cipher(strn,k) #call the cipher function
decipher_text=decipher(cipher_text,k) #here call the decipher function
print("Enter Text:",strn)
print("Cipher Text:",cipher_text)
print("Decipher Test:",decipher_text)
#output:
#if you have any doubt or more information needed comment below..i will respond as possible as soon..thanks....