Menu

[r443]: / phprpc_3.0 / ruby / xxtea.rb  Maximize  Restore  History

Download this file

112 lines (98 with data), 3.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
############################################################
# #
# The implementation of PHPRPC Protocol 3.0 #
# #
# xxtea.rb #
# #
# Release 3.0.0 #
# Copyright (c) 2005-2008 by Team-PHPRPC #
# #
# WebSite: http://www.phprpc.org/ #
# http://www.phprpc.net/ #
# http://www.phprpc.com/ #
# http://sourceforge.net/projects/php-rpc/ #
# #
# Authors: Ma Bingyao <andot@ujn.edu.cn> #
# #
# This file may be distributed and/or modified under the #
# terms of the GNU Lesser General Public License (LGPL) #
# version 3.0 as published by the Free Software Foundation #
# and appearing in the included file LICENSE. #
# #
############################################################
#
# XXTEA encryption arithmetic library.
#
# Copyright (C) 2005-2008 Ma Bingyao <andot@ujn.edu.cn>
# Version: 1.0
# LastModified: Sep 2, 2008
# This library is free. You can redistribute it and/or modify it.
module Crypt
class XXTEA
class << self
private
Delta = 0x9E3779B9
def long2str(v, w)
n = (v.size - 1) << 2
if w then
m = v.last
if (m < n - 3) or (m > n) then return '' end
n = m
end
s = v.pack("V*")
return w ? s[0, n] : s
end
def str2long(s, w)
n = s.length;
v = s.ljust((4 - (n & 3) & 3) + n, "\0").unpack("V*")
if w then v[v.size] = n end
return v
end
def mx(z, y, sum, k, p, e)
return ((z >> 5 ^ ((y << 2) & 0xffffffff)) + (y >> 3 ^ ((z << 4) & 0xffffffff)) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z)) & 0xffffffff
end
public
def encrypt(str, key)
if str.empty? then return str end
v = str2long(str, true)
k = str2long(key.ljust(16, "\0"), false)
n = v.size - 1
z = v[n]
y = v[0]
sum = 0
(6 + 52 / (n + 1)).downto(1) { |q|
sum = (sum + Delta) & 0xffffffff
e = sum >> 2 & 3
for p in (0...n)
y = v[p + 1]
z = v[p] = (v[p] + mx(z, y, sum, k, p, e)) & 0xffffffff
end
y = v[0]
z = v[n] = (v[n] + mx(z, y, sum, k, n, e)) & 0xffffffff
}
long2str(v, false)
end
def decrypt(str, key)
if str.empty? then return str end
v = str2long(str, false)
k = str2long(key.ljust(16, "\0"), false)
n = v.size - 1
z = v[n]
y = v[0]
q = 6 + 52 / (n + 1)
sum = (q * Delta) & 0xffffffff
while (sum != 0)
e = sum >> 2 & 3
n.downto(1) { |p|
z = v[p - 1]
y = v[p] = (v[p] - mx(z, y, sum, k, p, e)) & 0xffffffff
}
z = v[n]
y = v[0] = (v[0] - mx(z, y, sum, k, 0, e)) & 0xffffffff
sum = (sum - Delta) & 0xffffffff
end
long2str(v, true)
end
end
end
end
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.