# coding: utf-8 require 'openssl' require 'digest/sha1' module RailsCaptcha class Cipher @@key = Digest::SHA1.hexdigest(Config.options[:password]) @@iv = 'captchas'*2 def self.encrypt(text) # Encrypt cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") cipher.encrypt cipher.key = @@key cipher.iv = @@iv encrypted = cipher.update(text) encrypted << cipher.final # Turn into chr codes separated by underscores # 135_14_163_53_43_135_172_31_1_23_169_81_49_110_49_230 if encrypted.respond_to?(:codepoints) encrypted = encrypted.codepoints.to_a else encrypted = (0..encrypted.length-1).collect do |x| encrypted[x] end end encrypted.join('_') end def self.decrypt(text) # Decode chr coded string encrypted = text.split('_').collect do |x| x.to_i.chr end encrypted = encrypted.join('') # Decrypt cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") cipher.decrypt cipher.key = @@key cipher.iv = @@iv decrypted = cipher.update(encrypted) decrypted << cipher.final decrypted.gsub(/[^a-z]/, '') end end end