Hey guys I am trying to implement a caesar cypher. That is f(p) = (p+k) mod 26.
ID: 675333 • Letter: H
Question
Hey guys I am trying to implement a caesar cypher. That is f(p) = (p+k) mod 26. k = 11. I have figured out how to shift through the ascii table and get some kind of encoded message, but I dont know how to implement the mod 26 part in my code. Here is my code:
.data
String: .space 80
prompt1: .asciiz "Please enter a string to encrypt "
.text
.globl main
main:
li $v0, 4
la $a0, prompt1
syscall
li $v0, 8
la $a0, String
li $a1, 80
syscall
li $t3, 26
la $t1, String # loads address of
# the string labeled
# by String in $t1
loop:
lb $t2, 0($t1) #load a character
beq $t2, $zero, endfor #if at end of string go to endfor
addiu $t2, $t2, 11 #shift char in ascii by 11
sb $t2, 0($t1) #store char back in string
addiu $t1, $t1, 1 #go to next char in string
j loop #jump back to cypher next char
endfor: li $v0, 4 #loads 4 in $v0
la $a0, String #store address of string in a0
syscall
jr $ra #return the string to screen
Explanation / Answer
# Caesar Cipher
2.
3. MAX_KEY_SIZE = 26
4.
5. def getMode():
6. while True:
7. print('Do you wish to encrypt or decrypt a message?')
8. mode = input().lower()
9. if mode in 'encrypt e decrypt d'.split():
10. return mode
11. else:
12. print('Enter either "encrypt" or "e" or "decrypt" or "d".')
13.
14. def getMessage():
15. print('Enter your message:')
16. return input()
17.
18. def getKey():
19. key = 0
20. while True:
21. print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
22. key = int(input())
23. if (key >= 1 and key <= MAX_KEY_SIZE):
24. return key
25.
26. def getTranslatedMessage(mode, message, key):
27. if mode[0] == 'd':
28. key = -key
29. translated = ''
30.
31. for symbol in message:
32. if symbol.isalpha():
33. num = ord(symbol)
34. num += key
35.
36. if symbol.isupper():
37. if num > ord('Z'):
38. num -= 26
39. elif num < ord('A'):
40. num += 26
41. elif symbol.islower():
42. if num > ord('z'):
43. num -= 26
44. elif num < ord('a'):
45. num += 26
46.
47. translated += chr(num)
48. else:
49. translated += symbol
50. return translated
51.
52. mode = getMode()
53. message = getMessage()
54. key = getKey()
55.
56. print('Your translated text is:')
57. print(getTranslatedMessage(mode, message, key))
How the Code Works
The encryption and decryption processes are the reverse of the other, and even then they still share much of the same code. Let’s look at how each line works.
1. # Caesar Cipher
2.
3. MAX_KEY_SIZE = 26
The first line is just a comment. MAX_KEY_SIZE is a constant that stores the integer 26 in it. MAX_KEY_SIZE reminds us that in this program, the key used in the cipher should be between 1 and 26.
Deciding to Encrypt or Decrypt
5. def getMode():
6. while True:
7. print('Do you wish to encrypt or decrypt a message?')
8. mode = input().lower()
9. if mode in 'encrypt e decrypt d'.split():
10. return mode
11. else:
12. print('Enter either "encrypt" or "e" or "decrypt" or "d".')
The getMode() function will let the user type in if they want encryption or decryption mode for the program. The value returned from input() and lower() is stored in mode. The if statement’s condition checks if the string stored in mode exists in the list returned by 'encrypt e decrypt d'.split().
This list is ['encrypt', 'e', 'decrypt', 'd'], but it is easier for the programmer to type 'encrypt e decrypt d'.split() and not type in all those quotes and commas. Use whichever is easiest for you; they both evaluate to the same list value.
This function will return the first character in mode as long as mode is equal to 'encrypt', 'e', 'decrypt', or 'd'. Therefore, getMode() will return the string 'e' or the string 'd' (but the user can type in either “e”, “encrypt”, “d”, or “decrypt”.)