rails加密/解密 [英] rails encryption/decryption
本文介绍了rails加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
OpenSSL :: Cipher :: CipherError在ProfilesController#显示
错误的最终块长度
需要更改才能停止这个错误。我尝试使用这样的openssl的其他实现(从我的模型中调用的方法)
def encrypt_attr(unencrypted)
c = OpenSSL :: Cipher.new(aes-256-cbc)
c.encrypt
c.key = Digest :: SHA1.hexdigest('pass')
e = c。 update(unencrypted)
e<< c.final
return e
end
def decrypt_attr(encrypted_attr)
如果encrypted_attr!=
c = OpenSSL :: Cipher :: Cipher。 new(aes-256-cbc)
c.decrypt
c.key = Digest :: SHA1.hexdigest('pass')
d = c.update(encrypted_attr)
d< c.final
return d
end
end
它抛出完全相同的解密错误。我应该如何进行加密和解密,而不会得到这个openssl错误。
解决方案
require' openssl'
require'base64'
class AesEncryptDecrypt
KEY =EncryptDecryptGurudathBN
ALGORITHM ='AES-128-ECB'
def self.encryption(msg)
begin
cipher = OpenSSL :: Cipher.new(ALGORITHM)
cipher.encrypt()
cipher.key = KEY
crypt = cipher.update(msg)+ cipher.final()
crypt_string =(Base64.encode64(crypt))
return crypt_string
rescue Exception => exc
puts(Message for the encryption log file for message#{msg} =#{exc.message})
end
end
def self。解密(msg)
begin
cipher = OpenSSL :: Cipher.new(ALGORITHM)
cipher.decrypt()
cipher.key = KEY
tempkey = Base64。 decode64(msg)
crypt = cipher.update(tempkey)
crypt<< cipher.final()
return crypt
rescue Exception => exc
puts(Message for the decrypt log file for message#{msg} =#{exc.message})
end
end
end
加密
irb主要):007:0> AesEncryptDecrypt.encryption('gurudath')
=> rUPKObydUJd9cY9agm3Glw == \\\
解密
IRB(主):008:0> AesEncryptDecrypt.decryption('rUPKObydUJd9cY9agm3Glw ==')
=> gurudath
i need to do encryption and decryption in my rails app. Im trying to use ezcrypto, but whenever i do decryption i get this error.
OpenSSL::Cipher::CipherError in ProfilesController#show
wrong final block length
What would need to be changed to stop this error. I tried using another implementation of openssl like this (methods to be called from my model)
def encrypt_attr(unencrypted)
c = OpenSSL::Cipher.new("aes-256-cbc")
c.encrypt
c.key = Digest::SHA1.hexdigest('pass')
e = c.update(unencrypted)
e << c.final
return e
end
def decrypt_attr(encrypted_attr)
if encrypted_attr != ""
c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
c.decrypt
c.key = Digest::SHA1.hexdigest('pass')
d = c.update(encrypted_attr)
d << c.final
return d
end
end
It throws the exact same error on decryption. How should i do encryption and decryption and not get this openssl error.
解决方案
require 'openssl'
require 'base64'
class AesEncryptDecrypt
KEY = "EncryptDecryptGurudathBN"
ALGORITHM = 'AES-128-ECB'
def self.encryption(msg)
begin
cipher = OpenSSL::Cipher.new(ALGORITHM)
cipher.encrypt()
cipher.key = KEY
crypt = cipher.update(msg) + cipher.final()
crypt_string = (Base64.encode64(crypt))
return crypt_string
rescue Exception => exc
puts ("Message for the encryption log file for message #{msg} = #{exc.message}")
end
end
def self.decryption(msg)
begin
cipher = OpenSSL::Cipher.new(ALGORITHM)
cipher.decrypt()
cipher.key = KEY
tempkey = Base64.decode64(msg)
crypt = cipher.update(tempkey)
crypt << cipher.final()
return crypt
rescue Exception => exc
puts ("Message for the decryption log file for message #{msg} = #{exc.message}")
end
end
end
Encryption
irb(main):007:0> AesEncryptDecrypt.encryption('gurudath')
=> "rUPKObydUJd9cY9agm3Glw==\n"
Decryption
irb(main):008:0> AesEncryptDecrypt.decryption('rUPKObydUJd9cY9agm3Glw==')
=> "gurudath"
这篇关于rails加密/解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文