pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Python pastebin - collaborative debugging tool View Help


Posted by devloop on Sun 8 Feb 15:24
report abuse | download | new post

  1. #!/usr/bin/env python
  2. import random
  3. from numpy import *
  4.  
  5. # un byte : entier sur 8 bits
  6. # un char : caractere sur 8 bits
  7. # un block : 4 bytes
  8.  
  9. class Crypt:
  10.  
  11.   a2b={}
  12.   b2a={}
  13.  
  14.   def __init__(self):
  15.     i=0
  16.     # Chaine de 64 caracteres utilises pour les codages ASCII
  17.     for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_":
  18.       self.a2b[c]=i
  19.       i=i+1
  20.     for k,v in self.a2b.items():
  21.       self.b2a[v]=k
  22.  
  23.   def str2bytes(self,s):
  24.     t=[]
  25.     for c in s:
  26.       t.append(ord(c))
  27.     return t
  28.  
  29.   def bytes2str(self,t):
  30.     s=""
  31.     for x in t.values():
  32.       s+=chr(x)
  33.     return s
  34.  
  35.   def blocks2bytes(self,blocks):
  36.     bytes={}
  37.     iby=0
  38.     ibl=0
  39.     for a in range(len(blocks)):
  40.       bytes[iby]=255 & blocks[ibl] >> 24
  41.       iby+=1
  42.       bytes[iby]=255 & blocks[ibl] >> 16
  43.       iby+=1
  44.       bytes[iby]=255 & blocks[ibl] >> 8
  45.       iby+=1
  46.       bytes[iby]=255 & blocks[ibl]
  47.       iby+=1
  48.       ibl+=1
  49.     return bytes
  50.  
  51.   def bytes2blocks(self,bytes):
  52.     # retour un tableau de long (sur 8 octets)
  53.     blocks={}
  54.     iby=0
  55.     ibl=0
  56.     while True:
  57.       blocks[ibl]=(255 & bytes[iby]) << 24
  58.       iby+=1
  59.       if iby>=len(bytes):
  60.         break
  61.       blocks[ibl] |= (255 & bytes[iby]) << 16
  62.       iby+=1
  63.       if iby>=len(bytes):
  64.         break
  65.       blocks[ibl] |= (255 & bytes[iby]) << 8
  66.       iby+=1
  67.       if iby>=len(bytes):
  68.         break
  69.       blocks[ibl] |= (255 & bytes[iby])
  70.       iby+=1
  71.       if iby>=len(bytes):
  72.         break
  73.       ibl+=1
  74.     return blocks
  75.  
  76.   def binary2str(self,bks):
  77.     return self.bytes2str(self.blocks2bytes(bks))
  78.  
  79.   def str2binary(self,s):
  80.     return self.bytes2blocks(self.str2bytes(s))
  81.  
  82.   def xor_blocks(self,blk1,blk2):
  83.     # xor of two 8-byte blocks
  84.     blk={}
  85.     blk[0]=blk1[0]^blk2[0]
  86.     blk[1]=blk1[1]^blk2[1]
  87.     return blk
  88.  
  89.  
  90.   def binarydigest(self,str1,keystr=""):
  91.     key=[1633837924L,1650680933L,1667523942L,1684366951L]
  92.     c0=[1633837924L,1650680933L]
  93.     c1=c0
  94.  
  95.     v0={}
  96.     v1={}
  97.     blocks=self.bytes2blocks(self.digest_pad(self.str2bytes(str1)))
  98.  
  99.     ibl=0
  100.     swap=0
  101.     while ibl<len(blocks):
  102.       v0[0]=blocks[ibl]
  103.       ibl+=1
  104.       v0[1]=blocks[ibl]
  105.       ibl+=1
  106.  
  107.       v1[0]=blocks[ibl]
  108.       ibl+=1
  109.       v1[1]=blocks[ibl]
  110.       ibl+=1
  111.  
  112.       c0=self.tea_code(self.xor_blocks(v0,c0),key)
  113.       c1=self.tea_code(self.xor_blocks(v1,c1),key)
  114.  
  115.       swap=c0[0]
  116.       c0[0]=c0[1]
  117.       c0[1]=c1[0]
  118.       c1[0]=c1[1]
  119.       c1[1]=swap
  120.  
  121.     # mix up the two cipher blocks with a 32-bit left rotation...
  122.     swap={}
  123.     swap[0]=c0[0]
  124.     swap[1]=c0[1]
  125.     swap[2]=c1[0]
  126.     swap[3]=c1[1]
  127.     return swap
  128.  
  129.   def asciidigest(s):
  130.     return self.binary2ascii(self.binarydigest(s))
  131.  
  132.   def digest_pad(self,bytearray):
  133.     newarray={}
  134.     npads=15-(len(bytearray)%16)
  135.     newarray[0]=npads
  136.     ina=1
  137.     iba=0
  138.     for a in range(len(bytearray)):
  139.       newarray[ina]=bytearray[iba]
  140.       ina+=1
  141.       iba+=1
  142.     for a in range(npads,0,-1):
  143.       newarray[ina]=0
  144.       ina+=1
  145.     return newarray
  146.  
  147.   def rand_byte(self):
  148.     return random.randint(0,255)
  149.  
  150.  
  151.   def pad(self,bytearray):
  152.     newarray={}
  153.     npads=7-len(bytearray)%8
  154.  
  155.     # 248 | 7 = 255 ou encore F8 | 07 = FF
  156.     # ainsi les premiers bits de la premiere case du tableau n'ont pas d'interet
  157.     # d'ou l'utilisation de rand_byte()
  158.     # les 7 derniers bits permettent de retrouver le nombre d'octets de bourrage
  159.     newarray[0]=248 & self.rand_byte() | 7 & npads
  160.     ina=1
  161.     iba=0
  162.     for a in range(len(bytearray)):
  163.       newarray[ina]=bytearray[iba]
  164.       ina+=1
  165.       iba+=1
  166.     for a in range(npads,0,-1):
  167.       newarray[ina]=self.rand_byte()
  168.       ina+=1
  169.     return newarray
  170.  
  171.   def unpad(self,bytearray):
  172.     newarray={}
  173.     # on calcule la taille effective du tablea sans les octets de bourrage
  174.     nba=len(bytearray) - (7 & bytearray[0])
  175.     ina=0
  176.     iba=1
  177.     while iba<nba:
  178.       newarray[ina]=bytearray[iba]
  179.       ina+=1
  180.       iba+=1
  181.     return newarray
  182.  
  183.   def bytes2ascii(self,b):
  184.     s=""
  185.     ib=0
  186.     b1=0
  187.     b2=0
  188.     b3=0
  189.     carry=0
  190.  
  191.     # on prend 3 octets (soit 24 bits) que l'on decoupe en 4 blocks de 6 bits
  192.     # soit 64 valeurs possibles qui servent comme index pour un caractere ASCII
  193.     while True:
  194.       if ib>=len(b):
  195.         break
  196.       b1=255 & b[ib]
  197.       # 63 = masque 0x3F - elimine les deux premiers bits (poids fort)
  198.       s+=self.b2a[63 & b1 >> 2]
  199.       # carry prend les deux derniers bits (poids faible)
  200.       carry=3 & b1
  201.       ib+=1
  202.       if ib>=len(b):
  203.         s+=self.b2a[carry << 4]
  204.         break
  205.       b2=255 & b[ib]
  206.       # 240 = masque 0xF0 - elimine les quatres derniers bits
  207.       s+=self.b2a[240 & carry << 4 | b2 >> 4]
  208.       # carry prend les quatres derniers bits (poids faible)
  209.       carry=15 & b2
  210.       ib+=1
  211.       if ib>=len(b):
  212.         s+=self.b2a[carry << 2]
  213.         break
  214.       b3=255 & b[ib]
  215.       # 60 = masque 0x3C - elimine les deux premiers et deux derniers bits
  216.       # 63 = masque 0x3F - elimine les deux premiers bits (poids fort)
  217.       s+=self.b2a[60 & carry << 2 | b3 >> 6]+self.b2a[63 & b3]
  218.       ib+=1
  219.       if ib%36==0:
  220.         s+="\n"
  221.     return s
  222.  
  223.   def ascii2bytes(self,a):
  224.     la=len(a)
  225.     ia=-1 # pointeur sur chaine
  226.     ib=0 # pointeur sur tableau
  227.     b={} # tableau de retour
  228.     carry=0
  229.  
  230.     while True:
  231.       while True:
  232.         ia+=1
  233.         if ia>=la:
  234.           return b
  235.         if a[ia] in self.a2b.keys():
  236.           break
  237.       b[ib]=self.a2b[a[ia]] << 2
  238.       while True:
  239.         ia+=1
  240.         if ia>=la:
  241.           return b
  242.         if a[ia] in self.a2b.keys():
  243.           break
  244.       carry=self.a2b[a[ia]]
  245.       b[ib] |= carry >> 4
  246.       ib+=1
  247.       carry &= 15
  248.  
  249.       if carry==0 and ia==la-1:
  250.         return b
  251.  
  252.       b[ib]=carry<<4
  253.       while True:
  254.         ia+=1
  255.         if ia>=la:
  256.           return b
  257.         if a[ia] in self.a2b.keys():
  258.           break
  259.  
  260.       carry=self.a2b[a[ia]]
  261.       b[ib] |= carry >> 2
  262.       ib+=1
  263.       carry &= 3
  264.  
  265.       if carry==0 and ia==la-1:
  266.         return b
  267.       b[ib]=carry << 6
  268.       while True:
  269.         ia+=1
  270.         if ia>=la:
  271.           return b
  272.         if a[ia] in self.a2b.keys():
  273.           break
  274.       b[ib] |= self.a2b[a[ia]]
  275.       ib+=1
  276.     return b
  277.  
  278.   def ascii2binary(self,s):
  279.     return self.bytes2blocks(self.ascii2bytes(s))
  280.  
  281.   def binary2ascii(self,t):
  282.     return self.bytes2ascii(self.blocks2bytes(t))
  283.  
  284.   # cf. priorite des operateurs
  285.   # http://docs.python.org/reference/expressions.html
  286.   def tea_code(self,v,k):
  287.     # c'est du XTEA : http://en.wikipedia.org/wiki/XTEA
  288.     # TEA. 2-int (64-bit) (2x 4 octets) cyphertext block in v. 4-int (128-bit) key in k.
  289.     v0=uint32(v[0])
  290.     v1=uint32(v[1])
  291.     sum=0
  292.     tmp=0
  293.     tmp2=0
  294.  
  295.     for i in range(0,32):
  296.       tmp=uint32( add( (self.SHL(v1,4) ^ self.USHR(v1,5)) , uint32(v1) ) )
  297.       tmp2=uint32( sum+k[sum&3])
  298.  
  299.       v0=uint32( add(v0, tmp^tmp2))
  300.  
  301.       sum= uint32(sum - 1640531527) # TEA magic number 0x9e3779b9
  302.  
  303.       tmp=uint32( add( (self.SHL(v0,4) ^ self.USHR(v0,5)) , uint32(v0) ) )
  304.       tmp2=uint32( sum+k[(sum >> 11) & 3])
  305.       v1=uint32( add(v1,tmp^tmp2))
  306.       #print "v0:",v0,"v1:",v1
  307.     return [v0,v1]
  308.  
  309.   def tea_decode(self,v,k):
  310.     v0=uint32(v[0])
  311.     v1=uint32(v[1])
  312.     sum=uint32(-957401312)  # TEA magic number 0x9e3779b9<<5
  313.  
  314.     for i in range(0,32):
  315.       tmp=uint32( add( (self.SHL(v0,4) ^ self.USHR(v0,5)) , uint32(v0) ) )
  316.       tmp2=uint32( sum+k[(sum >> 11) & 3])
  317.       v1 = uint32( subtract(v1, tmp^tmp2))
  318.  
  319.       sum=uint32(sum + 1640531527) # TEA magic number 0x9e3779b9
  320.  
  321.       tmp=uint32( add( (self.SHL(v1,4) ^ self.USHR(v1,5)) , uint(v1) ) )
  322.       tmp2=uint32( sum+k[sum&3])
  323.       v0 = uint32( subtract(v0, tmp^tmp2))
  324.     return [v0,v1]
  325.  
  326.   def decrypt(self,M,K=""):
  327.     if K=="":
  328.       print "No key!!"
  329.       return False
  330.     if M=="":
  331.       return ""
  332.     bd=self.binarydigest(K)
  333.     bin=self.ascii2binary(M)
  334.     i=0
  335.     j=0
  336.     t0=[1633837924,1650680933]
  337.     t1=[]
  338.     t2={}
  339.     t3={}
  340.  
  341.     while i<len(bin):
  342.       t2[0]=bin[i]
  343.       i+=1
  344.       t2[1]=bin[i]
  345.       i+=1
  346.       t1=self.xor_blocks(t0,self.tea_decode(t2,bd))
  347.       t3[j]=t1[0]
  348.       j+=1
  349.       t3[j]=t1[1]
  350.       j+=1
  351.       t0[0]=t2[0]
  352.       t0[1]=t2[1]
  353.     return self.bytes2str(self.unpad(self.blocks2bytes(t3)))
  354.  
  355.   def encrypt(self,M="",K=""):
  356.     if K=="":
  357.       print "No key!!"
  358.       return False
  359.     if M=="":
  360.       return ""
  361.     bd=self.binarydigest(K)
  362.     bks=self.bytes2blocks(self.pad(self.str2bytes(M)))
  363.     i=0
  364.     j=0
  365.     t0=[1633837924,1650680933]
  366.     t1={}
  367.     t2={}
  368.  
  369.     while i<len(bks):
  370.       t1[0]=bks[i]
  371.       i+=1
  372.       t1[1]=bks[i]
  373.       i+=1
  374.       t0=self.tea_code(self.xor_blocks(t1,t0),bd)
  375.       t2[j]=t0[0]
  376.       j+=1
  377.       t2[j]=t0[1]
  378.       j+=1
  379.  
  380.     return self.binary2ascii(t2)
  381.  
  382.   # fonctions faits maisons pour les decalages de bits sur entier de 32 bits non signes
  383.   def USHR(self,n,d):
  384.     if n>=0:
  385.       return n>>d
  386.     else:
  387.       return (n>>d)&(0xffffffff>>d)
  388.  
  389.   def SHL(self,n,d):
  390.     mask=n&(0xffffffff>>d)
  391.     return mask<<d
  392.  
  393.   # fonction trouve sur le web, affiche un chiffre sous sa forme binaire
  394.   def Denary2Binary(self,n):
  395.     '''convert denary (base 10) integer n to binary string bStr'''
  396.     bStr = ''
  397.     if n < 0:  raise ValueError, "must be a positive"
  398.     if n == 0: return '0'
  399.     while n > 0:
  400.       bStr = str(n % 2) + bStr
  401.       n = n >> 1
  402.     return bStr
  403.  
  404. if __name__ == "__main__":
  405.   p="plop"
  406.   ep="ep"
  407.   v="1"
  408.   salt=""
  409.   for i in range(0,8):
  410.     salt+=str(random.randint(0,9))
  411.  
  412.   json = '{"p":"'+p+'","ep":"'
  413.   json+=ep+'","v":"'+v+'","s":"'+salt+'"}'
  414.  
  415.   c=Crypt()
  416.   print c.decrypt("q-mbHsiVngxbuIpQwvSCkEKSpfhfhNM7ujQB4fUcLXDbyF4cZABwsi8Vxa2n62deHjbDeZrqXTdGN4UkLDdogiiiOdpYWO9i68hX_AKZpeGtlOmyviP4JOxh13eKM7JWVXzjU3CtHefbvHiFrURTU4u6m8mmGZbuAadfnQ6UR4cqSC6bVipieCnTSpLpFQAcoXx8X86R5SmBpQ6FzQDfLXZJZhQ4MP_A4R3tBgdjSoCtbRlr7XAUV2DPe8ezkoxCq2YduYa_uOmGmB-TABOa--PMXf_XyueJi3puKyuwoJCZFfo1VFR0oA","I:NTnd7;+)WK?[:@S2ov")

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post