I've been working on this encryption program, basic overview is that the function shifts 2 letters backward on the alphabet if letter inputted is uppercase, otherwise it goes up 2 on the alphabet, but for some reason, when I enter a number it outputs a letter. How do I make it to just output the number inputted without converting it into a letter?
def text_encrypt(plain_text):
encrypted = ''
for i in range(len(plain_text)):
if plain_text[i] == ' ':
encrypted = encrypted + plain_text [i]
elif plain_text[i].isupper():
encrypted = encrypted + chr((ord(plain_text[i])-2-65)%26+65)
else:
encrypted = encrypted + chr((ord(plain_text[i])+2-97)%26+97)
return encrypted
#Testing code
text_plain = input("What would you like Encrypted?:")
plain_text = text_encrypt(text_plain)
print("Encrypted text: {}".format(plain_text))
#Sample Output: What would you like Encrypted?: One 1
Prints: Mpg g #I need it to print Mpg 1
Change your else to elif to detect lowercase characters also. Else case should be default of putting the character without changing.
if plain_text[i].isupper():
encrypted = encrypted + chr((ord(plain_text[i])-2-65)%26+65)
elif plain_text[i].islower():
encrypted = encrypted + chr((ord(plain_text[i])+2-97)%26+97)
else:
encrypted = encrypted + plain_text [i]
You have skipped encryption for spaces. Similarly, you can skip encrypting digits.
isdigit() method can be used to check if a character/string is digit or not.
def text_encrypt(plain_text):
encrypted = ''
for i in range(len(plain_text)):
if plain_text[i].isdigit() or plain_text[i] == ' ':
encrypted = encrypted + plain_text [i]
elif plain_text[i].isupper():
encrypted = encrypted + chr((ord(plain_text[i])-2-65)%26+65)
else:
encrypted = encrypted + chr((ord(plain_text[i])+2-97)%26+97)
return encrypted
Related
I'm having an issue with Vigenere Cipher Decryption, When an encrypted message is decrypted, the decrypted message is not printed, but the encrypted is?
I cant seem to see why this dose not work. But its probably a simple issue.
I think the issue is in the main decrypt function.
Help would be much appreciated
Encryption works fine
def encrypt_key(message, key):
i = 0
empty_key = '' # empty key to append to
# turn characters into index
for characters in message:
if characters.isalpha(): # checks if character is in the alphabet
empty_key = empty_key + key[i % len(key)] # append characters to empty key
i = i + 1 # increase integer value by 1
else:
empty_key = empty_key + ' ' # if character is a space or punctuation then add empty space
return empty_key
def encrypt_decrypt(message_char, key_char, choice='encrypt'):
if message_char.isalpha():
first_alphabet_letter = 'a' # assume character is lowercase
if message_char.isupper():
first_alphabet_letter = 'A' # assume character is upper, then replace with A
original_char_position = ord(message_char) - ord(first_alphabet_letter) # difference results in position of char in alphabet
key_char_position = ord(key_char.lower()) - ord('a')
if choice == 'encrypt':
new_char_position = (original_char_position + key_char_position) % 26 # encrypts & loops back around with % 26
else: # if choice == 'decrypt':
new_char_position = (original_char_position - key_char_position + 26) % 26 # decrypts & loops back around
return chr(new_char_position + ord(first_alphabet_letter))
return message_char
def encrypt(message, key):
cipher = ''
empty_key = encrypt_key(message, key)
for message_char, key_char in zip(message, empty_key):
cipher = cipher + encrypt_decrypt(message_char, key_char)
return cipher
def decrypt(cipher, key):
message = ''
empty_key = encrypt_key(cipher, key)
for cipher_char, key_char in zip(cipher, empty_key):
message = message + encrypt_decrypt(cipher_char, key_char, 'decrypt')
return message
message = input('Enter your message to encrypt here: ')
key = input('Enter your key: ')
cipher = encrypt(message, key) # inputs message and key to encrypt function
decrypt_message = decrypt(cipher, key)# inputs message and key to decrypt function
print(f'Cipher: {cipher}')
print(f'Decrypted: {decrypt_message}')
def choice():
user_choice = int(input('''
Enter your choice (1 or 2):
1. Encrypt
2. Decrypt
> '''))
if user_choice == 1:
print(f'Cipher: {cipher}')
print('')
elif user_choice == 2:
print('')
print(f'Decrypted: {decrypt_message}')
choice()
I have it set to ask the user a word to encrpyt which works fine using my own alphabet.
My issue is trying to also get it to return the deciphered text.
So far I have it either returning the encrypted message twice or sending back a different version of the encrypted message.
I have tried using - instead of + in my for char, and it gives me a error which I thought was the correct way to do it.
alphabet = "abcdefghijklmnopqrstuvwxyz"
key = "zjrekydnqoluaxmicvpgtfbhws"
def decryptMessage(ciphertext, key):
plaintext = ""
for char in ciphertext:
if alphabet.find(char) < +1:
plaintext += key[alphabet.find(char)]
else:
plaintext += char
return plaintext
def encryptMessage(plaintext, key):
ciphertext = ""
for char in plaintext:
if alphabet.find(char) > -1:
ciphertext += key[alphabet.find(char)]
else:
ciphertext += char
return ciphertext
message = input("Type a message to encrypt: ")
encrypted = encryptMessage(message, key)
decrypted = decryptMessage(encrypted, key)
print("Plaintext message: " + message)
print("Encrypted message: " + encrypted)
print("Decrypted message: " + decrypted)
you should use the builtin str.translate
message = "hello world"
alphabet = b"abcdefghijklmnopqrstuvwxyz"
key = b"zjrekydnqoluaxmicvpgtfbhws"
encrypted = message.translate(dict(zip(alphabet,key)))
print("E:",encrypted)
decrypted = encrypted.translate(dict(zip(key,alphabet)))
print("D:",decrypted)
If you want to keep with the theme of your original code:
You need to modify as follows:
alphabet = "abcdefghijklmnopqrstuvwxyz"
key = "zjrekydnqoluaxmicvpgtfbhws"
def decryptMessage(ciphertext, key):
plaintext = ""
for char in ciphertext:
if key.find(char) > -1:
plaintext += alphabet[key.find(char)]
else:
plaintext += char
return plaintext
def encryptMessage(plaintext, key):
ciphertext = ""
for char in plaintext:
if alphabet.find(char) > -1:
ciphertext += key[alphabet.find(char)]
else:
ciphertext += char
return ciphertext
message = input("Type a message to encrypt: ")
encrypted = encryptMessage(message, key)
decrypted = decryptMessage(encrypted, key)
print("Plaintext message: " + message)
print("Encrypted message: " + encrypted)
print("Decrypted message: " + decrypted)
I would greatly appreciate your feedback on my first ever Python project! :D
Basically I am coding a Caesar Cipher and I think its pretty terribly 'optimised / efficient' if you know what I mean, this is because I copied and pasted the encrypt() method for the decrypt() method and the only thing I changes was instead of rotating the numbers more, I rotated them less. This is what I'm talking about:
newPosition = (abc.find(letter) - key) % 26
^^ Instead of having a + (plus) I made it a - (minus) ^^
Is there a way I can sort of call the encrypt() method at just the newPosition line? Or what I did was correct and it doesn't need fixing (which I highly doubt)
** Please do take in mind that I do not have much knowledge in Python (if any at all) since I just started today so don't blow my brain up with some super complex code. THANK YOU!!! **
abc = 'abcdefghijklmnopqrstuvwxyz'
def main():
message = input("Would you like to encrypt or decrypt a word?")
if message.lower() == "encrypt":
encrypt()
elif message.lower() == "decrypt":
decrypt()
else:
print("You must enter either 'encrypt' or 'decrypt'.")
main()
def encrypt():
message = input("Enter a message to encrypt: ")
message = message.lower()
key = int(input("What number would you like for your key value?"))
cipherText = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) + key) % 26
cipherText += abc[newPosition]
else:
cipherText += letter
print(cipherText)
return cipherText
def decrypt():
message = input("Enter a message to decrypt: ")
message = message.lower()
key = int(input("What number would you like for your key value?"))
cipherText = ""
for letter in message:
if letter in abc:
newPosition = (abc.find(letter) - key) % 26
cipherText += abc[newPosition]
else:
cipherText += letter
print(cipherText)
return cipherText
main()
In general, str.find is bad performance-wise. It's O(n) complexity, which isn't awful, but you rarely actually need it. In this case you can use ord to convert each letter to its ordinal, then subtract ord('a') to get 0-25 instead of 97-122.
This is particularly useful, because you can then use chr to convert back without needing a lookup.
for letter in message:
if letter in string.ascii_lowercase: # same as "abcdef..z"
new_position = ((ord(letter) - ord('a') + key) % 26) + ord('a')
new_ch = chr(new_position)
ciphertext += new_ch
Note also that concatenating strings with += isn't as fast as something like str.join.
new_letters = [chr(((ord(letter) - ord('a') + key) % 26) + ord('a')) if letter in ascii_lowercase else letter for letter in message]
ciphertext = "".join(new_letters)
And since that chr(((ord(letter) - ord('a') + key) % 26) + ord('a')) is so ugly, I'd refactor that into a function.
def rotate(letter, key=0):
c_pos = ord(letter) - ord('a')
rotated = c_pos + key
modded = rotated % 26
final_pos = modded + ord('a')
return chr(final_pos)
new_letters = [rotate(c, key) if c in string.ascii_lowercase else c for c in letters]
ciphertext = "".join(new_letters)
A point of maintainability: it's easier to write testably good code if you separate your inputs from your results. Right now you'd have to do some monkey patching of stdin to write unit tests for any of your functions, but if you move your requests for user input into main and out of their respective function, that gets much easier.
def main():
message = input("What's the message to encrypt/decrypt? ")
key = int(input("What number would you like for your key value? "))
choice = input("Choose: encrypt or decrypt. ")
if choice == "encrypt":
result = encrypt(message, key)
elif choice == "decrypt":
result = decrypt(message, key)
else:
# something here about a bad user input.
In fact, when you consider the Caesar Cipher is reversible by flipping the sign of the key, you can simply do:
if choice == "encrypt":
result = encrypt(message, key)
elif choice == "decrypt":
result = encrypt(message, key * (-1))
and not write a decrypt function at all!
Locked. There are disputes about this question’s content being resolved at this time. It is not currently accepting new answers or interactions.
def menu():
choice = input("Press 1 to encode, 2 to decode, 9 to exit ")
return choice
def makeKeycode(message):
key = input("What is the key? ").upper()
length = len(message)
keycode = ""
counter = 0
while length >0:
if counter == len(key):
counter = 0
keycode = keycode + key[counter]
counter = counter + 1
length = length - 1
print(keycode)
return keycode
def enterMessage():
message = input("What is the message ").upper()
return message
def encodeMessage(message, keycode):
ciphertext =""
alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
for i in range (len(message)):
character = message[i]
charCode = alphabet.find(character)
keycodeChar =keycode[i]
keyLetter = alphabet.find(keycodeChar)
position = (charCode + keyLetter)%25
cipherLetter = alphabet[position]
ciphertext =ciphertext + cipherLetter
return ciphertext
def decodeMessage(ciphertext,keycode):
ciphertext =""
alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
for i in range (len(ciphertext)):
character = ciphertext[i]
charCode = alphabet.find(character)
keycodeChar =keycode[i]
keyLetter = alphabet.find(keycodeChar)
position = (charCode - keyLetter)%25
cipherLetter = alphabet[position]
ciphertext =ciphertext - cipherLetter
return message
def enterCipher ():
ciphertext = input("Enter the text to be decoded")
return ciphertext
def encode():
message = enterMessage()
keycode = makeKeycode(message)
ciphertext = encodeMessage(message,keycode)
print(ciphertext)
def decode():
keycode = makeKeycode(ciphertext)
message = decodeMessage(ciphertext, keycode)
print (message)
def main():
MyDictionary=("A:1","B:2","C:3","D:4","E:5","F:6","G:7","H:8","I:9","J:10","K:11","L:12","M:13","N:14","O:15","P:16","Q:17","R:18","S:19","T:20","U:21","V:22","W:23","X:24","Y:25","X:26")
print (MyDictionary)
choice = 0
while choice !=9:
choice = int(menu())
if choice == 1:
encode()
elif choice == 2:
decode()
if __name__ == "__main__":
main()
Hi, I cant get my code to work, my encode function works but i am struggling to fix my decode function. I dont understand where i am going wrong. I want to be able to decode the message i will have encoded, but this doesn't work as the decode function stops the program. Have I made an error in the coding? Thanks for all the help
Regards
def decodeMessage(ciphertext,keycode):
ciphertext =""
It appears that you are resetting ciphertext to a null string. as a result, your len(ciphertext) is 0 which means you ae not doing anything.
python 2.75
sig = 'abcd'
def test(sig):
sig = ""
print 'sig = ', sig
>>> test(sig)
sig = ''
>>> print sig
'abcd'
I am finding two problems with your decode message function:
def decodeMessage(ciphertext,keycode):
ciphertext =""
alphabet= "ABCDEFGHIJKLMNOPQRTUVWXYZ"
for i in range (len(ciphertext)):
character = ciphertext[i]
charCode = alphabet.find(character)
keycodeChar =keycode[i]
keyLetter = alphabet.find(keycodeChar)
position = (charCode - keyLetter)%25
cipherLetter = alphabet[position]
ciphertext =ciphertext - cipherLetter
return message
The first issue that I found was the fact that you pass in ciphertext to decodeMessage as a mandatory parameter and then you change the value of ciphertext to "" (an empty string). I would recommend changing the ciphertext in the parameters to something like "ct" to prevent that overlap.
The second issue I found was the fact you return message, but you don't perform any action on message. When running this program, you'll get a global error because message is not global yet you're calling message in the local scope without having defined it locally.
Now, onto another matter: Your encode method doesn't encode anything correctly. Look at the following output:
Press 1 to encode, 2 to decode, 9 to exit 1
What is the message some
What is the key? 18
YNLD
Press 1 to encode, 2 to decode, 9 to exit 1
What is the message some
What is the key? 5
YNLD
Press 1 to encode, 2 to decode, 9 to exit 1
What is the message some
What is the key? 1
YNLD
All 'some's get encoded to 'YNLD' regardless of the key. To cipher something using a Caesar Cypher, you're going to want to use ords and chrs. Ord makes the character numerical, Chr makes the number into a letter. Consider the following function made in Python 3, for example:
from string import ascii_letters
def cypher_letter(letter, key):
if letter not in ascii_letters: #commas, question marks, etc
return letter
if letter.islower(): #lowercase and uppercase have differing ord values
lower, upper = ord('a'), ord('z') # values: 97 to 122
elif letter.isupper():
lower, upper = ord('A'), ord('Z') # values: 65 to 90
else:
print("Something went wrong.") #probably didn't enter valid letters
cypher = ord(letter) + key #if key is "1" and letter is 'a', 97 + 1
if cypher < lower: cypher += 26 #26 letters in alphabet, this is for looping
if cypher > upper: cypher -= 26 #back around if it exceeds upper,lower limits
return chr(cypher) #chr(98) = 'b'
The following code:
cyphered_words = []
keys = [18, 5, 1]
for key in keys:
encoded = ""
for letter in 'some':
encoded += (cypher_letter(letter, key)) #to cypher we pass the key
print(encoded)
cyphered_word.append(encoded)
Outputs this:
Kgewlzafy hjwllq dgfy, oalz ugehdwp dwllwjk!
Xtrjymnsl uwjyyd qtsl, bnym htruqjc qjyyjwx!
Tpnfuijoh qsfuuz mpoh, xjui dpnqmfy mfuufst!
Cyphering and decyphering is almost the same thing. It's like they're yin and yang or something, so the decypher function would look like:
def decrypt_message(message, key):
decyphered_text = ""
for letter in message:
decyphered_text += cypher_letter(letter, (key*-1)) #to decypher we pass the negative key
return decyphered_text
And a loop to decrypt our 3 'some's:
for count, word in enumerate(cyphered_words):
print(decrypt_message_k(word, keys[count]))
And the output:
Something pretty long, with complex letters!
Something pretty long, with complex letters!
Something pretty long, with complex letters!
I hope this helps you get on the right track.
I am supposed to encrypt any string with a loop where some letters are change to special symbols, like a=# e=() h=# l=1 and so on. I honestly have no idea how to do this. the function replace cannot be used. So far I have this but I know it is wrong.
encryption(code):
encrypted = ''
for c in code:
if c != '':
a='#'
e='()'
h='#'
l='1'
r='+'
s='$'
v='^'
x='*'
return The encrypted code is:
You can't use replace? Perfect, because you should use translate instead.
def encrypt(cleartext):
mapping = {'a':'#',
'e':'()',
'h':'#', ...}
transdict = str.maketrans(mapping)
enctext = cleartext.translate(transdict)
return enctext
However I get the feeling that your instructor wants you to learn how to iterate over the cleartext, do a lookup for the correct value, and add it to an accumulator string. In pseudocode:
function encrypt(cleartext):
encrypted = ""
for each character in cleartext:
if character == 'h':
encrypted += "#"
elif character == "a":
encrypted += "#"
...
return encrypted
I'd do something like:
def encrypt(cleartext):
mapping = {'a':'#',
'e':'()', ... }
return ''.join(mapping.get(ch, ch) for ch in cleartext)
But if you turn that in without understanding it, I'm sure your teacher will fail you!
def encryption(code):
encrypted = ''
for c in code:
if c == 'a':
encrypted = encrypted + '#'
elif c == 'e':
encrypted = encrypted + '()'
elif c == 'h':
encrypted = encrypted + '#'
elif c == 'l':
encrypted = encrypted + '1'
elif c == 'r':
encrypted = encrypted + '+'
elif c == 's'
encrypted = encrypted + '$'
elif c == 'v'
encrypted = encrypted + '^'
elif c == 'x'
encrypted = encrypted + '*'
#here you can add other characters that need to be encrypted in elif blocks.
else:
encrypted = encrypted + c
return encrypted
Code:
def encryption(text):
if not text:
return 'No text present.'
encrypted = ''
encryption_key = {'a':'#', 'e':'()', 'h':'#', 'l':'1',
'r':'+', 's':'$', 'v':'^', 'x':'*'}
for c in text:
encrypted += encryption_key.get(c, '?')
return encrypted
Test:
>>> encryption('shell')
'$#()11'
>>> encryption('shellac')
'$#()11#?'