ruby 写discuz加密方法

xiuce 2008-09-02 05:03:27

function passport_encrypt($txt, $key) {
srand((double)microtime() * 1000000);
$encrypt_key = md5(rand(0, 32000));
$ctr = 0;
$tmp = '';
for($i = 0;$i < strlen($txt); $i++) {
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
$tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
}
return base64_encode(passport_key($tmp, $key));
}


function passport_key($txt, $encrypt_key) {
$encrypt_key = md5($encrypt_key);
$ctr = 0;
$tmp = '';
for($i = 0; $i < strlen($txt); $i++) {
$ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
$tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
}
return $tmp;
}


下边是用ruby写的,不过有点错误,麻烦大家帮忙找下,郁闷几天了!
  def encrypt(txt,key)
encrypt_key=Digest::MD5.hexdigest(rand(32000).to_s)
ctr=0
tmp=''
for i in (0..txt.length)
ctr = (ctr == encrypt_key.length ? 0 : ctr)
tmp += (encrypt_key[ctr].to_s+( txt[i] ^ encrypt_key[ctr]).to_s)
ctr+=1
end

return Base64.encode64(passport_key(tmp,key))
end


def passport_key(txt,key)
encrypt_key=Digest::MD5.hexdigest(key)
ctr=0
tmp=''
for i in (0..txt.length)
ctr= (ctr==encrypt_key.length ? 0 : ctr)
tmp += (txt[i] ^ encrypt_key[ctr]).to_s
ctr+=1
end

return tmp
end
...全文
211 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
CityOfAngels 2008-09-04
  • 打赏
  • 举报
回复
up
aotianlong 2008-09-04
  • 打赏
  • 举报
回复
class Crypt
#discuz encrypt function
def self.encrypt(txt, key)
encrypt_key = Digest::MD5.hexdigest(rand(32000).to_s);
ctr = 0
tmp = ''
Range.new(0,txt.length-1).each do |i|
ctr = (ctr==encrypt_key.length ? 0 : ctr)
tmp += encrypt_key[ctr].chr+(txt[i] ^ encrypt_key[ctr]).chr
ctr = ctr+1
end
Base64.encode64(self.key(tmp,key));
end


def self.decrypt(txt,key)
txt = self.key(Base64.decode64(txt),key)
tmp = ''
0.step(txt.length-1,2) do |i|
data = txt[i] ^ txt[i+1]
tmp += data.chr
end
x = self.decode(tmp)
print x.inspect
x
end


def self.key(txt, encrypt_key)
encrypt_key = Digest::MD5.hexdigest(encrypt_key);
ctr = 0;
tmp = '';
0.upto(txt.length-1) do |i|
ctr = ctr==encrypt_key.length ? 0 : ctr
tmp += (txt[i] ^ encrypt_key[ctr]).chr
ctr = ctr+1
end
tmp
end

def self.encode(hash)
raise "passport encode must recieve a hash" unless hash.instance_of? Hash
str = []
hash.each do |key,value|
str << "#{key}="+CGI::escape(value.to_s)
end
str.join("&")
end

def self.decode(txt)
txt.gsub!(/.+\?/,'')
params = Hash.new([].freeze)
txt.split(/[&;]/n).each do |pairs|
key, value = pairs.split('=',2).collect{|v| CGI::unescape(v) }
key = key.to_sym
if params.has_key?(key)
params[key].push(value)
else
params[key] = value
end
end
params
end
#end discuz encrypt function
end












刚学RUBY的时候写的,应该可以,我用过。我好像给你的邮件发送过,你可能没受到
多亏代码还在我的SVN里面
找了一下还在。

2,763

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ruby/Rails
社区管理员
  • Ruby/Rails社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧