lib/rock_fintech/encrypt/rsa.rb in rock_fintech-0.10.0 vs lib/rock_fintech/encrypt/rsa.rb in rock_fintech-0.11.0
- old
+ new
@@ -1,27 +1,34 @@
# coding: utf-8
module RockFintech
module Encrypt
module RSA
+ MAX_ENCRYPT_LENGTH = 245
+ MAX_DECRYPT_LENGTH = 256
def self.encrypt(content, public_key)
- content_str = ''
- content.scan(/.{1,100}/).each{ |str|
- content_str += public_key.public_encrypt(str)
- }
- Base64.strict_encode64(content_str)
+ 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
def self.decrypt(content, private_key)
- content_str = Base64.strict_decode64(content)
+ result_str, decryt_bytes, offset, i = "", "", 0, 0
- result_str = ''
- count = content_str.length / 256
- count.times.each{ |i|
- str = content_str[256*i, 256]
- result_str += private_key.private_decrypt(str)
- }
-
+ 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
end
end