yangxin0
2017-04-07 14:18:29 +08:00
我写过一个大规模人人爬虫程序, 一个建议不要使用混淆的代码,登录这一块使用的 RSA(非标准)对密码进行加密, 密钥的长度 256.
class RSAPubKey:
def __init__(self, exponent, modulo):
self.exponent = self.encode(exponent)
self.modulo = self.encode(modulo)
def encode(self, data):
return int(data, 16)
class RSAEncrypt:
def __init__(self, key):
self.key = key
def encrypt(self, text):
text = self.encode_text(text)
cipher = self.fast_exp(text, self.key.exponent, self.key.modulo)
return format(cipher, 'x')
def debug_bignum(self, num, prefix):
print("==> %s <%s>" % (prefix, format(num, 'x')))
def fast_exp(self, base, exponent, modulo):
result = 1
if exponent == 0:
return result
while exponent > 0:
if exponent & 1 == 1:
result = result * base % modulo
exponent /= 2
base = (base * base) % modulo
return result
def better_hex(self, num):
while num > 0:
pass
def encode_text(self, text):
if isinstance(text, int):
return text
result = 0
for c in text[::-1]:
result = result << 8
result += ord(c)
return result
if __name__ == '__main__':
expect = "24cf15330c44dae8846d386b97be97b278b42b2e54894c16dd682a8046c99bd8"
exponent = "10001"
modulo = "8cf5d22c6857c041a50947cf178b1519d5ab7befe7e523a0c93220c0a44593a9"
key = RSAPubKey(exponent, modulo)
rsa = RSAEncrypt(key)
cipher = rsa.encrypt("helloworld")
print "cipher: %s" % (cipher)
print "expect: %s" % (expect)