AES:使用crypto-js加密,使用ruby openssl库解密

时间:2015-02-01 22:52:19

标签: javascript ruby-on-rails ruby encryption cryptography

我想在网站上使用用户密钥加密消息并在服务器端解密,但解密不起作用。

crypto-js(coffe js):

$(document).on 'click', '.subclick', ->
  text = $('#myform_text').val()
  pass = $('#myform_passphrase').val()
  days = $('#store_days').val()
  encrypted = CryptoJS.AES.encrypt(text, pass)
  $.post '/otprecs',
    text: encrypted.toString(CryptoJS.enc.utf8);
    store_days: days
  return

ruby​​ decrypt功能:

 def decrypt_text(passphrase, text)
    encrypted = Base64.decode64(text)
    cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
    cipher.decrypt
    cipher.key = Digest::SHA256.digest(passphrase.chomp)
    text = cipher.update(encrypted)
    text << cipher.final
    puts text
  end

你有什么想法吗?

0 个答案:

没有答案