discuz 加密解密 authcode 用Ruby 1.9 实现 (可用)
[code=ruby]
def self.authcode(str, decode=true, key='', expiry=0)
return unless str
str = decode ? str+'====' : str.gsub(/\\t/,"\t")
ckey_length = 4
key = Digest::MD5.hexdigest(!key.empty? ? key : 'tool')
keya = Digest::MD5.hexdigest(key[0,16])
keyb = Digest::MD5.hexdigest(key[16,16])
keyc = decode ? str[0,ckey_length] : Digest::MD5.hexdigest("#{Time.now.to_f} #{Time.now.to_i}")[-ckey_length,ckey_length]
cryptkey = keya + Digest::MD5.hexdigest(keya+keyc)
key_length = cryptkey.size
str = decode ? Base64.decode64(str[ckey_length..-1]) : format("%010d",(expiry!=0 ? expiry + Time.now.to_i : 0)).to_s + Digest::MD5.hexdigest(str+keyb)[0,16]+str
str_length = str.size
result = ''
box = {}
(0..255).each { |i| box[i] = i}
rndkey = {}
(0..255).each do |i|
rndkey[i] = (cryptkey[i % key_length,1]).ord
end
j = 0
(0..255).each do |i|
j = (j + box[i].to_i + rndkey[i].to_i) % 256
tmp = box[i]
box[i] = box[j]
box[j] = tmp
end
a=j=0
(0..str_length-1).each do |i|
a = (a+1) % 256
j = (j + box[a]) % 256
tmp = box[a]
box[a] = box[j]
box[j] = tmp
ks = box[(box[a].to_i + box[j].to_i)%256]
result < < (str[i].ord ^ ks.to_i ).chr
end
if decode
if (result[0,10].to_i ==0 || (result[0,10].to_i - Time.now.to_i) > 0) && result[10,16] == (Digest::MD5.hexdigest(result[26..-1] + keyb)[0,16])
return result[26..-1]
else
return ''
end
else
return keyc+Base64.encode64(result).sub('=','')
end
end
[/code]
[解决办法]
支持!
[解决办法]
一直都不知道怎么用Ruby搞这些加密/解密的东东。
[解决办法]
是你自己改的吗