Ruby - Generating prime numbers for Websocket encryption

184 views Asked by At

I'm creating a WebSocket Server in Ruby, and I would like to encrypt the string messages between the server and the client. I can't afford a certificate, so I was thinking that I would create an encryption algorithm using modulo.

I need to generate large prime numbers for this algorithm. I know that Ruby has a built in function for Primes, but I'm not sure if it can generate 50 to 60 digit numbers. Is the built in function for Primes good for this?

If anyone can offer a better way of encrypting my WS messages for free (and decrypt on the other side) I would also accept that :)

1

There are 1 answers

5
Nick Veys On BEST ANSWER

A self-signed certificate will work unless this is a public-facing project. Real certificates only matter if your client needs a way to trust the server it's connecting to.

Alternately, the Crypt gem has lots of Ruby implementations of cryptographic functions. Here's an example of encrypting and decrypting a string using Rijndael:

crypter = Crypt::Rijndael.new("super-awesome-32-byte-key-goes-here")
plaintext = "Hey Bob, how's it going? -- Alice"
cyphertext = crypter.encrypt_block(plaintext)
plaintext_again = crypter.decrypt_block(cyphertext)

If you can share a key between your client/server, you shouldn't have any trouble doing this.