Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7728227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:39:56+00:00 2026-06-01T05:39:56+00:00

I am trying to write a program called encrypt that transforms a text file

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:39:57+00:00Added an answer on June 1, 2026 at 5:39 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that takes a large file (of any type)
I am trying to write a program that allows a binary to be run,
I am trying to write a program that displays the integers between 1 and
I'm trying to write a program in Java that uses osql to generate a
i am trying to write a program that close explorer then runs another program.
I am trying to write a program that has a vector of char arrays
I'm trying to write a program that can stop and start services using SilverLight
I'm trying to write text to a local file (i.e. on my laptop) using
I am trying to write a program that will output via coordinates. I am
I'm trying to compile a program (called es3), but, when I write from terminal:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.