问题
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.
回答1:
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
回答2:
Your code has some problems beyond what you're asking about. For instance, you're never actually applying the XOR operation to your input.
Try changing your final line to this (I also took the liberty of eradicating your extreme infestation of parentheses):
putStr . map chr $ zipWith xor (map ord txt) (cycle . map ord $ head arg1)
The key here is zipWith
, which zips two lists together using a combining function. zip
can be thought of as a special case of zipWith
(namely, zipWith (,)
).
You also must convert the String
s to Bits a => [a]
in order to XOR their contents. This is done via map ord
, which turns the String
into a [Int]
(Int
has an instance of Bits
). Then once the XORing is done, you convert the resulting [Int]
back to a String
with map chr
.
As far as decryption is concerned, you should be able to just pass the encrypted text back through the same program (using the same key, of course) to recover the original plaintext. XOR is cool like that.
回答3:
What I've understood from your comment is that you want an one time pad encryption algorithm. Here's the Vernam implementation
Prelude> let vernam p t = zipWith (\a b -> Data.Char.chr $ Data.Bits.xor (Data.Char.ord a) (Data.Char.ord b)) (cycle p) t
Prelude> vernam "XXAYSAAZZ" "The World is Not Enough"
"\f0$y\EOT.36>x12y\GS.5z\US674>;"
Prelude> vernam "XXAYSAAZZ" "\f0$y\EOT.36>x12y\GS.5z\US674>;"
"The World is Not Enough"
回答4:
Okay, if I understand this correctly, you're trying to do xor encryption here? Just FYI, I believe XOR only works on binary strings. I don't think there's some automatic duck typing that would make your plaintext respond as binary. You have to convert your string to binary first, then you can xor it with the key to encrypt it. To decrypt, just xor the key with the message text.
Look at the type signature for the function here.
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Bits.html
You can't just feed it plaintext.
来源:https://stackoverflow.com/questions/9931300/haskell-encryption-program