Created
April 15, 2016 06:13
-
-
Save trevordixon/b660f530aae5238d867e927db857b251 to your computer and use it in GitHub Desktop.
CS 465 code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- AES encryption in Haskell using Simple AES (http://hackage.haskell.org/package/SimpleAES-0.4.2) | |
-- Example encryption and decryption: | |
import Codec.Crypto.SimpleAES | |
import qualified Data.ByteString.Char8 as B | |
import qualified Data.ByteString.Lazy.Char8 as BL | |
import Data.Hex | |
key = B.pack "Thisismykey....." | |
iv = B.pack "Thisismyiv......" | |
msg = BL.pack "GGGGGGGGGGGGGGG\nGGGGGGGGGGGGGGG\nGGGGGGGGGGGGGGG\nGGGGGGGGGGGGGGG\nAAAAAAAAAAAAAAA\nC" | |
main = do | |
-- Encrpyt using CBC and ECB | |
let cbc = encryptMsg' CBC key iv msg | |
ecb = encryptMsg' ECB key iv msg | |
-- Print ciphertext | |
putStrLn $ hex (BL.unpack cbc) | |
putStrLn $ hex (BL.unpack ecb) | |
-- Decrpyt CBC and ECB ciphertext | |
let m = decryptMsg CBC key cbc | |
m' = decryptMsg ECB key ecb | |
-- Print decrypted messages | |
putStrLn $ BL.unpack m | |
putStrLn $ BL.unpack m' | |
-- CBC mode output | |
----------------------------------------------------------------------------------- | |
-- 5468697369736D7969762E2E2E2E2E2E00000000000000512596118FB3E8456843ECF018CEFF9231 | |
-- 1B7C62461589985FD17F07E7CD538E00CB9C7EBB00C210C062553E58DE80E890F64D400790C6A41B | |
-- DB48CD4FA1C2F50BF83C4278B5A2DD18C703998CF355C8370D763E81394651243640A4F0B28FC6FE | |
-- ECB mode output | |
----------------------------------------------------------------------------------- | |
-- 5468697369736D7969762E2E2E2E2E2E0000000000000051B2FC545513D62A393F524B6714B6FFA1 | |
-- B2FC545513D62A393F524B6714B6FFA1B2FC545513D62A393F524B6714B6FFA1B2FC545513D62A39 | |
-- 3F524B6714B6FFA179072EED75C17B034EAAED887AFC556D7F0851F58EE2882CFFE193F579E60D04 | |
-- Lessons learned | |
----------------------------------------------------------------------------------- | |
-- Padding must be addressed carefully. Somehow, the length of the plaintext must | |
-- be encoded so the right amount of padding can be stripped after decryption. | |
-- | |
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- AES encryption in Haskell using Simple AES (http://hackage.haskell.org/package/SimpleAES-0.4.2) | |
-- Example encryption and decryption: | |
import Crypto.Cipher | |
import Crypto.Cipher.Types | |
import qualified Data.ByteString.Char8 as B | |
import Data.Hex | |
Right key = makeKey (B.pack "Thisismykey.....") | |
cipher :: AES128 | |
cipher = cipherInit key | |
just iv = makeIV (B.pack "Thisismyiv......") | |
msg = B.pack "GGGGGGGGGGGGGGG\nGGGGGGGGGGGGGGG\nGGGGGGGGGGGGGGG\nGGGGGGGGGGGGGGG\nAAAAAAAAAAAAAAAC" | |
main = do | |
-- Encrpyt using CBC and ECB | |
let cbc = cbcEncrypt cipher iv msg | |
ecb = ecbEncrypt cipher msg | |
-- Print ciphertext | |
--putStrLn $ hex (BL.unpack cbc) | |
putStrLn $ hex (B.unpack ecb) | |
-- Decrpyt CBC and ECB ciphertext | |
--let m = decryptMsg CBC key cbc | |
--m' = decryptMsg ECB key ecb | |
-- Print decrypted messages | |
--putStrLn $ BL.unpack m | |
--putStrLn $ BL.unpack m' | |
-- CBC mode output | |
----------------------------------------------------------------------------------- | |
-- 5468697369736D7969762E2E2E2E2E2E00000000000000512596118FB3E8456843ECF018CEFF9231 | |
-- 1B7C62461589985FD17F07E7CD538E00CB9C7EBB00C210C062553E58DE80E890F64D400790C6A41B | |
-- DB48CD4FA1C2F50BF83C4278B5A2DD18C703998CF355C8370D763E81394651243640A4F0B28FC6FE | |
-- ECB mode output | |
----------------------------------------------------------------------------------- | |
-- 5468697369736D7969762E2E2E2E2E2E0000000000000051B2FC545513D62A393F524B6714B6FFA1 | |
-- B2FC545513D62A393F524B6714B6FFA1B2FC545513D62A393F524B6714B6FFA1B2FC545513D62A39 | |
-- 3F524B6714B6FFA179072EED75C17B034EAAED887AFC556D7F0851F58EE2882CFFE193F579E60D04 | |
-- Lessons learned | |
----------------------------------------------------------------------------------- | |
-- Padding must be addressed carefully. Somehow, the length of the plaintext must | |
-- be encoded so the right amount of padding can be stripped after decryption. | |
-- | |
-- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Bits | |
import System.Random | |
import Math.NumberTheory.Primes.Testing | |
modExp :: Integer -> Integer -> Integer -> Integer | |
modExp b 0 m = 1 | |
modExp b e m = t * modExp ((b * b) `mod` m) (shiftR e 1) m `mod` m | |
where t = if testBit e 0 then b `mod` m else 1 | |
rndPrime :: Int -> IO Integer | |
rndPrime bits = do | |
x <- fmap (.|. 1) $ randomRIO (2^(bits - 1), 2^bits - 1) | |
if isPrime x then return x else rndPrime bits | |
main :: IO () | |
main = do | |
p <- rndPrime 500 | |
s <- rndPrime 500 | |
putStrLn "p:" | |
putStrLn $ show p | |
putStrLn "g^s%p:" | |
putStrLn $ show $ modExp 5 s p | |
putStrLn "g^t&p:" | |
gtp <- getLine | |
putStrLn "(g^t%p)^s%p:" | |
putStrLn $ show $ modExp (read gtp :: Integer) s p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Bits | |
import System.Random | |
import Math.NumberTheory.Primes.Testing | |
modExp :: Integer -> Integer -> Integer -> Integer | |
modExp b 0 m = 1 | |
modExp b e m = t * modExp ((b * b) `mod` m) (shiftR e 1) m `mod` m | |
where t = if testBit e 0 then b `mod` m else 1 | |
extendedGCD :: Integer -> Integer -> (Integer, Integer) | |
extendedGCD a 0 = (1, 0) | |
extendedGCD a b = (t, s - q * t) | |
where (q, r) = quotRem a b | |
(s, t) = extendedGCD b r | |
modInv :: Integer -> Integer -> Integer | |
modInv a b = let i = fst (extendedGCD a b) | |
in if i < 0 then (b+i) else i | |
gcd :: Integer -> Integer -> Integer | |
gcd a b = a*x + b*y | |
where (x, y) = extendedGCD a b | |
rndPrime :: Int -> IO Integer | |
rndPrime bits = do | |
x <- fmap (.|. 1) $ randomRIO (2^(bits - 1), 2^bits - 1) | |
if isPrime x then return x else rndPrime bits | |
rsaPrimes :: IO (Integer, Integer) | |
rsaPrimes = do | |
p <- rndPrime 512 | |
q <- rndPrime 512 | |
if Main.gcd ((p-1) * (q-1)) 65537 == 1 then return (p, q) else rsaPrimes | |
main :: IO () | |
main = do | |
(p, q) <- rsaPrimes | |
let n = p * q | |
phi = (p-1) * (q-1) | |
e = 65537 | |
d = modInv e phi | |
putStrLn "p:" | |
putStrLn (show p) | |
putStrLn "" | |
putStrLn "q:" | |
putStrLn (show q) | |
putStrLn "" | |
putStrLn "n:" | |
putStrLn (show n) | |
putStrLn "" | |
putStrLn "d:" | |
putStrLn (show d) | |
putStrLn "" | |
putStrLn "Enter message:" | |
m <- getLine | |
putStrLn "" | |
putStrLn "Encrypted:" | |
putStrLn $ show (modExp (read m :: Integer) e n) | |
putStrLn "" | |
putStrLn "Enter encrypted message:" | |
c <- getLine | |
putStrLn "" | |
putStrLn "Decrypted:" | |
putStrLn $ show (modExp (read c :: Integer) d n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment