Haskell加密程序 [英] Haskell Encryption Program

查看:270
本文介绍了Haskell加密程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个名为encrypt的程序,通过加扰每个字符来转换文本文件。这是通过提供一个键(字符串)作为用于编码输入的命令行参数来实现的。因此:



cat txtfile.txt | ./encrypt XXAYSAAZZ 将通过将文本与密钥无限匹配来加密文本。

  .. 
XXA ..
世界...
XXAYSAAZZ等..
世界还不够...
XXAYSAAZZXXAYSAAZZXXAYS等...

And exclusive-or-ing字符。但是由于 XOR 的性质,我应该能够保留原始文本:

  cat txtfile.txt | ./encrypt XXAYSAAZZ |到目前为止,我已经得到了:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b txt< - getContents
putStr((snd(unzip(zip(txt)(cycle(head arg1))))))

xor 功能位于 Data.Bits 中。



我已经尝试使用 xor 多次,但我不知道如何解密文本。



请提供一些想法。 >

所以如果文本是:

  cat txtfile.txt | ./encrypt XXAYSAAZZ 

Hello World,Goodbye World它应该用KEY (XXAYSAAZZ)无限地替换文本,即



XXAYSAAZZXXAYSAAZZXXAYSAAZ cat txtfile.txt | ./encrypt XXAYSAAZZ | ./encrypt XXAYSAAZZ 应该回覆:

 Hello World,Goodbye World

如果您调用两次,则返回原始字符串。

解决方案

这个

  putStr((snd(unzip(zip(txt)头arg1)))))))

只是用循环键的同样长的一部分替换文本。因为除了长度以外的所有信息都已经丢失,所以不可能获取文本。



似乎加密是为了 xor 具有配对键字符的每个文本字符(循环键以获得足够的长度)。



对于这样的方案, zipWith 功能,

  zipWith ::(a  - > b  - > c) - > [a]  - > [b]  - > [c] 

这里,

 让cypher = zipWith xor文本(循环键)

但是你应该检查密钥是否为空,否则将无法正常工作。但是,还有另一个问题需要解决,没有

 实例位Char Char 

定义(从目前为止具有 Num - 这是有待删除 - 不可以)。因此,您需要将密钥和明文转换为具有 Bits 实例的类型,最简单的方法是使用枚举实例 Char

  main = do 
(key: _)< - getArgs
如果空键
那么错误加密密钥必须是非空
else do
let xkey = map fromEnum(循环密钥)
plain < - getContents
let cypher = map toEnum(zipWith xor xkey $ map fromEnum plain)
putStr cypher


I am trying to write a program called encrypt that transforms a text file by scrambling each of its characters. This is achieved by providing a key (string) as a command line argument which is used to encode the input. Thus:

cat txtfile.txt | ./encrypt XXAYSAAZZ will encrypt the text by matching up the text with the key infinitely.

The..
XXA..
The World...
XXAYSAAZZ etc..
The World is Not Enough etc...
XXAYSAAZZXXAYSAAZZXXAYS etc...

And "exclusively-or-ing" the corresponding characters. But due to the nature of XOR I am supposed to be able to retain the original text by doing this:

cat txtfile.txt | ./encrypt XXAYSAAZZ | ./scramble XXAYSAAZZ

So far I have got this:

module Main where
import System
import Data.Char

main = do arg1 <- getArgs  
       txt  <- getContents 
       putStr((snd (unzip (zip (txt) (cycle(head arg1))))))

The xor function is located in Data.Bits.

I have tried using xor many times, but I am not sure how I would decrypt the text.

Please give some ideas.

So if the text was:

cat txtfile.txt | ./encrypt XXAYSAAZZ

"Hello World, Goodbye World" it should replace the text with the KEY (XXAYSAAZZ) infinitely i.e.

"XXAYSAAZZXXAYSAAZZXXAYSAAZ" and cat txtfile.txt | ./encrypt XXAYSAAZZ | ./encrypt XXAYSAAZZ should give back:

"Hello World, Goodbye World" 

If you call it twice it returns the original string.

解决方案

This

putStr((snd (unzip (zip (txt) (cycle(head arg1))))))

just replaces the text with an equally long part of the cycled key. It is impossible to retrieve the text from that, since all information except its length has been lost.

It seems that the encryption is intended to xor each text character with the paired key character (cycling the key to get sufficient length).

For such a scheme, the zipWith function is intended,

zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]

here,

let cypher = zipWith xor text (cycle key)

But you should check that the key is nonempty, or it won't work. However, there's another problem you have to solve, there is no

instance Bits Char where

defined (and since so far Bits has a superclass constraint of Num - that is slated to be removed - there can't be). So you need to transform your key and plaintext to a type with a Bits instance, the easiest way is using the Enum instance of Char

main = do
  (key:_) <- getArgs
  if null key
     then error "encryption key must be nonempty"
     else do
       let xkey = map fromEnum (cycle key)
       plain <- getContents
       let cypher = map toEnum (zipWith xor xkey $ map fromEnum plain)
       putStr cypher

这篇关于Haskell加密程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆