1

I am creating a very simple encryption algorithm where I covert each letter of a word into ascii, placing the ascii values into an array and then adding a number onto each value. To then convert the ascii back to letters, which will then output the new encrypted word. Known as the ceaser cipher.

But I cannot figure out how to add the key number to each element of the array.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • 1
    Edit your question and add your attempts so far, that way people can help you with your specific issue. If there's an error add the full traceback Python produces. If not, explain what the issue you're facing is and what you've tried so far to tackle it. – Dimitris Fasarakis Hilliard Dec 30 '16 at 22:11

2 Answers2

0

Try having a look online for solutions:

Caesar Cipher Function in Python

https://inventwithpython.com/chapter14.html

These links will provide you with clear answers to your questions.

Community
  • 1
  • 1
Stijn Van Daele
  • 285
  • 1
  • 14
0

As others will mention, when posing a question like this, please post a code attempt first. Having clear input/output and any associated stack trace errors helps people answer your question better.

That being said, I've written a simple ceaser cipher encryption method that shifts to the right based on a given key. This works by converting the characters of a string to their numerical ascii representations using the built in method ord(). We then add the shift value to this representation to right shift the values over by a given amount. Then covert back to characters using chr() We take into account some wrapping back to the beginning of the alphabet if the shifted_value exceeds that of 'z'.

def ceaser_cipher_encryption(string, shift):
    alpha_limit = 26
    encrypted_msg = []
    for index, character in enumerate(string.lower()):
        isUpperCharacter = string[index].isupper()
        shifted_value = ord(character) + shift
        if shifted_value > ord('z'):
            encrypted_msg.append(chr(shifted_value - alpha_limit))
        else: 
            encrypted_msg.append(chr(shifted_value))
        if isUpperCharacter:
            encrypted_msg[index] = encrypted_msg[index].upper()
    return ''.join(encrypted_msg)

Sample Output:

>>> ceaser_cipher_encryption("HelloWorld", 5)
MjqqtBtwqi
ospahiu
  • 3,465
  • 2
  • 13
  • 24