2013-03-05 61 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你应该从语言没有问题,语言