-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecc_aes.rb
81 lines (70 loc) · 2.24 KB
/
ecc_aes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'base64'
require 'openssl'
require 'dotenv/load'
MAX_BUFFER_LENGTH = 100
MAX_ATTEMPTS = 5
class ECC
attr_accessor :cipher, :key, :iv
def initialize(cipher, key, iv)
@cipher = OpenSSL::Cipher.new('AES-256-CBC')
@key = [generate_shared_secret(ENV['ECC_PRIVATE_KEY'], ENV['ECC_PUBLIC_KEY'])].pack('H*')
@iv = [generate_iv(ENV['AES_IV'])].pack('H*')
@cipher.encrypt
@cipher.key = @key
@cipher.iv = @iv
end
def encryption_scheme(plaintext)
ciphertext = cipher.update(plaintext) + cipher.final
ciphertext = Base64.strict_encode64(ciphertext)
return ciphertext
end
def decryption_scheme(ciphertext)
cipher.decrypt
cipher.key = key
cipher.iv = iv
plaintext = Base64.strict_decode64(ciphertext)
plaintext = cipher.update(plaintext) + cipher.final
return plaintext
end
private
def generate_shared_secret(ecc_key, pub_key)
ecc_public_key = OpenSSL::PKey::EC.new('prime256v1')
ecc_public_key.public_key = OpenSSL::PKey::EC::Point.new(ecc_public_key.group, OpenSSL::BN.new(pub_key, 16))
ecc_private_key = OpenSSL::PKey::EC.new('prime256v1')
ecc_private_key.private_key = OpenSSL::BN.new(ecc_key, 2)
secret = ecc_private_key.dh_compute_key(ecc_public_key.public_key)
secret = OpenSSL::Digest::SHA256.digest(secret)[0, 32]
secret = secret.unpack('H*').first
return secret
end
def generate_iv(iv)
hashed_iv = OpenSSL::Digest::SHA256.digest(iv)[0, 16]
hashed_iv = hashed_iv.unpack('H*').first
return hashed_iv
end
end
def main
ecc = ECC.new(@cipher, @key, @iv)
attempts = MAX_ATTEMPTS
loop do
plaintext = ARGV[0..MAX_BUFFER_LENGTH].join(' ')
if plaintext.match?(/\A[A-Za-z\s]+\z/) && plaintext.length <= MAX_BUFFER_LENGTH
encrypted_message = ecc.encryption_scheme(plaintext)
puts "The ciphertext is: #{encrypted_message}"
decrypted_message = ecc.decryption_scheme(encrypted_message)
return encrypted_message
elsif plaintext.match?(/\A[A-Za-z0-9+\/=]+\z/)
plaintext = ecc.decryption_scheme(plaintext)
puts "The plaintext is: #{plaintext}"
return plaintext
else
attempts -= 1
puts "Invalid plaintext! Please try again. You have #{attempts} attempts left."
if attempts == 0
puts 'No more attempts left. Exiting...'
break
end
end
end
end
main if __FILE__ == $PROGRAM_NAME