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

  • SEARCH
  • Home
  • 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 7787177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:34:49+00:00 2026-06-01T20:34:49+00:00

Here’s what I’m trying to do in Haskell: take a message in ByteString format

  • 0

Here’s what I’m trying to do in Haskell:

  • take a message in ByteString format (doesn’t really matter if lazy or strict)
  • encrypt the message with an RSA public key
  • base64 encode the encrypted message

The RSA library that I’m using handles lazy ByteStrings internally. The Base64 library, however, uses strict ByteStrings only. My application uses lazy ByteStrings to send the messages to network sockets.

So, it looks like I have to convert between lazy and strict ByteStrings.
Here’s what I do:

encrypt :: CryptoRandomGen t => t -> RSA.PublicKey -> L.ByteString -> L.ByteString
encrypt gen pubkey msg = do
  let (ciphertext,_) = RSA.encrypt gen pubkey msg
  (L.fromChunks . map encode . L.toChunks) $ ciphertext

decrypt :: RSA.PrivateKey -> L.ByteString -> Either String L.ByteString
decrypt privkey ciphertext = do
  dec <- decode $ S.concat $ L.toChunks ciphertext
  return $ RSA.decrypt privkey $ L.fromChunks [dec]

Unfortunately, sometimes this fails. When I decrypt a message encrypted in this way it sometimes results in garbage followed by the actual message. I’m not sure exactly where the problem is: is it the conversion from lazy to strict ByteStrings or is it the base64 encoding step? Or is it both?

Lazy ByteStrings are just lists of strict ByteString chunks. Do I implicitly modify the length of the message by converting it?

Please enlighten me.

  • 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-01T20:34:50+00:00Added an answer on June 1, 2026 at 8:34 pm

    The problem is that base64 encoding maps every three bytes (3 × 8 bits) of input to four bytes (4 × 6 bits) of output, so when the size of the input is not a multiple of three, it has to add padding. This means that concatenating the result of encoding each chunk separately may not give the same result as encoding the entire thing.

    > encode "Haskell"
    "SGFza2VsbA=="
    > encode "Hask" `append` encode "ell"
    "SGFzaw==ZWxs"
    

    Note that these are different even if you remove the = characters used to pad the output. The padding of the input will still cause problems.

    Your best bet is probably to find a library that supports lazy bytestrings, but ensuring that the sizes of all chunks (except the last) are multiples of three can work as a workaround.

    Alternatively, if you don’t mind keeping the whole thing in memory, convert the lazy bytestring to a strict one, encode the whole thing in one step, and convert back (if necessary).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here's the flow that I am trying to achieve: 1) User uploads an audio
Here is what I am trying to achieve in PHP: I have this string:
Here's what I'm trying to do... It's quite simple and obviously there must be
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here is my problem...I have a page that loads a list of clients and
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY
Here is the situation, I am attempting to fire a set of Gallio tests
Here is binding: <Image Width=16 Height=16 Source={Binding SwitchForImage, Converter={StaticResource stringToImage}} HorizontalAlignment=Left> </Image> and here

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.