在CodeIgniter中编码,然后在Python中解码
我已将凭据(如登录名、密码)存储在CI中,如-在CodeIgniter中编码,然后在Python中解码,codeigniter,encryption,python-2.7,Codeigniter,Encryption,Python 2.7,我已将凭据(如登录名、密码)存储在CI中,如- $login = "foo@example.com"; $this->load->library('encrypt'); $encrypted = $this->encrypt->encode($login); //Then stored it in db 现在,我的一个python代码想要获得实际的凭据。通过观察CI的解码,我用python编写了自己的解密模块- // Like CI get_key def getKey
$login = "foo@example.com";
$this->load->library('encrypt');
$encrypted = $this->encrypt->encode($login);
//Then stored it in db
现在,我的一个python代码想要获得实际的凭据。通过观察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
以上所有方法都写在Decrypt模块的Decrypt类中
因此,当我将加密字符串传递给decode方法时,会得到一些奇怪的unicode字符串,而不是实际的凭据。当我用CI检查它时,上面代码中的
xorMerge
不会产生与CI中的\uxor\umerge
相同的输出。我做错了什么 如果您使用相同的编码/解码标准
和相同的加密密钥
,那么不同语言之间应该没有问题我编写了一个python模块来“使用加密库解密CodeIgniter中加密的内容”<代码>签出代码