LMU ☀️ CMSI 662
SECURE SOFTWARE DEVELOPMENT
HOMEWORK #4 PARTIAL ANSWERS
  1. A code is a straight-up table that maps words to words or phrases to phrases. A cipher is an algorithmic process for producing a ciphertext from a message.
  2. Vigenere autokey, using the ciphertext as the rest of the key:
    message    : TAKEACOPYOFYOURPOLICYTONORMAWILCOXONTHETHIRDFLOOR
    key        : QUARKJUKVKLIZTYQGNNPFUYVRDNMIFUZMENFBABBSUHFUZCYI
    ciphertext : JUKVKLIZTYQGNNPFUYVRDNMIFUZMENFBABBSUHFUZCYIZKQMZ
    

    Here’s how I generated it:

    from string import ascii_uppercase as a
    
    message = "TAKEACOPYOFYOURPOLICYTONORMAWILCOXONTHETHIRDFLOOR"
    key = "QUARK"
    ciphertext = ""
    for i in range(0, len(message)):
        c = a[(a.index(message[i]) + a.index(key[i])) % 26]
        ciphertext += c
        key += c
    print(ciphertext)
    
  3. You had to write the program for this one. Here’s mine:

    cryptoexample.mjs
    import { createCipheriv, createDecipheriv } from 'crypto'
    
    function encrypt(data, key, iv) {
      const cipher = createCipheriv('aes-256-cbc', key, iv);
      return cipher.update(data, 'utf-8', 'hex') + cipher.final('hex');
    }
    
    function decrypt(data, key, iv) {
      const cipher = createDecipheriv('aes-256-cbc', key, iv);
      return cipher.update(data, 'hex', 'utf-8') + cipher.final('utf-8');
    }
    
    if (process.argv.length !== 6) {
      console.error(`Syntax: node cryptoexample.mjs (-e|-d) message-or-hexstring key iv
        where:
          -e means decrypt
          -d means encrypt
        If -e, the next argument must be text, and the program will respond with a hex string
        If -d, the next argument must be a hex string, and the program will respond with text`);
    } else if (!['-d', '-e'].includes(process.argv[2])) {
      console.error('First argument must be -d or -e');
    } else if (process.argv[2] === '-e') {
      console.log(encrypt(process.argv[3], process.argv[4], process.argv[5]));
    } else {
      console.log(decrypt(process.argv[3], process.argv[4], process.argv[5]));
    }
    
  4. Here’s the script I used to figure this out:
    p = 100392089237316158323570985008687907853269981005640569039457584007913129640081
    q = 90392089237316158323570985008687907853269981005640569039457584007913129640041
    n = p * q
    e = 65537
    d = pow(e, -1, (p-1)*(q-1))
    m = "Scaramouche, Scaramouche, will you do the Fandango? 💃🏽"
    m = int(''.join(f'{x:02x}' for x in bytearray(m, "utf8")), 16)
    c = pow(m, e, n)
    print(f"N  = {n}")
    print(f"d  = {d}")
    print(f"m  = {m:x}")
    print(f"c  = {c:x}")
    print(f"m' = {pow(c, d, n):x}")
    

    Result:

    N  = 9074650689060089248199307400991055468098862103321403389047443270291029585149437412899788230307477461518354291801534660904940424431810965948411931416083321
    d  = 3440604854078842449902442746842634638010902628184540510109569714515334896803156693630578151012041316815494692897384029619755303913249828307540510260406273
    m  = 53636172616d6f756368652c2053636172616d6f756368652c2077696c6c20796f7520646f207468652046616e64616e676f3f20f09f9283f09f8fbd
    c  = 1510726ec4756e595c4b5ce1f3a1974798a34369eb8f43f7462d4093f30973994849a5b63d6b28e33c2200bfea7f7005bd7642e74302832b739be60d966a926b
    m' = 53636172616d6f756368652c2053636172616d6f756368652c2077696c6c20796f7520646f207468652046616e64616e676f3f20f09f9283f09f8fbd
    
  5. Quickest way to get this answer is to use Python
    $ import hashlib
    $ hashlib.sha384(bytes("Російський військовий корабель, іди нахуй", "utf-8")).hexdigest()
    'c358ff602ada470dfb85fad41bd1fe277d587ace98d09c7eb70f48ef1048b76a2ec1103f67d54871cd18046cbd6fe816'