From: "ara.t.howard" Date: 2008-01-22T03:32:12+09:00 Subject: Re: Encrypting/decrypting data On Jan 21, 2008, at 11:05 AM, laredotornado@zipmail.com wrote: > > I'm using the latest version of Ruby for Fedora Core 6 Linux. I want > to store credit card info in my database. I'd like to store it in an > encrypted form, but I'd also like to be able to decrypt it later. > Does anyone have any useful encryption/decryption routines? > > Thanks, - Dave > module Site module Encryption class << self attr_accessor 'mac_address' def mac_address return @mac_address if defined? @mac_address re = %r/[^:\-](?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F][^: \-]/io cmds = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all' null = test(?e, '/dev/null') ? '/dev/null' : 'NUL' lines = nil cmds.each do |cmd| stdout = IO.popen("#{ cmd } 2> #{ null }"){|fd| fd.readlines} rescue next next unless stdout and stdout.size > 0 lines = stdout and break end raise "all of #{ cmds.join ' ' } failed" unless lines candidates = lines.select{|line| line =~ re} raise 'no mac address candidates' unless candidates.first candidates.map!{|c| c[re]} maddr = candidates.first raise 'no mac address found' unless maddr maddr.strip! maddr.instance_eval{ @list = candidates; def list() @list end } @mac_address = maddr end attr_accessor 'blowfish' def blowfish @blowfish ||= Crypt::Blowfish.new(key) end attr_accessor 'key' def key @key ||= "--#{ mac_address }--#{ hostname }--"[0,56] end attr_accessor 'hostname' def hostname @hostname ||= Socket.gethostname end def encrypt string Base64.encode64(blowfish.encrypt_string(string.to_s)).chop # kill "\n" end def decrypt string, kw = {} blowfish.decrypt_string(Base64.decode64("#{ string }\n")).strip end end end def self.encryption() Encryption end def self.encrypt(*a, &b) Encryption.encrypt(*a, &b) end def self.decrypt(*a, &b) Encryption.decrypt(*a, &b) end end you will want to set both the mac_address, the key, or both. you'll need crypt/blowfish for tis to work, obviously. regards. a @ http://codeforpeople.com/ -- share your knowledge. it's a way to achieve immortality. h.h. the 14th dalai lama