1

How do I modify so it could go back to "a" after getting to "z" while encrypting?

# ceaser cipher encoder
# by SaLiXa MoUdInHo aka Okhotnik

def ccipher():
    print "This is a program to compute the ceaser cipher encoding algorithm"
    # get the input from the user
    text = raw_input("Enter the text you want to encode: ")
    jmp = input("Input the numerical value of the key: ")
    # set up an accumulator
    lstmsg = ""
    for j in text:
        lstmsg = lstmsg + chr(ord(j) +jmp)
    # output the encrypted message
    print "[" + lstmsg + "]"
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 1
    Did any of the related questions help? [this one](http://stackoverflow.com/a/18031651/953482) suggests modulo, which ought to be useful to you. – Kevin Apr 24 '14 at 11:47
  • possible duplicate of [Caesar Cipher Function in Python](http://stackoverflow.com/questions/8886947/caesar-cipher-function-in-python) – Perseids Apr 24 '14 at 12:04
  • @kelvin Thanks but i am not making use of conditionals at least not yet allowed – uJossyboy2580 Apr 25 '14 at 02:34
  • The code you posted outputs a ceasar cipher of the specified offset. Your question `go back to 'a' after getting to 'z'`, this doesn't make sense. If you're asking how to create another cipher, then just call that function again. – stackuser Apr 28 '14 at 21:45
  • presently what i want to do is to make it iterate to the letter "a" while encrypting. that is when it try's to convert the letter "y" to its ciphertext value with a key value of 2.... it should output the letter "a" since it shifthed through "z" to the next... instead of giving me some ASCII character. Thanks – uJossyboy2580 Jun 18 '14 at 13:36

1 Answers1

2

Python source code:

Note: working for negative shift numbers also
Note: if reverse shift then we do encode - decode message
Note: preserving spaces also

small_chars = [chr(item) for item in range(ord('a'), ord('z')+1)]
upper_chars = [item.upper() for item in small_chars]

def encode_chr(chr_item, is_upper_case):
    '''
    Cipher each chr_item.
    '''
    # setting orig and end order.
    if is_upper_case:
        orig_ord = ord('A')
        end_ord  = ord('Z')
    else:
        orig_ord = ord('a')
        end_ord  = ord('z')

    # calculating shift    
    temp_ord = ord(chr_item)+shift
    # calculating offset order with modulo.
    # char is after end_ord, calculating offset
    num_of_chars = 26
    offset_ord = (temp_ord - end_ord - 1)%num_of_chars
    return chr(orig_ord + offset_ord)

# enable while loop to repeat until status not 'y'
status = 'y'
while status == 'y':
    # enter word to cipher.
    word = raw_input("Word: ")
    # enter char shift
    shift = input("Shift: ")
    print

    # create cipher list variable
    cipher = list()
    # loop trough each char in word
    for chr_item in word:
        # encode just letters.
        # replace non-alfa with underscore: "_"
        if chr_item in upper_chars or chr_item in small_chars:
            # set is_uppser_case to True for upper case chars.
            is_upper_case = (chr_item in upper_chars) and True
            # cipher char.
            temp_chr = encode_chr(chr_item, is_upper_case)
            # append ciphered char to list
            cipher.append(temp_chr)
        elif chr_item is ' ':
            cipher.append(chr_item)
        else:
            cipher.append('_')

    # print word
    print word
    # print ciphered word
    print ''.join(cipher)

    # repeat again for another word?
    status = raw_input("Repeat? [y|n]: ")
    print

Test cases:

>>> 
Word: aAzZ!@
Shift: 1

aAzZ!@
bBaA__
Repeat? [y|n]: y

Word: aAzZ@!
Shift: -1

aAzZ@!
zZyY__
Repeat? [y|n]: y

Word: aAzZ@$
Shift: 27

aAzZ@$
bBaA__
Repeat? [y|n]: y

Word: aAzZ%^
Shift: -27

aAzZ%^
zZyY__
Repeat? [y|n]: n

>>> 

Output:

Note: if reverse shift then we do encode - decode message

>>>
Word: "Mary Had a Little    Lamb"
Shift: 1

"Mary Had a Little    Lamb"
_Nbsz Ibe b Mjuumf    Mbnc_
Repeat? [y|n]: y

Word: _Nbsz Ibe b Mjuumf    Mbnc_
Shift: -1

_Nbsz Ibe b Mjuumf    Mbnc_
_Mary Had a Little    Lamb_
Repeat? [y|n]: n

>>>