Subato

Resource Files

RSA

Studieren Sie das Aufgaben-Papier zum RSA-Kryptosystem und lösen Sie die dort enthaltenen Aufgaben.


> module RSA where > import Data.Char > import Data.List > import Data.Maybe > import Math.NumberTheory.Primes > import System.Random > decryptExample msg = 0 > type Key = (Integer,Integer) > type KeyPair = (Key,Key) > publicPrivateFor :: Integer -> Integer -> Integer -> Maybe KeyPair > publicPrivateFor e p q = do > s <- multiplicativInverse e ((p-1)*(q-1)) > return (let n = p*q in ((e,n),(s ,n))) > publicPrivate = publicPrivateFor e > e = (2^17+1) > gcdExt :: Integer -> Integer -> (Integer,Integer,Integer) > gcdExt a b = (1, 1, 1) > multiplicativInverse a b = mkPos$gcdExt a b > where > mkPos (1,x,_) > | x < 0 = Just (x + b) > | otherwise = Just x > mkPos (_,_,_) = Nothing > p = 0x19fbd41d69aa3d86009a967db3379c63cd501f24f7 > q = 0x1b6f141f98eeb619bc0360220160a5f75ea07cdf1d > pow x y z = x^y `mod` z > encrypt (public ,n) msg = pow msg public n > decrypt = encrypt > primes = sieb [2..] > where > sieb (x:xs) = x:sieb [y|y<-xs,y `mod` x /= 0] > genPrime :: Int -> IO Integer > genPrime bits = do > x <- randomRIO (2^(bits - 1), 2^bits - 1) > if isPrime x then return x else genPrime bits > genPrimes :: Int -> IO (Integer, Integer) > genPrimes bits = do > p1 <- genPrime bits > genPrim2 p1 > where > genPrim2 p1 = do > p2 <- genPrime bits > if p1 /= p2 then return (p1, p2) else genPrim2 p1 > genKey = do > (p1,p2) <- genPrimes 1024 > let ks = publicPrivate p1 p2 > if isNothing ks then genKey else return (fromJust ks) > testRSA1 = do > (pub,priv) <- genKey > let enc = encrypt pub > 123456789009876543211111222233334444555566667777888899990000 > print (decrypt priv enc) > unicodesize = 2^16 > stringToInteger :: String -> Integer > stringToInteger xs = 1 > integerToString :: Integer -> String > integerToString n = "" > verschlüssel key = (encrypt key).stringToInteger > entschlüssel key = integerToString.(decrypt key) > test = do > (pub,priv) <- genKey > let enc = verschlüssel pub "hallo welt jetzt mal ein richtig langer text das wird doch wohl gut gehen" > print (entschlüssel priv enc) > splitString n str = [] > encryptFile key inFile outFile = do > content <- readFile inFile > writeFile outFile $ show $map (verschlüssel key) $ splitString 100 content > decryptFile key inFile outFile = do > content <- readFile inFile > writeFile outFile $ concat $map (entschlüssel key) (read content)
lhs
You are not logged in and therefore you cannot submit a solution.