Skip to content

Instantly share code, notes, and snippets.

@toshi0383
Last active January 7, 2017 08:56
Show Gist options
  • Save toshi0383/0bd8a8b072b5e5e19cc7d8bb7475068b to your computer and use it in GitHub Desktop.
Save toshi0383/0bd8a8b072b5e5e19cc7d8bb7475068b to your computer and use it in GitHub Desktop.
{-|
- Cryptography Week 1 Problem Set #7 | Corsera
-
- 1. Extract One-Time-Pad key from PT and CT pair.
- 2. Encrypt a new message using the key.
- 3. Print the result as a Hex String.
-}
import Text.Printf (printf)
import Data.Bits (xor)
import Data.Char (ord, chr)
import Numeric (showHex)
extractKey :: [Int] -> [Int] -> [Int]
extractKey = zipWith xor
encrypt :: [Int] -> [Int] -> [Int]
encrypt = zipWith xor
fixedHexString :: Int -> String
fixedHexString x = printf "%02X" x :: String
showHexString = map fixedHexString
main=do print $ concat . showHexString $ encrypt key m
where
-- original plain text message
om = map ord "attack at dawn"
-- original cipher text in hex
oc = [0x6c, 0x73, 0xd5, 0x24, 0x0a, 0x94, 0x8c, 0x86, 0x98, 0x1b, 0xc2, 0x94, 0x81, 0x4d]
-- extracted otp key
key = extractKey om oc
-- new plain text message
m = map ord "attack at dusk"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment