## RCM3720 Cryptography, Network and Computer Security

### Laboratory Class 2: Strings and Values

Characters and Strings

- All printable characters have a fixed ASCII value; some of which are:

            Character |  A   B   Y   Z   a   b   y   z
          ------------+-------------------------------
          ASCII Value | 65  66  89  90  97  98  121 122
                      |
            Character |  0   1   8   9   ,   -   .   /
          ------------+-------------------------------
          ASCII Value | 48  49  56  57  44  45  46  47

- To obtain values 0 to 25 for A to Z, we need to subtract 65 from the ASCII values.
- In Axiom, the `ord` command gives the ASCII value of a character. Create a string such as:
  - str:="THISISASTRING"
- A string can be turned into a list of characters using `members`:
  - members(str)
- This means a string can be turned into a list of ASCII values by mapping the `ord` function onto the list of members:
  - map(ord,members(str))
- To obtain values in the 0--25 range, try using an unnamed function:
  - strn:=map(x +-> ord(x)-65,members(str))
- Use this last command to create a function `str2lst` which will take a string (assumed to be of capital letters, with no spaces or punctuation), and return a list of values between 0 and 25.
- To go the other way, we first need to add 65 to all elements of `strn`:
  - map(x +-> x+65,strn)
- Turn this into characters with `char`:
  - map(char,%)
- These can be done as a single command:
  - map(x +-> char(x+65),strn)
- To put them all together as a single string we can concatenate them with the `concat` function from the `String` domain:
  - concat(%)$String
- In one line:
  - concat(map(x +-> char(x+65),strn))$String
- Alternatively, we could convert the characters to type `String` before concatenation:
  - concat(map(x +-> char(x+65)::String,strn))
- Use either version of this last command to create a function `lst2str` which will take a list of values between 0 and 25 and return a string.
- Create a text file in one of your private directories called `my3720.input` and copy your `str2lst` and `lst2str` functions to it.
- You can read command line input from a file with the extension `.input` using the `)read` command:
  - )read my3720
- The Caesar cipher can be implemented by the following three steps:
  1. Turn the string into a list,
  2. Add 3 to every number in the list,
  3. Turn this new list back into a string.
- To ensure that step (2) remains in the 0--25 range, we need to use the `rem` function. These can all be put together as:
  - caesar(str) == lst2str(map(x +-> (x+3) rem 26, str2lst(str)))
- Try this out on a few strings of your choice.
- By replacing the "`+3`" in the `caesar` function with "`+n`" create a new function called `trans(str,n)` which implements a general translation cipher.
- Test it out; these two commands should produce the same results.
  - caesar("MYSTRING")
  - trans("MYSTRING",3)
- If you like, add the `caesar` and `trans` functions to your `my3720.input` file.
- Test your `trans` function out on a few other strings and translation values.
- The `ROT13` cipher is used in Usenet postings to hide information which might be considered offensive. It is a translation cipher with a shift of 13. Since 13 is half of 26, this means that encrytion and decryption are exactly the same. Apply `ROT13` to:
  - GUVFVFNIRELFREVBHFOHFVARFF
- Consider this string which has been produced with a translation cipher. To decrypt it, simply apply all possible shifts until you obtain understandable text.
  - IUDTCUQBBOEKHCEDUO
- To apply all the possible shifts do:
  1. ct:="IUDTCUQBBOEKHCEDUO"
  2. for i in 1..26 repeat output trans(ct,i)
- What is the plaintext?