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 6194953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:23:00+00:00 2026-05-24T03:23:00+00:00

I have a site that needs to encrypt and store binary files that are

  • 0

I have a site that needs to encrypt and store binary files that are uploaded to the server. The uploading and storage works fine, but I’m getting this error when trying to write the encrypted file:

Encoding::UndefinedConversionError (“\xDD” from ASCII-8BIT to UTF-8):

The code that causes it looks like this:

fd_in = IO.sysopen(self[:name].tempfile.path, "rb")                           
file_in = IO.open(fd_in)                                                      
fd_out = IO.sysopen(self[:name].tempfile.path + ".encrypted", "wb")           
file_out = IO.open(fd_out)                                                    
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')                           
cipher.encrypt                                                                                                         
cipher.key = cipher_key                                                     
cipher.iv = cipher_iv                                                       
while chunk = file_in.read(1024)                                              
  file_out << cipher.update(chunk)                                            
end
file_out << cipher.final

The line that causes the error is the file_out << cipher.update(chunk) in the while loop. I’ve looked into this online and found some reports of similar ASCII/UTF conversion issues, but they all appear to be based on coercing string input, not stream file input. I’m using Ruby 1.9.2 which I believe affects default string encoding.

My rationale as to why (I think) I need to use a stream-based approach: the files tend to be large and I do not want to load the entire file (input or output) into memory to process it.

Any help is appreciated. Thanks.

  • 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-05-24T03:23:01+00:00Added an answer on May 24, 2026 at 3:23 am

    What you want to do when en-/decrypting is treat input and output as raw bytes, you want to avoid any transcoding caused by associating an encoding with your data at all cost. So you should open your files in binary mode, both for reading and for writing.

    Actually you did this, but with IO#sysopen, but then you did not pass the “b” flags when using IO#open.

    Your code should work if you rather try this:

    fin = File.open("TODO", "rb")                           
    fout = File.open("TODO.encrypted", "wb")           
    cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
    cipher.encrypt                     
    cipher.key = key                                                     
    cipher.iv = iv                                                       
    while chunk = fin.read(1024)                                              
      fout << cipher.update(chunk)                                            
    end
    fout << cipher.final
    fin.close
    fout.close 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a site that needs to (as part of our process) generate a
I have one site that is displaying html content that needs to be displayed
I have a Drupal site that needs to display a unique header image based
We have an ASP.Net MVC site that needs to authenticate to facebook to post
I have a site that's growing large, and has very separate needs for different
I have a site that uses a solr search engine, but I've only been
I have a site that requires Windows Authentication the application obtains the credential from
I have a site that is using x509 client certificates (2 way SSL) to
I have a site that usually has news items at the top of the
I have a site that I've developed that makes decent use of Javascript, and

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.