Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Encryption/Decryption of Affine Cipher

Views: 533
# Affine Cipher by Johnathan Nop # Affine cipher is a substitution cipher using a specific of function at mod 26. Each letter in the alphabet correspond to the numbers from 0-25, with A=0, B=1, C=2, etc. The function given for encoding is c = a*p+b (mod 26), where both a and "b" must be between 1 and 26. Along with that, a cannot be a number that have the same factors as 26. Use the encoding function to encode the message. To decode the message, the function given is p = a^-1(c-b), where a^-1 is the multiplicative inverse of a at modulus 26, and c is the number that was encoded from the function above.
# For the upcoming examples, let "a" = 7 and "b" = 11.(a)How can we take the secret message "MATH" to a numerical form?(b)How can the (numerical) plaintext message be encrypted?(c)How do we find a^-1?(d)Using what was found in part (c), how can the message encrypted in part (b) be decrypted?(e)How do we make a table of letters, numerical forms, and encrypted numerical forms, for encryption and decryption purposes?
%r encrypt = function(){ count = 0 plaintext = c("A", "B", "C", "D", "E", "F","G","H","I","J","K","L","M,","N","O","P","Q","R","S","T","U","V","W","X","Y","Z") for (i in 0:(length(plaintext) - 1)){ print(c(plaintext[i+1], count)) count = count + 1 } } encrypt()
[1] "A" "0" [1] "B" "1" [1] "C" "2" [1] "D" "3" [1] "E" "4" [1] "F" "5" [1] "G" "6" [1] "H" "7" [1] "I" "8" [1] "J" "9" [1] "K" "10" [1] "L" "11" [1] "M," "12" [1] "N" "13" [1] "O" "14" [1] "P" "15" [1] "Q" "16" [1] "R" "17" [1] "S" "18" [1] "T" "19" [1] "U" "20" [1] "V" "21" [1] "W" "22" [1] "X" "23" [1] "Y" "24" [1] "Z" "25"
# M = 12, A = 0, T = 19, H = 7
# Note that a = 7, and b = 11. The equation for encoding is c = (a*p + b) (mod 26). So to encrypt "MATH," take each individual numerical value as p and solve for c. Mod(7*12 +11, 26) Mod(7*0 +11, 26) Mod(7*19 +11, 26) Mod(7*7 +11, 26)
17 11 14 8
# Looking at the table above, 17 = R, 11 = L, 14 = O, and 8 = I. The encrypted message is "RLOI." This is the message I would send to whoever I was communicating with.
# Now that we know how to encrypt a message, how does the receiver decrypt the message? That is when the finding the multiplicative inverse of a is needed. (Normally brute force is the easiest, but Sage can help us.) Mod(1/7,26)
15
Mod(7*15,26)
1
#Thus the multiplicative inverse of 7 is 15.
# Recall that the function to decrypt is p = a^-1(c-b) (mod 26). Now that we have a^-1 = 15, we can now use the decryption function to decrypt the message "RLOI". Mod(15*(17-11),26) Mod(15*(11-11),26) Mod(15*(14-11),26) Mod(15*(8-11),26)
12 0 19 7
# Note that the number should convert back to the original numerical value of the original plaintext. 12 = M, 0 = A, 19 = T, 7 = H, thus the message is "MATH," which was the original message at the start.
%r encrypt = function(){ a = 7 b = 11 count = 0 plaintext = c("A", "B", "C", "D", "E", "F","G","H","I","J","K","L","M,","N","O","P","Q","R","S","T","U","V","W","X","Y","Z") for (i in 0:(length(plaintext) - 1)){ print(c(plaintext[i+1], count, (a*count + b) %% 26)) count = count + 1 } } encrypt()
[1] "A" "0" "11" [1] "B" "1" "18" [1] "C" "2" "25" [1] "D" "3" "6" [1] "E" "4" "13" [1] "F" "5" "20" [1] "G" "6" "1" [1] "H" "7" "8" [1] "I" "8" "15" [1] "J" "9" "22" [1] "K" "10" "3" [1] "L" "11" "10" [1] "M," "12" "17" [1] "N" "13" "24" [1] "O" "14" "5" [1] "P" "15" "12" [1] "Q" "16" "19" [1] "R" "17" "0" [1] "S" "18" "7" [1] "T" "19" "14" [1] "U" "20" "21" [1] "V" "21" "2" [1] "W" "22" "9" [1] "X" "23" "16" [1] "Y" "24" "23" [1] "Z" "25" "4"