Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encrypt / decrypt binary file on disk #80

Open
elbarto132 opened this issue Jun 23, 2017 · 2 comments
Open

encrypt / decrypt binary file on disk #80

elbarto132 opened this issue Jun 23, 2017 · 2 comments

Comments

@elbarto132
Copy link

What's the recommended way to encrypt or decrypt large binary files which are stored on the disk? I couldn't find an example in the documentation

@reidmorrison
Copy link
Owner

It works the same as we would do in code to read or write large files, except using the SymmetricEncryption::Reader:

Create a test file:

SymmetricEncryption::Writer.open('a.txt.enc') {|f| f.write('Hellow world')}

Read from the source file in blocks so that the entire file is not loaded into memory:

SymmetricEncryption::Reader.open('a.txt.enc') do |input|
  File.open('a.txt', 'w') do |output|
    while !input.eof?
      output.write(input.read(65535))
    end
  end
end

There are also rake tasks for encrypting or decrypting files at the command line:
https://rocketjob.github.io/symmetric-encryption/rake_tasks.html

Symmetric Encryption v4 will replace the rake tasks with a simpler and more complete command line interface.

@elbarto132
Copy link
Author

elbarto132 commented Jun 29, 2017

Thanks for your reply
One note:
File.open('a.txt', 'w') do |output|
should be
File.open('a.txt', 'w', encoding: 'ASCII-8BIT') do |output|
or it will throw if you use a binary file

Encoding::UndefinedConversionError: "\xAF" from ASCII-8BIT to UTF-8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants