-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path加密解密aes
24 lines (23 loc) · 830 Bytes
/
加密解密aes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
AESSECRET = $setting['aes']['secret']
AESIV = $setting['aes']['iv']
def self.token_for id, user_id
jwt_encrypt = JWT.encode({user_id: user_id.to_s}, id.to_s)
cipher = OpenSSL::Cipher::AES.new(256, :CBC)
cipher.encrypt
cipher.key = AESSECRET
cipher.iv = AESIV
aes_encrypt = cipher.update(jwt_encrypt) + cipher.final
aes_encrypt = aes_encrypt.force_encoding('utf-8')
aes_encrypt = Base64.encode64(aes_encrypt).encode('utf-8')
return aes_encrypt
end
def self.decrypt token, id
aes_encrypt = Base64.decode64(token)
decipher = OpenSSL::Cipher::AES.new(256, :CBC)
decipher.decrypt
decipher.key = AESSECRET
decipher.iv = AESIV
plain = decipher.update(aes_encrypt) + decipher.final
token_hash = JWT.decode(plain, id.to_s)
return token_hash[0]["user_id"].to_i
end