EG Information

Main Index
EG Manual
Disclaimer
Legal Information
Hall of Fame
Hall of Shame
Member Rankings
Members List
Meet the Staff
Hacker's Home Page

Training Missions

Read Me First
Basic Skills
Realistic Scenarios
Cryptography
Software Cracking
Linux ELF Binary Cracking
Logical Thinking
Programming
Captcha Cracking
Patching
Steganography
Deface This Wall
/dev/null
/dev/urandom
/dev/extra

Knowledge Bank

Discussion Forums
Exploit Database New
PasteBin New
RSS Feeds RSS
Articles / Tutorials
Videos
Online EG MP3 Player Radio
Downloads
Tools

Code Resources

Submit Code
Ajax
ASM
Bash
C
CPP
Csharp
Delphi
Haskell
Java
Javascript
Jython
Lisp
mIRC
MySQL
Perl
PHP
Python
QBASIC
VisualBasic

Pimp Us Out!

Review enigmagroup.org on alexa.com

Has Enigma Group Helped You? Then Help Us By Advertising For Us. Place One Of The Following Images On Your Site And Create A Link Back To Enigma Group.

Enigma Group

Enigma Group

Enigma Group

Enigma Group

 

Affiliates

hackhound.org

suck-o.com

hack.org.za

flyninja.net

 

Enigma Group's Code Bank


The Caesar Cipher

By: ParadoxMAI  -  Date Submitted: 2011-12-18 20:23:56

  1. import Data.Char
  2. import Data.List
  3. import Test.QuickCheck
  4.  
  5. import Data.Function
  6. import Data.Maybe
  7.  
  8.  
  9. rotate :: Int -> [Char] -> [Char]
  10. rotate k list | 0 <= k && k <= length list = drop k list ++ take k list
  11. | otherwise = error "Argument to rotate too large or too small"
  12.  
  13. prop_rotate :: Int -> String -> Bool
  14. prop_rotate k str = rotate (l - m) (rotate m str) == str
  15. where l = length str
  16. m = if l == 0 then 0 else k `mod` l
  17.  
  18. -- prop_rotate rotates a list of lenght l first an arbitrary number m times,
  19. -- and then rotates it l-m times; together (m + l - m = l) it rotates it all
  20. -- the way round, back to the original list
  21. --
  22. -- to avoid errors with 'rotate', m should be between 0 and l; to get m
  23. -- from a random number k we use k `mod` l (but then l can't be 0,
  24. -- since you can't divide by 0)
  25.  
  26.  
  27. alphabet = ['A'..'Z']
  28.  
  29. makeKey :: Int -> [(Char, Char)]
  30. makeKey k = zip alphabet (rotate k alphabet)
  31.  
  32. .
  33. lookUp :: Char -> [(Char, Char)] -> Char
  34. lookUp ch [] = ch
  35. lookUp ch ((key,val):restKey)
  36. | key == ch = val
  37. | otherwise = lookUp ch restKey
  38.  
  39. -- alternative solution with list-comprehension
  40. --
  41. -- lookUp ch xs = head ([ y | (x,y) <- xs, x == ch] ++ [ch])
  42.  
  43.  
  44. encipher :: Int -> Char -> Char
  45. encipher k ch = lookUp ch (makeKey k)
  46.  
  47.  
  48. normalize :: String -> String
  49. normalize [] = []
  50. normalize (ch:str)
  51. | isAlpha ch = toUpper ch : normalize str
  52. | isDigit ch = ch : normalize str
  53. | otherwise = normalize str
  54.  
  55.  
  56. encipherStr :: Int -> String -> String
  57. encipherStr k str = [encipher k ch | ch <- normalize str]
  58.  
  59.  
  60. reverseKey :: [(Char, Char)] -> [(Char, Char)]
  61. reverseKey key = [(b, a) | (a, b) <- key]
  62.  
  63.  
  64. decipher :: Int -> Char -> Char
  65. decipher k ch = lookUp ch (reverseKey (makeKey k))
  66.  
  67. decipherStr :: Int -> String -> String
  68. decipherStr k str = [decipher k ch | ch <- str]
  69.  
  70.  
  71. prop_cipher :: Int -> String -> Bool
  72. prop_cipher k str = decipherStr l (encipherStr l str) == normalize str
  73. where l = k `mod` 26
  74.  
  75.  
  76. contains :: String -> String -> Bool
  77. contains _ [] = True
  78. contains [] _ = False
  79. contains str substr =
  80. isPrefixOf substr str || contains (tail str) substr
  81.  
  82. .
  83. candidates :: String -> [(Int, String)]
  84. candidates str = [(i, decipherStr i str) | i <- [0..25], candidate (decipherStr i str)]
  85. where candidate str = str `contains` "AND" || str `contains` "THE"
  86.  
  87.  
  88. splitEachFive :: String -> [String]
  89. splitEachFive xs | length xs > 5 = take 5 xs : splitEachFive (drop 5 xs)
  90. | otherwise = [ fillToFive xs ]
  91.  
  92. fillToFive :: String -> String
  93. fillToFive xs = xs ++ replicate (5 - length xs) 'X'
  94.  
  95. -- An alternative solution demonstrating 'repeat'
  96. fillToFive' :: String -> String
  97. fillToFive' xs = take 5 (xs ++ repeat 'X')
  98.  
  99. prop_transpose :: String -> Bool
  100. prop_transpose xs = ys == transpose (transpose ys)
  101. where
  102. ys = splitEachFive xs
  103.  
  104. encrypt :: Int -> String -> String
  105. encrypt n str = concat (transpose (splitEachFive (encipherStr n str)))
  106.  
  107.  
  108.  
  109. splitFiveWays :: String -> [String]
  110. splitFiveWays xs | n `mod` 5 == 0 = splitEach (n `div` 5) xs
  111. | otherwise = error "splitFiveWays: not a multiple of 5"
  112. where n = length xs
  113.  
  114. splitEach :: Int -> String -> [String]
  115. splitEach _ [] = []
  116. splitEach n xs = take n xs : splitEach n (drop n xs)
  117.  
  118. decrypt :: Int -> String -> String
  119. decrypt n str = concat (transpose (splitFiveWays (decipherStr n str)))
  120.  
  121. -- Increment a frequency in a character frequency list.
  122. incAsc :: Char -> [(Char, Int)] -> [(Char, Int)]
  123. incAsc c [] = [(c,1)]
  124. incAsc c ((d,n) : xs) | c == d = (d, n+1) : xs
  125. | otherwise = (d, n) : incAsc c xs
  126.  
  127. countFreqs :: String -> [(Char, Int)]
  128. countFreqs xs = count xs []
  129. where count [] freqs = freqs
  130. count (x : xs) freqs = count xs (incAsc x freqs)
  131.  
  132. -- Rank the candidates which might decipher to 'E'
  133. freqCandidates :: [Char] -> [Int]
  134. freqCandidates xs = [ (ord c - ord 'E') `mod` 26 | (c, _) <- freqChars ]
  135. where freqChars = sortBy p (countFreqs xs)
  136. p (_,m) (_,n) = compare n m
  137.  
  138. freqDecipher :: String -> [String]
  139. freqDecipher xs = [ decrypt n xs | n <- freqCandidates xs ]
Return to haskell category list

Who Visited EnigmaGroup Today?

1387 Guests, 225 Users (218 Spiders)
lolzsec, tgm001, plex, Edika, TheCheeseDemon, rockcraft, Pabz, recoveryToolbox, saraf, soufiaane, sickmind, cat1vo, mjneat, famous0123, Galagatron, dark_void, CJ_Omaha, junaid_junaid59, JohnJohnJohn, ssmaslov, psychomarine, Dregoon, Patrickk, Aska, Beat_Slayer, M0rdak, Ausome1, Imre, Vreality2007, mmndglxuwn, m0rt, unholyblood, iterrumzz, VurbTrurb, Mayonoula, MAMWOURBROR, mutabor, gobinda, cossyDrybrich, Razin, zaCruBumas8, hunja, johny34, pantoufle, bagy, arctica, hackarchives, UsedDeteKef, Peculator, Fadhilat606, TheTrueMonarch, Pascall01, hackaday, Tjm, arndevil, flairvelocity, lol, alphbond, kdivanov, elizbethallis6, Rik, bn11, BorgBot, SHASHANK101hello, 4poc4lyptic, ksajxai, nbmorri1, electro-technic, شمالي عرعر, lamb, AutobotPrime, Underleaf, The End, tomtombomb, killobyte, snowgirlx, so_saucey, zerolife, Althor, Cramps, Hekser, Hyperborn, cyber-guard, jhgrunn, cobra, Partisan, MAZI_, cyborg, GenbreedX, moel77, cliptoX, pwnpwnlolz, letshavepie, Mrwormz, yshiau, mirmo, roozyoppomo, soft_devil, cls777, scoobywan, Reiversed, joshua, st3alth, Afrika, PaiffDryday, venter, Anthony12796, sh3llcod3, 8FIGURE, Rannim, Evil1, maloaboy, BACanON, SlayingDragons, Repuhlsive, IvanDimitriev, 1RiB, mzungudo, Micro_Geek, iMaxx, aciboummamymn, k0unterkulcher, somebody777, m14m16, GoododotAlcob, negasora, Rastii, UninueMem, Swifsolja, ad.conquest, ngolatkar, Infinity8, Jigoku, thesupervisor, p0is0n5ting, kernel_mod, AKL, GothicLogic, themastersinner, dnatrixene135, ChewBigRed, kalak55, sejem, cve916, pollolololo, triecturn, Violatedsmurf, Ops, jmp, xsiemich, generalisimo, strudels, ga3ttpom, KingOfBritains, epoch_qwert, suten, FriskyKat, Ryuske, Adonis Achilles, ubqbcdzzhf, 3vil, Nightraven, US£RNAM£, Weindittewcon, Batesheelocot, GSmyrlis, MaxMeier, Elite.America, rabbidmind, Psiber_Syn, phoenix22, imittyerrotte, peewster, cyberturtle, ctb, dexgeda, sdw, Pizza, White_widdow, devarian, finesse, Nature112091777, Danc7171, Alphadragon, Estadagause, 53QR10U5, Xargos, Alkomage, hardlock, Barry Gonzoles, MineDweller, Gkjt, N4g4c3N, [I]nfectedbug, wimsteege, aqr5zdcw, xin214, Bugshuppy, SnoopSky, Hessesian, voodooKobra, sKcarr, IROverRated, W1F1G3NJU75U, Baddy, ziadmosaan, gamble86, realzs, CruelDemon, Shinju, aVoid, aquiredanonymity, kukumumu, web_request, callmeneon, KissMyDAFFODIL, Feld Grau, Abhinav2107, prabhataditya, mbuyiselo, shumer, phenom216, princennamdi, huskyboiza, ninety-nine
 
Enigma Group