Sha256: 86263bde40f61d0b61f2f90ed5e5b447244c4779e8774abf7770493d1e298c46

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'digest/md5'
require 'base64'
require 'base64'
require 'digest'
require 'openssl'

module CryptojsAesDecrypt
  class Error < StandardError; end
  class << self
    def bytes_to_key(data, salt, output = 48)
      merged = data + salt
      key = Digest::MD5.digest(merged)
      final_key = key
      while final_key.length < output
        key = Digest::MD5.digest(key + merged)
        final_key += key
      end
      final_key[0..output - 1]
    end

    def aes256_cbc_decrypt(key, data, iv)
      # p key
      key = Digest::SHA256.digest(key) if key.is_a?(String) && key.bytesize != 32
      iv = Digest::MD5.digest(iv) if iv.is_a?(String) && iv.bytesize != 16
      aes = OpenSSL::Cipher.new('AES-256-CBC')
      aes.decrypt
      aes.key = key
      aes.iv = iv
      aes.update(data) + aes.final
    end

    def run(cipher, passcode)
      secret = Base64.decode64(cipher)
      unless secret[0..7] == 'Salted__'
        puts 'Invalid encrypted data'
        exit(1)
      end
      decrypt(secret, passcode)
    end

    def decrypt(secret, passcode)
      salt = secret[8..15]
      key_iv = bytes_to_key(passcode, salt, 48)
      key = key_iv[0..31]
      iv = key_iv[32..key_iv.length - 1]
      aes256_cbc_decrypt(key, secret[16..secret.length - 1], iv)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cryptojs-aes-decrypt-1.0.1 lib/decrypt.rb
cryptojs-aes-decrypt-1.0.0 lib/decrypt.rb