symmetric-encryption icon indicating copy to clipboard operation
symmetric-encryption copied to clipboard

encrypt / decrypt binary file on disk

Open elbarto132 opened this issue 8 years ago • 2 comments

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

elbarto132 avatar Jun 23 '17 20:06 elbarto132

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.

reidmorrison avatar Jun 26 '17 17:06 reidmorrison

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

elbarto132 avatar Jun 29 '17 15:06 elbarto132