elgamal 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/elgamal.rb +28 -0
- data/lib/elgamal/ciphertext.rb +19 -0
- data/lib/elgamal/key_pair.rb +21 -0
- data/lib/elgamal/private_key.rb +20 -0
- data/lib/elgamal/public_key.rb +24 -0
- metadata +48 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 5abd9582417e08624a039e6dcea88225ee851603
|
|
4
|
+
data.tar.gz: fab762ab23fb3aaa7552c8749fcf9988b9a78b38
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c185f01236eb137a91a9fe0753eec8f6c24b9a101e32b2fa47b63888ec20053eab209bebaca104842467a18dff973eceb486ff59b6310279f5f7687143f88e92
|
|
7
|
+
data.tar.gz: 8782e95e5078e2be41c67c9e3dc52e128bc1e09a206d7a01e6d5a9dc77cffdf8d94fe7b2d987d764f11245b8e9892874292edb187ac1011b72bb7f7968e58bff
|
data/lib/elgamal.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# require 'bigdecimal' unless defined?(BigDecimal)
|
|
2
|
+
# require 'stringio' unless defined?(StringIO)
|
|
3
|
+
|
|
4
|
+
# if RUBY_VERSION < '1.9.1'
|
|
5
|
+
# # @see http://rubygems.org/gems/backports
|
|
6
|
+
# begin
|
|
7
|
+
# require 'backports/1.9.1'
|
|
8
|
+
# rescue LoadError
|
|
9
|
+
# abort "RSA.rb requires Ruby 1.9.1 or the Backports gem (hint: `gem install backports')."
|
|
10
|
+
# end
|
|
11
|
+
# end
|
|
12
|
+
|
|
13
|
+
# module ElGamal
|
|
14
|
+
# # autoload :Math, 'rsa/math'
|
|
15
|
+
# # autoload :PKCS1, 'rsa/pkcs1'
|
|
16
|
+
# autoload :PrivateKey, 'elgamal/private_key'
|
|
17
|
+
# autoload :PublicKey, 'elgamal/public_key'
|
|
18
|
+
# autoload :KeyPair, 'elgamal/key_pair'
|
|
19
|
+
# # autoload :OpenSSL, 'rsa/openssl'
|
|
20
|
+
# # autoload :VERSION, 'rsa/version'
|
|
21
|
+
# # autoload :KeyPair, 'rsa/version'
|
|
22
|
+
# end
|
|
23
|
+
|
|
24
|
+
require 'openssl'
|
|
25
|
+
require_relative 'elgamal/key_pair'
|
|
26
|
+
require_relative 'elgamal/private_key'
|
|
27
|
+
require_relative 'elgamal/public_key'
|
|
28
|
+
require_relative 'elgamal/ciphertext'
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module ElGamal
|
|
2
|
+
|
|
3
|
+
class Ciphertext
|
|
4
|
+
|
|
5
|
+
def initialize(ciphertext)
|
|
6
|
+
raise ArgumentError.new("Required a tuple input, eg. [123,456]") unless ciphertext.class == Array and ciphertext.length == 2
|
|
7
|
+
@ciphertext = ciphertext
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def to_s
|
|
11
|
+
"#{@ciphertext}"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def [](pos)
|
|
15
|
+
return @ciphertext[pos]
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module ElGamal
|
|
2
|
+
|
|
3
|
+
class KeyPair
|
|
4
|
+
|
|
5
|
+
def initialize(public_key: nil, private_key: nil)
|
|
6
|
+
@public_key = public_key
|
|
7
|
+
@private_key = private_key
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def generate(bits: 20)
|
|
11
|
+
p = OpenSSL::BN::generate_prime(bits).to_i
|
|
12
|
+
g = (rand * p).to_i
|
|
13
|
+
a = (rand * (p - 1)).to_i + 1
|
|
14
|
+
h = g.to_bn.mod_exp(a, p)
|
|
15
|
+
return ElGamal::PublicKey.new(public_p: p, public_g: g, public_h: h),
|
|
16
|
+
ElGamal::PrivateKey.new(private_a: a, public_p: p)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module ElGamal
|
|
2
|
+
|
|
3
|
+
class PrivateKey
|
|
4
|
+
|
|
5
|
+
attr_reader :private_a
|
|
6
|
+
|
|
7
|
+
def initialize(private_a: nil, public_p: nil)
|
|
8
|
+
@private_a = private_a
|
|
9
|
+
@public_p = public_p
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def decrypt(ciphertext)
|
|
13
|
+
raise ArgumentError unless ciphertext.instance_of? Ciphertext
|
|
14
|
+
ciphertext[1] * (ciphertext[0] ** @private_a).to_bn.mod_inverse(@public_p) % @public_p
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module ElGamal
|
|
2
|
+
|
|
3
|
+
class PublicKey
|
|
4
|
+
|
|
5
|
+
attr_reader :public_p,
|
|
6
|
+
:public_g,
|
|
7
|
+
:public_h
|
|
8
|
+
|
|
9
|
+
def initialize(public_p: nil, public_g: nil, public_h: nil)
|
|
10
|
+
@public_p = public_p
|
|
11
|
+
@public_g = public_g
|
|
12
|
+
@public_h = public_h
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def encrypt(message)
|
|
16
|
+
rand_k = rand(@public_p - 1) + 1
|
|
17
|
+
element_a = @public_g.to_bn.mod_exp(rand_k, @public_p).to_i
|
|
18
|
+
element_b = message * @public_h.to_bn.mod_exp(rand_k, @public_p).to_i % @public_p
|
|
19
|
+
return ElGamal::Ciphertext.new([element_a, element_b])
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: elgamal
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Joel Scarfone
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-12-01 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Basic ruby ElGamal implementation. Used to do basic encryption and decryption.
|
|
14
|
+
email: joel.scarfone@carleton.ca
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/elgamal.rb
|
|
20
|
+
- lib/elgamal/ciphertext.rb
|
|
21
|
+
- lib/elgamal/key_pair.rb
|
|
22
|
+
- lib/elgamal/private_key.rb
|
|
23
|
+
- lib/elgamal/public_key.rb
|
|
24
|
+
homepage: https://github.com/JoelScarfone/ElGamal
|
|
25
|
+
licenses:
|
|
26
|
+
- MIT
|
|
27
|
+
metadata: {}
|
|
28
|
+
post_install_message:
|
|
29
|
+
rdoc_options: []
|
|
30
|
+
require_paths:
|
|
31
|
+
- lib
|
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
requirements: []
|
|
43
|
+
rubyforge_project:
|
|
44
|
+
rubygems_version: 2.5.1
|
|
45
|
+
signing_key:
|
|
46
|
+
specification_version: 4
|
|
47
|
+
summary: Basic ruby ElGamal implementation.
|
|
48
|
+
test_files: []
|