Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.