mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-03 03:40:49 +08:00
新增: 登陆密码加密处理
This commit is contained in:
45
app/helpers/aes_crypt_helper.rb
Normal file
45
app/helpers/aes_crypt_helper.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
module AesCryptHelper
|
||||
|
||||
AES_KEY = EduSetting.get("login_crypt_key") || '59c96c3572ab8cc1'
|
||||
|
||||
def encrypt(plain_text, output_encoding = 'base64')
|
||||
|
||||
# 将字符串密钥和IV转换为16字节的字节数组
|
||||
key = AES_KEY.byteslice(0, 16)
|
||||
iv = AES_KEY.byteslice(0, 16)
|
||||
|
||||
# 创建并设置AES-CBC加密器
|
||||
cipher = OpenSSL::Cipher.new('AES-128-CBC')
|
||||
cipher.encrypt
|
||||
cipher.key = key
|
||||
cipher.iv = iv
|
||||
|
||||
# 加密数据,并添加PKCS7填充
|
||||
encrypted_data = cipher.update(plain_text) + cipher.final
|
||||
# 将加密数据转换为Base64编码
|
||||
Base64.strict_encode64(encrypted_data)
|
||||
end
|
||||
|
||||
def decrypt(cipher_text, input_encoding = 'base64')
|
||||
# 确保密钥是16字节长
|
||||
key = AES_KEY.byteslice(0, 16) # 如果密钥不足16字节,填充空格;如果超过,截断
|
||||
iv = AES_KEY.byteslice(0, 16)
|
||||
|
||||
decipher = OpenSSL::Cipher.new('AES-128-CBC')
|
||||
decipher.decrypt
|
||||
decipher.key = key
|
||||
decipher.iv = iv
|
||||
|
||||
# 根据输入编码解码密文
|
||||
decrypted_data = case input_encoding
|
||||
when 'base64'
|
||||
Base64.strict_decode64(cipher_text)
|
||||
else
|
||||
cipher_text
|
||||
end
|
||||
|
||||
decrypted = decipher.update(decrypted_data) + decipher.final
|
||||
decrypted
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user