Module: RockFintech::Encrypt::RSA

Defined in:
lib/rock_fintech/encrypt/rsa.rb

Constant Summary collapse

MAX_ENCRYPT_LENGTH =
245
MAX_DECRYPT_LENGTH =
256

Class Method Summary collapse

Class Method Details

.decrypt(content, private_key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rock_fintech/encrypt/rsa.rb', line 21

def self.decrypt(content, private_key)
  result_str, decryt_bytes, offset, i = "", "", 0, 0

  content_str = Base64.strict_decode64(content)
  input_length = content_str.length
  while input_length - offset > 0
    decryt_bytes = content_str[offset, MAX_DECRYPT_LENGTH]
    result_str << private_key.private_decrypt(decryt_bytes)
    offset = (i += 1) * MAX_DECRYPT_LENGTH
  end
  result_str
end

.encrypt(content, public_key) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rock_fintech/encrypt/rsa.rb', line 8

def self.encrypt(content, public_key)
  content_str, encryt_str, offset, i = "", "", 0, 0

  bytes_array = content.unpack("C*")
  input_length = bytes_array.length
  while  input_length - offset > 0
    encryt_bytes = bytes_array[offset, MAX_ENCRYPT_LENGTH]
    encryt_str << public_key.public_encrypt(encryt_bytes.pack("C*"))
    offset = (i += 1) * MAX_ENCRYPT_LENGTH
  end
  Base64.strict_encode64(encryt_str)
end