-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.lua
More file actions
43 lines (36 loc) · 1.09 KB
/
Copy pathrsa.lua
File metadata and controls
43 lines (36 loc) · 1.09 KB
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
local openssl_pkey = require "resty.openssl.pkey"
local gsub = string.gsub
local _M = { _VERSION = '1.0' }
--公钥加密
function _M.public_encrypt(msg, publicKey)
local key = gsub(publicKey, "\\n", "\n")
local pub = openssl_pkey.new(key)
local chunkSize = 2048 / 8 - 11;
local cipher_txt = ''
for i = 1, #msg, chunkSize do
local sub_msg = msg:sub(i, i + chunkSize - 1)
local sub_cipher_txt, err = pub:encrypt(sub_msg)
if err then
return nil, nil
end
cipher_txt = cipher_txt .. sub_cipher_txt
end
return 1, cipher_txt
end
--私钥解密
function _M.private_decrypt(msg, privateKey)
local key = gsub(privateKey, "\\n", "\n")
local pri = openssl_pkey.new(key)
local chunkSize = 2048 / 8;
local decrypted = ''
for i = 1, #msg, chunkSize do
local sub_msg = msg:sub(i, i + chunkSize - 1)
local sub_decrypted_txt, err = pri:decrypt(sub_msg)
if err then
return nil, nil
end
decrypted = decrypted .. sub_decrypted_txt
end
return 1, decrypted
end
return _M