简体   繁体   中英

How do I decrypt files that are encrypted using des command in Ruby?

I need to decrypt files that are encrypted with this command:

des -E -u -k "some key" file.in file.out.enc

The decryption code in Ruby:

def decrypt(key)
  cipher = OpenSSL::Cipher.new(‘des’).decrypt
  cipher.key = key

  File.open(‘file.out’, ‘wb’) do |outf|
    decrypted = cipher.update(File.read(‘file.in.enc’)) + cipher.final
    outf.write(decrypted)
  end
end

I'm getting wrong final block length error when I run the code above. I also tried decrypting using the openssl command line tool and got a bad magic number error. Any advice?

Try switching the mode, from CBC to ECB for instance with OpenSSL::Cipher.new('DES-ECB') .

If you check which ciphers your Ruby installation supports by looking at OpenSSL::Cipher.ciphers , you'll find a list of available modes too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM