示例#1
0
    def decrypt(self, data, label, cpart):
        """"""
        rt      = RuTime(eval(__CALLER__()))
        data    = str(a2b_base64(bytes(self.unsign(data),'utf-8')),'utf-8')
        dataDec = ''
        hlst    = self.ck.getHashList(label,cpart,True)
        cp = ni = si = ei = rsz = 0
        
        for row in hlst['data']:
            rsz += row[2]+row[3]

        psize = ceil((len(data)-rsz)/cpart)
        for row in hlst['data']:
            si = ni + row[2]
            ei = si + psize
            if cp == cpart-1 :
                ei = -row[3]                
                if not si > len(data)+ei : pass
                else :
                    dataDec=dataDec[:len(data)+ei-si]
                    break
            dataDec += data[si:ei]
            ni  = ei + row[3]
            cp += 1
        dataDec = self.subdec(dataDec)
  
        rt.stop()
        return dataDec
示例#2
0
 def __init__(self, key, mark):
     """"""
     rt = RuTime(eval(__CALLER__()))
     self.key  = bytes(key,'utf-8')
     self.mark = mark
     self.ck   = ConfigKey(mark)
     rt.stop()
示例#3
0
 def _build(self,l=48):
     """"""
     rt = RuTime(eval(__CALLER__()))
     kg = KeyGen(l)
     k  = urlsafe_b64encode(bytes(kg.key,'utf-8'))
     rt.stop()
     return k
示例#4
0
 def unsign(self,data):
     """"""
     rt = RuTime(eval(__CALLER__()))
     d = data[64:]
     if not data[:64] == hash_sha256(self.mark + hash_sha256(d)):
         raise BadKeyException() 
     else: 
         rt.stop()
         return d
示例#5
0
 def __init__(self, key=None, salt=None, psize=19710000):
     """"""
     rt = RuTime(eval(__CALLER__()))
     if key  : self.key  = bytes(key,'utf-8')
     else :    self.key  = self._build()
     if salt ==None : self.salt = str(self.key[::-10])
     else :    self.salt = salt
     self.psize  = psize
     self.noiser = Noiser(self.key)
     self.rdmz   = Randomiz(1)
     rt.stop()
示例#6
0
    def encrypt(self, odata, label, cpart):
        """"""
        rt = RuTime(eval(__CALLER__()))
        data    = self.subenc(odata)
        hlst    = self.ck.getHashList(label,cpart,True)
        dataEnc = ''
        psize   = ceil(len(data)/cpart)
        cp      = 0
        for row in hlst['data']:
            dataEnc += self.ck.noiser.getNoise(row[2],True)+data[cp*psize:cp*psize+psize]+self.ck.noiser.getNoise(row[3],True)
            cp      += 1

        dataEnc = self.sign(str(b2a_base64(bytes(dataEnc,'utf-8')),'utf-8'))
        rt.stop()
        return dataEnc
示例#7
0
    def subenc(self,data):
        """"""        
        rt = RuTime(eval(__CALLER__()))
        s, i, lkey, ld = '', 0, len(self.key), len(data)

        for c in data:
            if i >= lkey: i = 0
            s += chr(self.enchr(ord(c),i,lkey-1-i))
            i += 1

        for c in self.key[::-12]:
            if ld-c < 0: continue            
            s = s[ld-c:ld][::-1]+s[0:ld-c]

        rt.stop()
        return s
示例#8
0
 def getHashList(self,name,count,noSorted=False):
     """"""
     rt = RuTime(eval(__CALLER__('"%s",%s,%i' % (name,count,noSorted))))
     self.rdmz.new(count)
     dic, lst, hroot = {}, [], hash_sha256(self.salt+name)
     for i in range(count) :  
         self.noiser.build(i)
         d     = str(i).rjust(2,'0')
         # part n°, hash, lns, lne, pos
         hpart = hash_sha256(self.salt+name+'.part'+d)[:-3]+str(ord(hroot[i])).rjust(3,'0')
         lst.append((d, hpart, self.noiser.lns, self.noiser.lne, self.rdmz.get()))
     dic['head'] = [name,count,hroot,self.getKey()]
     if not noSorted :
         lst = sorted(lst, key=lambda lst: lst[4])
     dic['data'] = lst
     rt.stop()
     return dic
示例#9
0
    def subdec(self,data):
        """"""
        rt = RuTime(eval(__CALLER__()))
        s, i, lkey, ld = '', 0, len(self.key), len(data)

        for c in self.key[::-12][::-1]:
            if ld-c < 0: continue
            data = data[c:ld]+data[0:c][::-1]

        for c in data:
            if i >= lkey: i = 0
            try:
                s += chr(self.dechr(ord(c),i,lkey-1-i))
            except ValueError as e:
                pass
            i += 1
        rt.stop()
        return s
示例#10
0
 def sign(self,data):
     """"""
     rt = RuTime(eval(__CALLER__()))
     sd = hash_sha256(self.mark + hash_sha256(data)) + data
     rt.stop()
     return sd