Python 34 Information encryption Cryptography is thousands o

Python 3.4

Information encryption (Cryptography) is thousands of years old and has been used to protect secrets, communicate messages and preserve the integrity of modern commerce. A simple version (the Substitution Cipher) is the use of one character to represent another within a message. In this method, the letter “A” is encrypted as “Q”, “B” is “W” and so on. This concept can be expanded beyond capital letters to include lower case, spaces, special characters and the entire international character set to completely encode a message making it difficult to decipher. A version of the Substitution Cipher, (the Caesar Shift) uses a standard integer value to slide or shift the character up or down the character set. If the shift integer is -3, then the character “D” is encrypted as “A” (ie: -3 positions in the alphabet), “E” is “B”, and so on.

Write a Python program that prompts the user for a message string and an integer. Based on these entries and using Python’s string and character manipulation capabilities, create a new string by capitalizing all the characters, ignoring punctuation, replacing all spaces with a “#”, then incrementing each remaining character’s ordinal (ord) value by the integer provided. The result of the character increment should always be a capital letter between A Z so if an incremented value goes beyond the capital letters of the alphabet, (ordinal values between 65 and 90), it should ‘wrap around’ to the other end. Once the string is encoded, your program should display the newly encoded message. For example, the user enters the string “Howbow Dah!” and the integer 5 –

Solution

s = input(\"Enter a string: \")
n = int(input(\"Enter a number: \"))

try:
temp_list1 = []
for i in range(len(s)):
if s[i] == \' \':
temp_list1.append(\'#\')
elif ord(s[i].upper()) >= 65 and ord(s[i].upper()) <= 90:
temp_list1.append(s[i].upper())
else:
temp_list1.append(s[i].upper())

except Exception as e:
print(e)
  
temp_list2 = []
for val in temp_list1:
if ord(val) >=65 and ord(val) <= 90:
if (ord(val) + n) >= 65 and (ord(val) + n) <= 90:
temp_list2.append(chr(ord(val)+n))
elif (ord(val) + n) > 90:
tmp_val = (ord(val) + n) - 90
temp_list2.append(chr(65 + tmp_val))
else:
temp_list2.append(val)

print(\"Encoded Message: \" , \'\'.join(temp_list2))

Python 3.4 Information encryption (Cryptography) is thousands of years old and has been used to protect secrets, communicate messages and preserve the integrity

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site