lazy_rsa 0.3.1 → 0.3.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/lazy_rsa.rb +29 -10
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21e9a09c2b446c925d4ac119589f7f24b549f7bc662863dd5d64c4c41812e336
4
- data.tar.gz: 9bdc9ceda55df07759aba32d77a8f92c0328c2edb29bdfe27f0a7f107083804c
3
+ metadata.gz: cc42b9892d9823a2c14bba278e7a6a1910f97d9045f7ab4405728eb80cc60432
4
+ data.tar.gz: d11eb00ba4e516288b09e5924db56f06ab8ee53cfce451f756e1eadd9dc5a1ed
5
5
  SHA512:
6
- metadata.gz: 70a6c4abc45ea146e15727fd1fc3cd5842b83a779112530cb333f401e4de5b39f63e6fda9cf15d785476b5c6a0d122660bfb7a28cf6aa28c18d7a498ae0f5833
7
- data.tar.gz: 59709a1f78ccdf10579cdb5849d03fb18c5cd898238286af6762352187493c01fce9c6d8b4c0f4409dbb5e7b18bba81e77509496eca0e5ceb1ee684d78478d48
6
+ metadata.gz: 54a2c17dfebcec69795b27667fe7704f1458b8da16f3075a002e035a0b9e2677bcf1272ded73c46d800030608bbc0449e61bcf8f98cc638b93805e1c7f06f96a
7
+ data.tar.gz: 5e50beea6a6501e5ba86fb1d0975961d5de967c15eea94d9ecea330e696fb0d826d40ced26341a0f9276ded650af4a955cf77c6d7ecd3a8de813ac76321008ff
data/lib/lazy_rsa.rb CHANGED
@@ -1,7 +1,12 @@
1
1
  module LazyRsa
2
- KeyParams = Data.define(:n, :e, :d)
3
-
4
2
  class << self
3
+ KeyParams = Data.define(:n, :e, :d)
4
+ SECURE_EXPONENT = 0xECE669597141FAC8A699C75294E7E087698EAD0B32D58F4A17DC016B0483E3B9163B666AFC8D251B69F180B3902BB6238D7BAE57AF938DD8484F98B5790E8D96FBB5F2A0207497F9AEEEBECED882F0F55540443B5FA1221DF5688DE1F89E9A5801AFFAA82A5A4D213938CD08479DEE3A13974144C9F624CEA7E0DE246F00635D
5
+ SECURE_MODULUS = 0x48D4498B62509947218CD104794AF6DBB6498F5CCCFCCACA57BF4E6FC9BDAAFAE06341F118430BCE33B5304AB8EEF60A7E53466E92965EB716C6673F5D3B726923FF699F1B197E941870D8DFC9C740A7DE9C92F10C151CCB0405E1960CB62A5547037FD3CD1FC2C1F7BF4899A971D0D1E4D5E6C1676F03645658CB66744D3BDD84E5C316163CE4AA7B8205F784CC0012B19951DDF90920858C4DA9A4BCC6D21C55BB164C74EAB2817556598AF9D7AFF2F703E46AC7D5E9250C7C04B0B1603EABA11A31EF0B99D7F75277EF624E436C9ADD8D3DC614BC325C36A0025DF3E0C2259C89D2AC482D8DF9AC6670F6A45C4D42D310948D5298634868981F121F185C5B
6
+
7
+ # TODO: Don't forget to delete
8
+ # 8640597603637126406272555462588607969196265598480577420885940100654219489731968191782621284496957292949698043346999288278510409937300241248019375509693403172885588630944559389636407405809836240044729232734036234641195652957645172843481237165369064901933891564045525753333289716030022555506189600600361366572922924448439537301834900154422907814193181069086899891297666471122045361586113907893536706052205097080200373985921768568459673078912498025950915719208025786019022039776921643808423880484962610454155546320083727309260845710176973892925885708962743395401298886761017168076121983679500825805191588517583935100021
9
+
5
10
  def encrypt(key, plaintext)
6
11
  key = read_key(key)
7
12
  text_to_integer(plaintext).mod_exp(key.e, key.n).to_i
@@ -9,28 +14,38 @@ module LazyRsa
9
14
 
10
15
  def decrypt(key, ciphertext)
11
16
  key = read_key(key)
12
- OpenSSL::BN.new(ciphertext).mod_exp(key.d, key.n).to_i
17
+ integer = OpenSSL::BN.new(ciphertext).mod_exp(key.d, key.n)
18
+ integer_to_text(integer)
13
19
  end
14
20
 
15
21
  def generate_key
22
+ p = 0
16
23
  q = OpenSSL::BN.generate_prime(512)
17
- phi = OpenSSL::BN.new(0)
18
- while q.gcd(phi) != 1
24
+ phi = 0
25
+ e = 0
26
+ loop do
19
27
  p = OpenSSL::BN.generate_prime(512)
20
28
  phi = (p - 1) * (q - 1)
29
+ e = p.mod_exp(SECURE_EXPONENT, SECURE_MODULUS)
30
+ break if e.gcd(phi) == 1
21
31
  end
22
32
  n = p * q
23
- e = p.mod_exp(Constants::SECURE_EXPONENT, Constants::SECURE_MODULUS)
24
33
  d = e.mod_inverse(phi)
25
34
 
26
35
  build_key(e:, d:, n:)
27
36
  end
28
37
 
29
- def build_key(e:, d: nil, n:)
38
+ def build_key(e: nil, d: nil, n:)
30
39
  data_sequence = OpenSSL::ASN1::Sequence([
40
+ OpenSSL::ASN1::Integer.new(0),
31
41
  OpenSSL::ASN1::Integer(n),
32
- OpenSSL::ASN1::Integer(e),
33
- OpenSSL::ASN1::Integer(d),
42
+ e.nil? ? OpenSSL::ASN1::Integer.new(0) : OpenSSL::ASN1::Integer(e),
43
+ d.nil? ? OpenSSL::ASN1::Integer.new(0) : OpenSSL::ASN1::Integer(d),
44
+ OpenSSL::ASN1::Integer.new(0),
45
+ OpenSSL::ASN1::Integer.new(0),
46
+ OpenSSL::ASN1::Integer.new(0),
47
+ OpenSSL::ASN1::Integer.new(0),
48
+ OpenSSL::ASN1::Integer.new(0),
34
49
  ])
35
50
  asn1 = OpenSSL::ASN1::Sequence(data_sequence)
36
51
  OpenSSL::PKey::RSA.new(asn1.to_der)
@@ -39,11 +54,15 @@ module LazyRsa
39
54
  private
40
55
 
41
56
  def read_key(key)
42
- KeyParams.new(**key.params)
57
+ KeyParams.new(**key.params.slice("e", "d", "n"))
43
58
  end
44
59
 
45
60
  def text_to_integer(text)
46
61
  OpenSSL::BN.new(text.unpack("H*").first.to_i(16))
47
62
  end
63
+
64
+ def integer_to_text(integer)
65
+ [integer.to_s(16)].pack("H*")
66
+ end
48
67
  end
49
68
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_rsa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Quaranto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-12 00:00:00.000000000 Z
11
+ date: 2024-05-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: WARNING! PLEASE DON'T USE THIS GEM SINCE IT HAVE INTENTIONAL BACKDOOR
14
14
  (FOR EDUCATIONAL PURPOSE)
@@ -37,7 +37,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
39
  requirements: []
40
- rubygems_version: 3.4.19
40
+ rubygems_version: 3.1.2
41
41
  signing_key:
42
42
  specification_version: 4
43
43
  summary: Easy RSA