# a little bit of cryptography:with(StringTools):Map( Capitalize, "This is a test." ); NiNRMFRISVN+SVN+QX5URVNULjYiMap( LowerCase, %); NiNRMHRoaXN+aXN+YX50ZXN0LjYiconvert("Test string", 'bytes'); NiM3LSIjJSkiJCwiIiQ6IiIkOyIiI0tGJkYnIiQ5IiIkMCIiJDUiIiQuIg==convert([65,66,67], 'bytes'); NiNRJEFCQzYiconvert("abcdefghijklmnopqrstuvwxyz", 'bytes'); NiM3PCIjKCoiIykqIiMqKiIkKyIiJCwiIiQtIiIkLiIiJC8iIiQwIiIkMSIiJDIiIiQzIiIkNCIiJDUiIiQ2IiIkNyIiJDgiIiQ5IiIkOiIiJDsiIiQ8IiIkPSIiJD4iIiQ/IiIkQCIiJEEiconvert("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 'bytes'); NiM3PCIjbCIjbSIjbiIjbyIjcCIjcSIjciIjcyIjdCIjdSIjdiIjdyIjeCIjeSIjeiIjISkiIyIpIiMjKSIjJCkiIyUpIiMmKSIjJykiIygpIiMpKSIjKikiIyEq# irem(a, b) gives the remainder r of a upon division by b, # i.e. the r in a = q * b + r with 0 <= r < b. # For example,irem(17,5); NiMiIiM=# butirem(-1,5); NiMhIiI=# the Caesar cipher is the following: # it takes the plaintext and shifts the letters # alphabetically by some fixed amount # For instance, a shift by 3 amounts to # a -> d # b -> e # c -> f # ... # w -> z # x -> a # y -> b # z -> d # # So, abc becomes def, for instance. # to decipher, oneft back the other direction by the same amount. # now we implement the Caesar cipher, # which is shifting the letters of the alphabet by some fixed amount caesar := proc(s::string, shift::integer) local S, l, i, c; S := convert(s,'bytes'); l := nops(S); for i to l do c := S[i]; if c >= 97 and c <= 122 then c := c - 97 + shift; c := irem(c, 26); if c < 0 then c := c + 26; end; S[i] := 97 + c; end; end; return convert(S,'bytes'); end; NiM+SSdjYWVzYXJHNiJmKjYkJ0kic0dGJUknc3RyaW5nR0kqcHJvdGVjdGVkR0YrJ0kmc2hpZnRHRiVJKGludGVnZXJHRis2JkkiU0dGJUkibEdGJUkiaUdGJUkiY0dGJUYlRiVDJj44JC1JKGNvbnZlcnRHRis2JDkkLkkmYnl0ZXNHRiU+OCUtSSVub3BzR0YrNiNGNj8oOCYiIiJGREY+SSV0cnVlR0YrQyQ+OCcmRjY2I0ZDQCQzMSIjKCpGSDFGSCIkQSJDJj5GSCwoRkhGRCEjKCpGRDklRkQ+RkgtSSVpcmVtR0YrNiRGSCIjRUAkMkZIIiIhPkZILCZGSEZERlpGRD5GSSwmRk5GREZIRkRPLUY4NiRGNkY7RiVGJUYl# encryption:caesar("abcdefghijklmnopqrstuvwxyz", 3); NiNRO2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6YWJjNiI=# decryption using the negative shiftcaesar(%, -3); NiNRO2FiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6NiI=caesar("abcdefghijklmnopqrstuvwxyz", 26); NiNRO2FiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6NiI=vigenere_cipher := proc(s::string, k::string) local S, l, K, kl, i, j, c, d; S := convert(s,'bytes'); l := nops(S); K := convert(k,'bytes'); kl := nops(K); for i to l do j := irem(i, kl) + 1; c := S[i]; d := K[j]; if c >= 97 and c <= 122 then c := c - 97; c := c + d; c := irem(c, 26); if c < 0 then c := c + 26; end; S[i] := 97 + c; end; end; return convert(S,'bytes'); end:p := "Shake and quiver, little tree, Throw gold and silver down to me";NiM+SSJwRzYiUWpuU2hha2V+YW5kfnF1aXZlcix+bGl0dGxlfnRyZWUsflRocm93fmdvbGR+YW5kfnNpbHZlcn5kb3dufnRvfm1lRiU=c := vigenere_cipher(p, "verysecret");NiM+SSJjRzYiUWpuU3JydmJ+a2twfm5lemdibSx+eHdxZGNwfm9iYnEsflR5Y2xyfmRhemF+cnlhfmNmeGpiYn5vbHJ4fmZjfnd2RiU=vigenere_decipher := proc(s::string, k::string) local S, l, K, kl, i, j, c, d; S := convert(s,'bytes'); l := nops(S); K := convert(k,'bytes'); kl := nops(K); for i to l do j := irem(i, kl) + 1; c := S[i]; d := K[j]; if c >= 97 and c <= 122 then c := c - 97; c := c - d; while c < 0 do c := c + 26; end; c := irem(c, 26); S[i] := 97 + c; end; end; return convert(S,'bytes'); end:vigenere_decipher(c, "verysecret");NiNRam5TaGFrZX5hbmR+cXVpdmVyLH5saXR0bGV+dHJlZSx+VGhyb3d+Z29sZH5hbmR+c2lsdmVyfmRvd25+dG9+bWU2Ig==p := "I have seen too much not to know that the impression of a woman may be more valuable than the conclusion of an analytical reasoner.";NiM+SSJwRzYiUV5zSX5oYXZlfnNlZW5+dG9vfm11Y2h+bm90fnRvfmtub3d+dGhhdH50aGV+aW1wcmVzc2lvbn5vZn5hfndvbWFufm1heX5iZX5tb3JlfnZhbHVhYmxlfnRoYW5+dGhlfmNvbmNsdXNpb25+b2Z+YW5+YW5hbHl0aWNhbH5yZWFzb25lci5GJQ==vigenere_cipher(p, "arthurdoyle"); Error, (in vigenere_cipher) assigning to a long list, please use ArraysS := convert(p,'bytes');NiM+SSJTRzYiN19zIiN0IiNLIiQvIiIjKCoiJD0iIiQsIkYoIiQ6IkYsRiwiJDUiRigiJDsiIiQ2IkYwRigiJDQiIiQ8IiIjKipGKUYoRi5GMEYvRihGL0YwRigiJDIiRi5GMCIkPiJGKEYvRilGKkYvRihGL0YpRixGKCIkMCJGMSIkNyIiJDkiRixGLUYtRjZGMEYuRihGMCIkLSJGKEYqRihGNUYwRjFGKkYuRihGMUYqIiRAIkYoIiMpKkYsRihGMUYwRjhGLEYoRitGKiIkMyJGMkYqRjtGPEYsRihGL0YpRipGLkYoRi9GKUYsRihGM0YwRi5GM0Y8RjJGLUY2RjBGLkYoRjBGOUYoRipGLkYoRipGLkYqRjxGOkYvRjZGM0YqRjxGKEY4RixGKkYtRjBGLkYsRjgiI1k=S[1] := 100;Error, assigning to a long list, please use ArraysT := Array(S);NiM+SSJURzYiLUknUlRBQkxFR0YlNioiKnc3SFEiSSlhbnl0aGluZ0dJKnByb3RlY3RlZEdGK0kmQXJyYXlHRitJLHJlY3Rhbmd1bGFyR0YlSS5Gb3J0cmFuX29yZGVyR0YlNyIiIiI7RjAiJEoiT[1];NiMiI3Q=vigenere_cipher := proc(s::string, k::string) local S, T, L, l, K, kl, i, j, c, d; S := convert(s,'bytes'); T := Array(S); l := nops(S); K := convert(k,'bytes'); kl := nops(K); for i to l do j := irem(i, kl) + 1; c := T[i]; d := K[j]; if c >= 97 and c <= 122 then c := c - 97; c := c + d; c := irem(c, 26); if c < 0 then c := c + 26; end; T[i] := 97 + c; end; end; L := []; for i to l do L := [op(L),T[i]]; end; return convert(L,'bytes'); end:vigenere_cipher(p, "arthurdoyle");NiNRXnNJfmhuZmF+amliZ35mb2J+aWJ0bH5neWZ+Z3l+cmVzdH5kdGFnfnBvdn5mZnpkZWZjZXZlfmx5fm1+anlpaGV+anRpfmJyfml2aWl+b2t4dW5saGx+eGV0eH50dW9+amZyemVlZWlieH52d354Z35tbm52dWF6Z3hlfmRlbmNrdXZ2LjYi# HOMEWORK ### 1) # The Nero cipher exchanges a and z, b and y, c and x etc. # Write a procedure to encrypt using the Nero cipher. # Take your favourite text and encrypt it with # 7 Caeser, one Nero and 12 more Caesar ciphers. # What do you get? # How do you decipher it? # Can you decipher it using only one n-fold Caesar and one Nero step? # 2) re-write thefunction vigenere_decipher() to use an Array # (to make it work for long strings.) # Then decipher the following piece (the key is jane) # "Wnv, sv wkxt, vqn jwlz mgut, Mou. Lqgm utep mnxv Nbvakohbkif op mghgg ya g ahaki sxp uc egoix cqkzrpx cths vak phxqj uc Etdntta" # # 3) re-write the functions vigenere_cipher and vigenere_cipher # to also encrypt capital letters.# Use this function the encrypt the text of 2) with the key jane. TTdSMApJNlJUQUJMRV9TQVZFLzEzODI5MTI3NlgqJSlhbnl0aGluZ0c2IjYiW2dsISElISEhIl5zIl5zIiN0IiNLIiQvIiIjKCoiJD0iIiQsIkYoCiIkOiJGLEYsIiQ1IkYoIiQ7IiIkNiJGMEYoIiQ0IiIkPCIiIyoqRilGKEYuRjBGL0YoRi9GMEYoIiQyIkYuRjAiJD4iRihGL0YpRipGL0YKKEYvRilGLEYoIiQwIkYxIiQ3IiIkOSJGLEYtRi1GNkYwRi5GKEYwIiQtIkYoRipGKEY1RjBGMUYqRi5GKEYxRioiJEAiRigiIykqRixGKApGMUYwRjhGLEYoRitGKiIkMyJGMkYqRjtGPEYsRihGL0YpRipGLkYoRi9GKUYsRihGM0YwRi5GM0Y8RjJGLUY2RjBGLkYoRjBGOUYoRipGCi5GKEYqRi5GKkY8RjpGL0Y2RjNGKkY8RihGOEYsRipGLUYwRi5GLEY4IiNZRiYK