我最近开始使用haskell开发DES算法。我创建了两个函数des
和indes
,它们与DES类似,需要16个十六进制输入和16个十六进制密钥,然后分别加密和解密它们。我编写了一个函数来从文件中获取输入作为参数,并相应地应用所需的过程。我的加密过程运行良好,但解密过程显示错误Prelude.!!: index too large
我在函数dens
中给出了多行输入并且它有效。但是,每当我尝试在文件中写入它时,我都会给出相同的错误。我只附加重要代码,因为我假设函数des
和indes
工作正常。
denc :: String->String->String
denc [] _ = []
denc xs sx = if ((length xs)>=16) then (des (take 16 xs) sx ++ denc (drop 16 xs) sx) else (des (take 16 (xs ++ (repeat '0'))) sx)
dens :: String->[Char]->[Char]
dens [] _ = []
dens xs sx = (indes (take 16 xs) sx) ++ (dens (drop 16 xs) sx)
main :: IO ()
main = do
x:y:z:_ <- getArgs
handle <- openFile y ReadMode
contents <- hGetContents handle
if (z=="encrypt") then (do
hanle <- openFile "cipher.txt" WriteMode
let some = unlines $ unhex (denc (hex contents) (hex x))
hPutStrLn hanle some
hClose hanle
) else (do
hanlee <- openFile "decipher.txt" WriteMode
let some = unlines $ unhex (dens (hex contents) (hex x))
hPutStrLn hanlee some
hClose hanlee
)
hClose handle
答案 0 :(得分:1)
启用性能分析并使用RTS -xc选项将在发生异常时显示堆栈跟踪,这可能有助于找到问题:
ghc program.hs -prof -fprof-auto
program ...args... +RTS -xc