Skip to content

Instantly share code, notes, and snippets.

@angstbear
Created November 16, 2021 19:16
Show Gist options
  • Save angstbear/2bee928c3559ccbb2f2d61925e8b9263 to your computer and use it in GitHub Desktop.
Save angstbear/2bee928c3559ccbb2f2d61925e8b9263 to your computer and use it in GitHub Desktop.
AES Decryption logic for implicit IVs
require "openssl"
require "digest"
require "base64"
passphrase = '<key>'
encrypted_data = '<base64 encoded value>'
def derive_key_and_iv(passphrase, salt)
dx = di = ""
enc_pass = passphrase.bytes.pack('c*')
for _ in 1...4
di = Digest::MD5.digest(di + enc_pass + salt)
dx += di
end
return dx[0..31], dx[32..47]
end
data = Base64.strict_decode64(encrypted_data)
salted = data[0..7]
if salted != "Salted__"
raise "Invalid data"
end
salt = data[8..15]
crypted = data[16..-1]
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.decrypt
cipher.key, cipher.iv = derive_key_and_iv(passphrase, salt)
decrypted = cipher.update(crypted) + cipher.final
puts decrypted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment