-8
def encryption(message,skey):
    encryptedmessage=''
    for character in message:
        if character.isalpha()==True:
            if character==character.lower():
                newchar=ord(character)-97
                newchar=(newchar+skey)%26
                encryptedmessage+= chr(newchar +97)
            else:
                newchar=ord(character)-65
                newchar=(newchar+skey)%26
                encryptedmessage+= chr(newchar +65)
        else:
            encryptedmessage+= character
    return('Your Message is: {:s}'.format(encryptedmessage))

def decryption(message,skey):
    decryptedmessage=''
    for character in message:
        if character.isalpha()==True:
            if character==character.lower():
                newchar=ord(character)-97
                newchar=(newchar-skey)%26
                decryptedmessage+= chr(newchar +97)
            else:
                newchar=ord(character)-65
                newchar=(newchar-skey)%26
                decryptedmessage+= chr(newchar +65)
        else:
            decryptedmessage+= character
    return('Your Message is: {:s}'.format(decryptedmessage))

while True:
    u=str(input("To encrypt a messege enter 'E' or 'e',To dycrypt a message enter 'D' or 'd',To stop the program enter'quit': "))
    if u=='':
        print("")  
    elif (u in 'Ee'):
        Message1=str(input('Please enter your message: '))
        key=int(input('Please enter the shifting key: '))        
        print(encryption(Message1,key))
    elif (u in 'Dd'):
        Message1=str(input('Please enter your message: '))
        key=int(input('Please enter the shifting key: '))        
        print(decryption(Message1,key))        
    elif u=='quit':
        print('Program ended.')
        break;
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    _What will happen if i used another number instead of 26..._ Well use another number and see what happens.... and **visit** the **[help center](http://stackoverflow.com/tour)** and take the tour to see what and how to ask. – B001ᛦ May 08 '18 at 08:42
  • 26 is the number of letters in the alphabet. If you use 28, you need to be using an alphabet of 28 letters. – khelwood May 08 '18 at 08:44
  • 1
    What would be the possible meaning of 26 in this context ? Hell even without context, if you asked me what would be the meaning of special 26 in English I would have answered it.... PS. _It's related with alphabet._ – BcK May 08 '18 at 08:46
  • 1
    You can read about Caesar/shift cipher here: https://en.wikipedia.org/wiki/Caesar_cipher – tevemadar May 08 '18 at 08:50

2 Answers2

1

It does not have to be 26, just this way the ciphertext will use the (English) alphabetical characters to represent the (English) alphabetical characters.
If you used 28, your ciphertext would use 26 characters from the a...z, '{', '|' range for representing lower-case, and 26 characters from the A...Z, '[', '\' range for upper-case. Both ranges have 28 characters, but 26 of them is in use only, so there will be a 2-character gap in each, its location depends on the actual "key" you use. This is something you can simply try, nothing bad will happen*.

(* as long as your original text does not contain the extra characters introduced in the encoded alphabet - however if it contains, those characters will become letters after decoding)

tevemadar
  • 12,389
  • 3
  • 21
  • 49
0

The line newchar=(newchar-skey)%26 shifts the letter by a value of skey and than returns a modulo 26.
This was done to make sure that returned value will always be a letter in English alphabet. In ASCII they are represented in ranges of 65 to 90 for upper_case and 97 to 122 for lower (those values are than shifted to range of 0-26 in line 10 and 6 respectively).

Changing that number would increase (or decrease) the range of characters that can be used by the script, see: ASCII table

Rafał Gajda
  • 151
  • 6