如何使用terminal openssl命令解密OpenSSL Ruby加密文件?

时间:2016-10-11 09:00:53

标签: ruby encryption openssl

我在OpenSSL的帮助下使用AES-256-CBC加密文件:

# Encrypt a file with openSSL
require 'openssl'

class Encryptor

  def initialize
    @src_file = 'test.txt'
    @dst_file = 'test.enc'
    @key = "1234567890ABCDEF1234567890ABCDEF"
    @iv  = "1234567890ABCDEF"
    @algorithm = 'AES-256-CBC'
  end

  #
  def encrypt
    cipher = OpenSSL::Cipher.new(@algorithm)
    cipher.encrypt
    cipher.key = @key
    cipher.iv = @iv
    buffer = ""
    File.open(@dst_file,'wb') do |outf|
      File.open(@src_file, 'rb') do |inf|
        while inf.read(4096, buffer)
          outf << cipher.update(buffer)
        end
        outf << cipher.final
      end
    end
  rescue Exception => e
    "#{e}"
  end

end

enc = Encryptor.new
enc.encrypt

然后我尝试使用终端解密它:

openssl enc -aes-256-cbc -d -in test.enc -K "1234567890ABCDEF1234567890ABCDEF" -iv "1234567890ABCDEF" -out test_dec.txt

我得到的是一个错误:

bad decrypt
29152:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64/src/crypto/evp/evp_enc.c:330:

主要称为&#34; bad_decrypt &#34;。

我试过&amp;没有iv,将-k更改为-K,使用-a,首先使用base64解密,当然没有任何效果。任何想法如何解决这个问题?

0 个答案:

没有答案