Jump to content

Bacon cipher: Difference between revisions

→‎{{header|Haskell}}: Applied hlint, hindent, inlined comments, added main.
(→‎{{header|Haskell}}: Applied hlint, hindent, inlined comments, added main.)
Line 938:
=={{header|Haskell}}==
 
<lang haskell>-- Necessary imports
<lang Haskell>import Data.List (findIndexelemIndex, unfoldr)
import Data.CharBool (isAlpha, isUpper, toUpper, toLowerbool)
import Data.BoolChar (boolisAlpha, isUpper, toLower, toUpper)</lang>
import Data.List.Split (chunksOf)
 
-- The list of characters to be encoded:
chars :: String
<lang Haskell>chars = ['a' .. 'z'] ++ ['0' .. '9'] ++ ",.;?! "
bitsPerChar = 6 :: Int</lang>
 
bitsPerChar = 6 :: Int</lang>
Some simple helper functions:
bitsPerChar = 6
<lang Haskell>toBinary :: Int -> [Bool]
 
toBinary = unfoldr (pure . (\(a,b)->(odd b,a)) . (`divMod` 2))
-- Some simple helper functions:
<lang Haskell>toBinary :: Int -> [Bool]
toBinary = unfoldr (pure . (\(a, b) -> (odd b, a)) . (`divMod` 2))
 
fromBinary :: [Bool] -> Int
fromBinary = foldr (\x n -> (2 * n) + bool 0 1 x) 0</lang>
 
-- And, finally, main functions -- encoding:
<lang Haskell>encode :: String -> String -> Either String String
encode message txt message = do
mask <- traverse coding message
zipAlphas (bool toLower toUpper) (concat mask) txt
where
coding ch = case findIndex (== ch) chars of
case elemIndex ch chars of
Nothing -> Left $ "Unknown symbol " ++ show ch
Just i Nothing -> RightLeft $ take"Unknown symbol " bitsPerChar++ (toBinaryshow i)ch
Just i -> Right $ take bitsPerChar (toBinary i)
 
zipAlphas f = go
where go _ [] = Left "Text is not long enough!"
go _ [] = goLeft []"Text _is =not Rightlong []enough!"
go (x:xs)[] (y:ys) | isAlpha y_ = (f x y :) <$> go xsRight ys[]
| otherwise = (y :) <$> go (x:xs) (y:ys</lang>)
| isAlpha y = (f x y :) <$> go xs ys
| otherwise = (y :) <$> go (x : xs) ys
 
-- And decoding:
<lang Haskell>decode :: String -> String
decode = map decipher . chunksOf bitsPerChar . filter isAlpha
where
decipher = (chars !!) . min (length chars - 1) . fromBinary . map isUpper
chunksOf n = takeWhile (not . null) . unfoldr (pure . splitAt n)</lang>
 
'''-- Examples'''
text :: String
text =
unwords
<lang Haskell>text = concat [ "Bacon's cipher is a method of steganography created by Francis Bacon. "
, ,"This task is to implement a program for encryption and decryption of "
, ,"plaintext using the simple alphabet of the Baconian cipher or some "
, ,"other kind of representation of this alphabet (make anything signify "
, ,"anything). The Baconian alphabet may optionally be extended to encode "
, ,"all lower case characters individually and/or adding a few punctuation "
, ,"characters such as the space." ]
]
 
message :: String
message = "the quick brown fox jumps over the lazy dog"</lang>
 
main :: IO ()
main = do
<lang Haskell>λ> let m = encode message text message
mapM_
(either
(putStrLn . ("-> " ++))
(putStrLn . (++ "\n") . unlines . fmap unwords . chunksOf 10 . words))
[ m
λ> , decode <$> m
λ> , encode text "something wrong @ in the message" text
, encode "abc" message
]</lang>
{{Out}}
<pre>BAcoN's CIPher is A metHod Of StegaNogrApHy creAted By franCiS
baCon. ThIS task iS to iMplEMEnt a PRoGrAm FOr eNcrYpTIoN
and dECRypTIOn Of PlaInTExt Using ThE simPLe aLPHAbet Of tHe
BacOnIaN CIphEr Or Some OtheR kinD oF rePrESEntAtION of thiS
alpHabEt (MAKe Anything sIgnIFy anyTHiNg). tHe BAConian ALPhabET may
 
'''Examples'''
 
Right "the quick brown fox jumps over the lazy dog"
<lang Haskell>text = concat ["Bacon's cipher is a method of steganography created by Francis Bacon. "
,"This task is to implement a program for encryption and decryption of "
,"plaintext using the simple alphabet of the Baconian cipher or some "
,"other kind of representation of this alphabet (make anything signify "
,"anything). The Baconian alphabet may optionally be extended to encode "
,"all lower case characters individually and/or adding a few punctuation "
,"characters such as the space." ]
 
message = "the quick brown fox jumps over the lazy dog"</lang>
 
Left-> "Unknown symbol '@'"
<lang Haskell>λ> let m = encode message text
Left-> "Text is not long enough!"</langpre>
λ> m
Right "BAcoN's CIPher is A metHod Of StegaNogrApHy creAted By franCiS baCon. ThIS task iS to iMplEMEnt a PRoGrAm FOr eNcrYpTIoN and dECRypTIOn Of PlaInTExt Using ThE simPLe aLPHAbet Of tHe BacOnIaN CIphEr Or Some OtheR kinD oF rePrESEntAtION of thiS alpHabEt (MAKe Anything sIgnIFy anyTHiNg). tHe BAConian ALPhabET may"
λ> decode <$> m
Right "the quick brown fox jumps over the lazy dog"
λ> encode "something wrong @ in the message" text
Left "Unknown symbol '@'"
λ> encode message "abc"
Left "Text is not long enough!"</lang>
 
=={{header|J}}==
9,655

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.