Skip to content

Instantly share code, notes, and snippets.

@bkerley
Last active December 10, 2015 02:58
Show Gist options
  • Save bkerley/4371315 to your computer and use it in GitHub Desktop.
Save bkerley/4371315 to your computer and use it in GitHub Desktop.
> ruby slugs.rb
id: 1639513188
enciphered output, string-escaped for your convenience:
"X\xD0\x97N"
slug: lt1krc
decrypted id: 1639513188
the same?
true
require 'openssl'
require 'pp'
id = rand(2**32)
puts "id: #{id}"
pack_instruction = 'L'
cryptor = OpenSSL::Cipher::Cipher.new 'AES-128-CFB'
k = 'stinkyfartz12345'
# don't make the iv and the key same for the love of god
# also never change these
cryptor.key = k
cryptor.iv = k
cryptor.encrypt
output_buffer = ''
# Q = uint64, once you get past 18446744073709551616 things you'll
# have to update your encoding
output_buffer << cryptor.update([id].pack(pack_instruction))
output_buffer << cryptor.final
# o is now some bits representing your encrypted integer
puts "enciphered output, string-escaped for your convenience:"
pp output_buffer
slug = output_buffer.unpack(pack_instruction).first.to_s(36)
puts "slug: #{slug}"
decryptor = OpenSSL::Cipher::Cipher.new 'AES-128-CFB'
decryptor.key = k
decryptor.iv = k
decryptor.decrypt
decrypt_buffer = ''
decrypt_buffer << decryptor.update([slug.to_i(36)].pack(pack_instruction))
decrypt_buffer << decryptor.final
decrypted_id = decrypt_buffer.unpack(pack_instruction).first
puts "decrypted id: #{decrypted_id}"
puts "the same?"
pp id == decrypted_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment