Asymmetric Ruby OpenSSL Encryption - Using Two Keypairs

I want to use two key pairs to implement secure messaging without abandoning two communication systems. I created and saved two sets of key pairs using:

sys1_key = OpenSSL::PKey::RSA.generate( 2048 ) sys2_key = OpenSSL::PKey::RSA.generate( 2048 ) 

Both of these key pairs have their own separate public and private keys stored in files:

  • sys1.pub.pem
  • sys1.priv.pem
  • sys2.pub.pem
  • sys2.priv.pem

System 1 has its own public and private keys, as well as the System 2 public key. System 2 has its own public and private keys, as well as the System 1 public key.

On system 1, I want to accept the "Hello world" message and encrypt it with the System 1 private key and the System 2 public key. This message can only be decrypted by System 2 using its own private key and the System 1 public key.

We are currently implementing a similar process using GPG, but I want to automate the process - any help is appreciated. We are on Ruby 1.9.2.

+4
source share
2 answers

Private keys are for decryption, and public keys are for encryption.

So, if System 1 intends to send the encrypted text to System 2.

System 1 should receive the System 2 public key and encrypt the text using the System 2 public key. System 2 can receive the text and use its private key for decryption. When sending Side you can use such a code.

 #!/usr/bin/env ruby require 'openssl' require 'base64' public_key_file = 'server2_public.pem'; string = 'Secret!'; public_key = OpenSSL::PKey::RSA.new(File.read(public_key_file)) encrypted_string = Base64.encode64(public_key.public_encrypt(string)) print encrypted_string, "\n" 

Base 64 encoding is just an optional step, depending on whether you want to send binary data over the wire, etc.

On the receiving side

 #!/usr/bin/env ruby require 'openssl' require 'base64' key = 'my_private_key.pem'; private_key = OpenSSL::PKey::RSA.new(File.read(key),'password') data = private_key.private_decrypt(Base64.decode64(encrypted_data_received)) puts data 

If you have n number of systems, you should use conventions to search for public key names. For example, the name of the public key may be the same name as the name of the target device for the message, and it can be found from the key server. private keys should be present only in the corresponding system.

The goal of asymmetric encryption is double.

1) If you encrypt using the public key. Only a person with the corresponding secret key can decrypt. This ensures message security. 2) If you sign using a private key, the public key can be used for authentication. Everyone who has access to the public key can verify the authenticity of the message. This proves that only you could encrypt it. This is called a signature.

+8
source

There is a pretty useful library that I wrote a few months ago that serves as a very easy-to-use OpenSSL shell that supports RSA. The interface is quite readable and understandable.

 public_key, private_key = Encryption::Keypair.generate( 2048 ) plaintext = 'some secret data' securetext = public_key.encrypt( plaintext ) private_key.decrypt( securetext ) == plaintext # true 

He called encryption :
GitHub encryption
Rubygems encryption
Documentation

+1
source

Source: https://habr.com/ru/post/1437055/


All Articles