2013-03-05 88 views
2

我已經像CI編碼它們的存儲一樣登錄名,密碼憑據。因此,通過觀察CI的解碼,我寫我自己的解密模塊中的蟒蛇 -編碼的笨然後解碼在Python

// Like CI get_key 
def getKey(self, key): 
      self.log.info("In getKey method with key : %s"%key) 
      md5Key = hashlib.md5() 
      md5Key.update(key) 
      return md5Key.hexdigest() 

// Like CI base64_decode 
def getBase64Decode(self, encString): 
      self.log.info("In getBase64Decode.") 
      b64DecString = base64.b64decode(encString) 
      return b64DecString 

// Like CI _xor_decode 
def xorDecode(self, string, key): 
      self.log.info("In xorDecode method with string : %s and key : %s"%(string, key)) 
      mString = self.xorMerge(string, key) 
      if mString == self.FAILED: 
        self.log.info("xorMerge Failed!") 
        return self.FAILED 
      self.log.info("xor Merge returned %s"%mString) 
      dec = '' 
      for (x, y) in izip(mString[1:], cycle(mString)): 
        dec += ''.join(chr(ord(x)^ord(y))) 

// Like CI _xor_merge 
def xorMerge(self, string, key): 
      self.log.info("In xorMerge method. with string : %s and key : %s"%(string, key)) 
      hashString = self.hashMethod(key) 
      if hashString == self.FAILED: 
        self.log.info("hasMethod failed!") 
        return self.FAILED 
      self.log.info("hash method retured : %s"%hashString) 
      xored = '' 
      for (x, y) in izip(string, cycle(hashString)): 
        xored += ''.join(chr(ord(x)^ord(y))) 

// Like CI hash 
def hashMethod(self, key): 
      self.log.info("In hash method with key : %s"%key) 
      hashStr = '' 
      try: 
        hashStr = hashlib.sha1(key).hexdigest() 
      except Exception, e: 
        self.log.info("Exception in sha1 %s"%str(e)) 
        return self.FAILED 
      return hashStr 

// Like CI decode 
def decode(self, string): 
      self.log.info("In decode method. Decoding string : %s"%string) 
      securitySection = "security" 
      keyItem = "key" 
      key = self.config.get(securitySection, keyItem) 
      if not key: 
        self.log.info("Key Invalid") 
        return self.FAILED 
      key = self.getKey(key) 
      self.log.info("Encrypted key : %s"%key) 
      dec = self.getBase64Decode(string) 
      self.log.info("b64decoded string : %s"%dec) 
      xorDec = self.xorDecode(dec, key) 
      if xorDec == self.FAILED: 
        self.log.info("Decoding failed!") 
        return self.FAILED 
      self.log.info("Decoded string: %s"%xorDec) 
      return xorDec 

上述所有方法都寫在解密類解密模塊。

所以,當我將加密字符串傳遞給解碼方法時,我得到了一些奇怪的unicode字符串,而不是實際的憑據。當我使用CI對其進行檢查時,xorMerge從上面的代碼中得出的結果與CI中給出的_xor_merge不同。我究竟做錯了什麼?

回答

0

我寫了一個Python模塊,以「解密什麼是使用加密庫笨加密」。 checkout密碼here

0

如果使用相同的encoding/decoding standard和與same encryption key你應該從語言沒有問題,語言