2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby#SSLを利用したサーバーとクライアント

Last updated at Posted at 2016-05-27

文字を大文字にするサーバー

server.rb
require 'socket'
require 'openssl'

include OpenSSL

ctx = SSL::SSLContext.new()
ctx.cert = X509::Certificate.new(File.read('server-cert.pem'))
ctx.key = PKey::RSA.new(File.read('server-key.pem'))
svr = TCPServer.new(4443)
serv = SSL::SSLServer.new(svr, ctx)

socket = serv.accept
text = socket.gets
socket.puts(text.upcase)

"hello"を送るだけのクライアント

client.rb
require 'socket'
require 'openssl'

include OpenSSL

soc = TCPSocket.new('localhost', 4443)
socket = SSL::SSLSocket.new(soc)
socket.connect

socket.puts("hello")
puts socket.gets

サーバー起動

$ ruby server.rb

クライアント実行

$ ruby client.rb
HELLO # helloが大文字になって帰って来ます

おまけ: 秘密鍵とかを作るコマンド

$ openssl genrsa -out server-key.pem 1024
$ openssl req -new -key server-key.pem -out server-csr.pem
$ openssl x509 -req -in server-csr.pem -signkey server-key.pem -out server-cert.pem
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?