Skip to content

Instantly share code, notes, and snippets.

@arslanaly47
Last active November 7, 2017 18:59
Show Gist options
  • Save arslanaly47/870462894e627cafe6abb979f28647f3 to your computer and use it in GitHub Desktop.
Save arslanaly47/870462894e627cafe6abb979f28647f3 to your computer and use it in GitHub Desktop.
require 'openssl'
require 'base64'
aes = OpenSSL::Cipher::AES256.new(:CBC)
aes.encrypt
key = aes.random_key
iv = aes.random_iv
encrypted_text = aes.update('{"application_bundle":"com.mx.serfimet.api"}') + aes.final
Base64.encode64(encrypted_text) # The final text to send along in the API request.
Here's the public key given to me: `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl2uzoWA2iEGfeqDYO4XA0mQV8odb6qgdLT0T/k1PsbviMMt+SlAnZpwDkPlFySxNejOv8zAUHwDTWV7NGpYA0y+TrrBLkGGl9uUrzOjQLryJmFWsXxBGFd4emYXYBzROaH+r7pzhK7mIWLw1e2Cj5RL9Ac6wKA56axqCr2UZRNDBcS5A+nKTQTWy5eTrq6EQSBA6Hr81UWtYwlNSo1YbZ9JSNdD75Go7Du/zv1iPI+wWr7tMp0AJVBmieeB0kryXl+8zhli80wa72hDtyTkmfkBJAid5Il1biTrOXtMDaqXdfi9s4xzFp/ilP17KnA+VeAH2eLcqaC6LkEp/A1LApwIDAQAB`
I build a file with an extension of `.pem`, consisting of the public key in the following format:
-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----
After that,
require 'openssl'
require 'base64'
pubblic_key = OpenSSL::PKey::RSA.new(File.read(/path/to/.pem/file))
cipher_text = public_key.public_encrypt('n2yTcM0+ho4cnNPggQpoABi8wUbGteJIE/gfl1A/6Fo=\n')
encrypted_text = Base64.encode64(cipher_text)
Running the above code gave me:
FC/5xtnSnYSsOUP5fVNGLAK0JaJ3rVcc3WtZfhlyRdsbugbSs815FGHm3KN6\nPk+AuwWNngitm9UDzW5J1H2VwPGZ4vnK5d7q1Ue+O5U8P9uTMZ6Ect0/nL8z\ngAPRMfUMQY/Y9sBznx+x8u5kxOm4TADsXrHCp0Ny/X+0+fXQnTn2LWaDRiy/\nw7QwJDndYqQhjJhPEb4B6Jr6+OkoZ2A0MKtA06KTQQP7QAlQYNQXXVqP8B0v\nOfm6+rZIao1jaKrmAkid6PGdxqQqUliUmLB3nx4NjzWH3+sHGSJgypCFkfPh\n6a5sYb/Cu+7hDcKwYtzBl5wSuILdRHchZgtvdDN3dg==\n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment