def check(self,s): txt = c16.cbcdecrypt(s,self.iv,self.key) for c in txt: if ord(c) > 126 or ord(c) < 32: raise Exception(txt) return True
def check(self, s): txt = c16.cbcdecrypt(s, self.iv, self.key) #这个是cbc模式的解密函数 for c in txt: if ord(c) > 126 or ord(c) < 32: raise Exception(txt) return True
def myinput(self,s): s = s.replace(";","\;").replace("=","\=") return c16.cbcencrypt(self.prefix+s+self.suffix,self.iv,self.key) def check(self,s): txt = c16.cbcdecrypt(s,self.iv,self.key) for c in txt: if ord(c) > 126 or ord(c) < 32: raise Exception(txt) return True if __name__ == "__main__": t = c27() enc = list(t.myinput("")) test = "".join(enc[:16]+["\x00"]*16+enc[:16]+enc[16:]) dec = "" try: out = t.check(test) print "no exception, try one more time" sys.exit(1) except Exception, e: dec = str(e) key = [chr(ord(c1)^ord(c2)) for c1,c2 in zip(dec[:16],dec[32:48])] txt = c16.cbcdecrypt("".join(enc),"".join(key),"".join(key)) print txt.encode('string_escape')
txt = c16.cbcdecrypt(s, self.iv, self.key) #这个是cbc模式的解密函数 for c in txt: if ord(c) > 126 or ord(c) < 32: raise Exception(txt) return True #这个函数是检测每个字节是否在ascii允许的范围内(32-126) if __name__ == "__main__": t = c27() enc = list(t.myinput("hahahah")) #加密信息并且用list形式保存 print 'enc is :' + "".join(enc) + '\n' #这是加密之后的信息 test = "".join(enc[:16] + ["\x00"] * 16 + enc[:16] + enc[16:]) #这个地方把加密之后的信息故意填充把密文信息改变之后作为测试 print 'change enc is :' + test + '\n' #这个是把密文篡改之后的信息 dec = "" try: out = t.check(test) #这个函数是检测每个字节是否在ascii允许的范围内(32-126) print "no exception, try one more time" sys.exit(1) except Exception, e: dec = str(e) print 'error msg:' + dec + '\n' #这里是报错信息,报错信息会泄露key key = [chr(ord(c1) ^ ord(c2)) for c1, c2 in zip(dec[:16], dec[32:48])] #通过泄露信息还原出key print 'recover key :' + "".join(key) + '\n' txt = c16.cbcdecrypt("".join(enc), "".join(key), "".join(key)) #解密 print txt.encode('string_escape') #还原出消息