lib/AppleDecrypter.rb
def aes_gcm_decrypt(key, iv, tag, encrypted_data, debug_text=nil)
    plaintext = nil

      begin
        decrypter = OpenSSL::Cipher.new('aes-128-gcm').decrypt
        decrypter.key = key
        decrypter.iv_len = iv.length # Just in case the IV isn't 16-bytes
        decrypter.iv = iv
        decrypter.auth_tag = tag
        plaintext = decrypter.update(encrypted_data) + decrypter.final
      rescue OpenSSL::Cipher::CipherError
        puts "Failed to decrypt #{debug_text}, unwrapped key likely isn't right."
        @logger.error("Apple Decrypter: #{debug_text} caught a CipherError while trying final decrypt, likely the unwrapped key is not correct.")
      end

    return plaintext
  end
## 
# This function performs the AES-GCM decryption. It expects a +key+ as a binary String, an +iv+ as a binary
# String, a +tag+ as a binary String, the +encrypted_data+ as a binary String, and optional +debug_text+ 
# if you want something helpful to hunt in the debug logs. It sets the iv length explicitly to the length of 
# the iv and returns either nil or the plaintext if there was a decrypt. It rescues an OpenSSL::Cipher::CipherError 
# and logs the issue. 
This function performs the AES-GCM decryption.
This function performs the AES - GCM decryption .
def aes_gcm_decrypt(key, iv, tag, encrypted_data, debug_text=nil)
    plaintext = nil
      begin
        decrypter = OpenSSL::Cipher.new('aes-128-gcm').decrypt
        decrypter.key = key
        decrypter.iv_len = iv.length 
        decrypter.iv = iv
        decrypter.auth_tag = tag
        plaintext = decrypter.update(encrypted_data) + decrypter.final
      rescue OpenSSL::Cipher::CipherError
        puts "Failed to decrypt #{debug_text}, unwrapped key likely isn't right."
        @logger.error("Apple Decrypter: #{debug_text} caught a CipherError while trying final decrypt, likely the unwrapped key is not correct.")
      end
    return plaintext
  end
def aes_gcm_decrypt ( key , iv , tag , encrypted_data , debug_text = nil ) plaintext = nil begin decrypter = OpenSSL :: Cipher . new ( 'aes-128-gcm' ) . decrypt decrypter . key = key decrypter . iv_len = iv . length decrypter . iv = iv decrypter . auth_tag = tag plaintext = decrypter . update ( encrypted_data ) + decrypter . final rescue OpenSSL :: Cipher :: CipherError puts "Failed to decrypt #{debug_text}, unwrapped key likely isn't right." @logger . error ( "Apple Decrypter: #{debug_text} caught a CipherError while trying final decrypt, likely the unwrapped key is not correct." ) end return plaintext end
This function performs the AES-GCM decryption.
This function performs the AES - GCM decryption .
# Just in case the IV isn't 16-bytes
key
iv
tag
encrypted_data
debug_text