Skip to content

Instantly share code, notes, and snippets.

@kv109
Last active January 1, 2017 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kv109/42289aa65f81e819910005f4773215a1 to your computer and use it in GitHub Desktop.
Save kv109/42289aa65f81e819910005f4773215a1 to your computer and use it in GitHub Desktop.
ruby AES gem test
require 'aes'
# Obvious part
plain = "Very important message"
full_valid_password = "password"
encrypted = AES.encrypt(plain, full_valid_password)
decrypted = AES.decrypt(encrypted, full_valid_password)
plain == decrypted #=> true, obviously
# Now the sad part
invalid_passwords = [
"gassword", # Will decrypt anyway
"gsssword", # Will decrypt anyway!
"gsssssss", # Will decrypt anyway!!
"ssssssss", # Will decrypt anyway!!!
"totally wrong password", # Yes, will decrypt anyway!!!!
]
invalid_passwords.each do |wrong_password|
full_invalid_password = wrong_password
decrypted = AES.decrypt(encrypted, full_invalid_password) rescue(OpenSSL::Cipher::CipherError) || nil
puts "VALID PASSWORD: #{full_valid_password}"
puts "USED PASSWORD: #{full_invalid_password}"
puts "Did it work? #{plain == decrypted ? 'YES! Here is decrypted message: ' + decrypted : 'NO'}"
puts
end
# Output:
# VALID PASSWORD: password
# USED PASSWORD: gassword
# Did it work? YES! Here is decrypted message: Very important message
#
# VALID PASSWORD: password
# USED PASSWORD: gsssword
# Did it work? YES! Here is decrypted message: Very important message
#
# VALID PASSWORD: password
# USED PASSWORD: gsssssss
# Did it work? YES! Here is decrypted message: Very important message
#
# VALID PASSWORD: password
# USED PASSWORD: ssssssss
# Did it work? YES! Here is decrypted message: Very important message
#
# VALID PASSWORD: password
# USED PASSWORD: totally wrong password
# Did it work? YES! Here is decrypted message: Very important message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment