[ Index ]

title

Body

[close]

/se3master/var/www/se3/includes/ -> decode.py (source)

```   1  # shop-js encrypted message decoder in Python
2  # Copyright John Hanna (c) 2002 under the terms of the GPL
3  # see http://shop-js.sf.net for details and latest version
4
5  debug=0
6
7  import privateKey
8  import sys
9
10  def rc4(key, string):
11      """Return string rc4 (de/en)crypted with RC4."""
12      s,i,j,klen=range(256),0,0,len(key)
13      for i in range(256):
14          j=(ord(key[i%klen])+s[i]+j)%256
15          s[i],s[j]=s[j],s[i]
16      for i in range(256):
17          j=(ord(key[i%klen])+s[i]+j)%256
18          s[i],s[j]=s[j],s[i]
19      r=''
20      for i in range(len(string)):
21          i2=i % 256
22          j=(s[i2]+j)%256
23          s[i2],s[j]=s[j],s[i2]
24          r+=chr(ord(string[i])^s[(s[i2]+s[j])%256])
25      return r
26
27  def inverse(x, n):
28       """Return the mod n inverse of x."""
29       y, a, b = n, 1, 0
30       while y>0:
31           x, (q, y) = y, divmod(x, y)
32           a, b = b, a - b*q
33       if a < 0:
34           a = a + n
35       assert x==1, "No inverse, GCD is %d" % x
36       return a
37
38
39  def crt_RSA(m, d, p, q):
40       """ Compute m**d mod p*q for RSA private key operations."""
41       xp = pow(m % p, d%(p-1), p)
42       xq = pow(m % q, d%(q-1), q)
43       t = ((xq - xp) * inverse(p, q)) % q
44       if t < 0:
45           t = t + q
46       return t * p + xp
47
48
49  b64s='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"'
50  def base64ToText(text):
51      r,m,a,c='',0,0,0
52      for i in text[:]:
53          c=b64s.find(i)
54          if(c >= 0) :
55               if(m):
56                     r += chr((c << (8-m))& 255 | a)
57               a = c >> m
58               m+=2
59               if(m==8):m=0
60      return r
61
62  def t2b(s):
63      r=0L
64      m=1L
65      for i in s[:]:
66          r+=m*ord(i)
67          m*=256L
68      return r
69
70  def b2t(b):
71      r=''
72      while(b):
73          r+=chr(b % 256)
74          b>>=8
75      return r
76
77  def fix(a):
78      r=0L
79      s=0
80      for i in a[:]:
81          r|=long(i) << s
82          s+=28
83      return r
84
85  def rsaDecode(key, text):
86      """ decode the text based on the given rsa key. """
87      # separate the session key from the text
88      text=base64ToText(text)
89      sessionKeyLength=ord(text[0])
90      sessionKeyEncryptedText=text[1:sessionKeyLength+1]
91      text=text[sessionKeyLength+1:]
92      sessionKeyEncrypted=t2b(sessionKeyEncryptedText)
93
94      # un-rsa the session key
95      sessionkey=crt_RSA(sessionKeyEncrypted,fix(key[0]),fix(key[1]),fix(key[2]))
96      #sessionkey=crt_RSA(sessionKeyEncrypted,fix(d),fix(p),fix(q))
97      sessionkey=b2t(sessionkey)
98
99      text=rc4(sessionkey,text)
100      return text
101
102  if  debug: