2017-08-31 4 views
0

最近我做了一些類似自動登錄python,但我是這方面的大一新生。如何在python中實現這個js加密腳本?

我想登錄到www.jd.com,使用Chrome瀏覽器獲取數據後之後,我發現這個

uuid:37ac1f08-0ed9-4e0d-a424-76c31d566915 
eid:ZLTFMPUYPUVH3AQWGB3I4YEJ5YR4EQLSXV7YKAX27FNOY3CPTY37EVDW755A2DUGP6GKOFADCU7JKKYCAMYM3QHAS4 
fp:b871dc2da5cf2bf85a6a5a56259e28e3 
_t:_ntscXrr 
loginType:f 
loginname:xxxxxxx 
nloginpwd:RAc2wPFCr7jwP5ocHh295pGBuZL9xUYzlWh108xqsp6o90x6KiHVTbw3Yn6NRz8YMDp%2BOHlT58oinO%2FuLwvysmD3XKazm0MYEulWseG2gotduYTywA6%2FrO1hUskfVjHuPoLu8r3stjNRQ0dnKF%2BvIxganMiDEUTiUmliAGQqnWc%3D 
chkRememberMe: 
authcode: 
pubKey:MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21TAIfT%2FvexbobzH7Q8SUdP5uDPXEBKzOjx2L28y7Xs1d9v3tdPfKI2LR7PAzWBmDMn8riHrDDNpUpJnlAGUqJG9ooPn8j7YNpcxCa1iybOlc2kEhmJn5uwoanQq%2BCA6agNkqly2H4j6wIDAQAB 
sa_token:B68C442BE645754F33277E701208059007998C0A52BF7A8EAD6AD256C8D009D929F000C9DF4192A57234E3EA6F615E156C81EFA53D580517BB9357FB9516A01E25761124AE9AF7B3CFA3C38D38484A73343117232D8C7101034817F5DC7B6F52055B86745C3B42647752AEED916BA60EBBC84732E823766B234DC3A36C47691794488C0EAF9A459DC55C70B43B5F9AE3A233810AAC8D52FE65CB29B0C97A162039D4626DD8AFF4ECE8ABDB2411B3D4509C293CD344C3BAF14F059FF462D18C8761724FAF12E2BF3C590E14AE8198C7542C8A575F7FAD5B021BC1A4C852AB71E972157546E442CF0E9E7ABA667A02DD8386375595080A9E9A1B232DCE0944244FDC6AB4A499CC881E1BB8BA47831877F6AD6CF02FFF5671C60461E90517D1761B40FD6CA361677595F096C4E5250D72FDCC6E1FF89771AE1B0F1C89B7DCAFB88BD6F068F47850A7EBC747F35939552C7E32B28E6D347D5AEB78B9334779D4896431CCA166537C67690687B1DDA7CC9881914D2A9F25CAF2B80EB8E2D0DFED09EBB766287A6E34179DCC9530DF3D4FCD28C17845571E587FFA3FD0B69E3ACBC80FCBCE115EFD6CBC87BB8ED4D95AAAA680 
seqSid:1777115819395099100 

這裏的密碼是加密的,後來我發現這個JS代碼

function getEntryptPwd(pwd){ 
    var pubKey = $('#pubKey').val(); 
    if(!pwd || !pubKey || !SysConfig.encryptInfo){ 
     return pwd; 
    } 
    var encrypt = new JSEncrypt(); 
    encrypt.setPublicKey(pubKey); 
    return encrypt.encrypt(pwd); 
} 

但是,我不知道如何在python中做同樣的事情,誰能告訴我如何做到這一點?

任何建議將不勝感激。

回答

0

這裏是我使用從html獲得的pubkey加密密碼的方法。

  1. 請求登錄網址獲取pubkey(還有其他信息)。
  2. 安裝第三方模塊rsa。

    PIP安裝RSA

  3. 運行以下腳本加密基於PUBKEY您的密碼。

而且你可以找到我的代碼here。 :)

<input type="hidden" name="pubKey" id="pubKey" value="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21TAIfT/vexbobzH7Q8SUdP5uDPXEBKzOjx2L28y7Xs1d9v3tdPfKI2LR7PAzWBmDMn8riHrDDNpUpJnlAGUqJG9ooPn8j7YNpcxCa1iybOlc2kEhmJn5uwoanQq+CA6agNkqly2H4j6wIDAQAB" class="hide"/> 

(admin) $ cat encrypt.py 
import rsa 
import base64 
import os 

def bytes2hex(s): 
    return s.decode() 

def bytes2base64(s): 
    return bytes2hex(base64.standard_b64encode(s)) 

class RSA(): 

    def __init__(self): 
     # self.pubkey = self.load_pub_key(pubkey) if pubkey else None 
     self.pubkey = None 
     pass 

    def form_pem_pub_key(self, keystr,): 
     """ 
     Fortmat a public key string to PEM format, in which start with 
     BEGIN PUBLIC KEY and end with END PUBLIC KEY. 
     Parameters: 
     keystr -- the public key string of PEM-encoded(base64) 
     Returns: 
       -- Formatted public key string 
     """ 
     import textwrap 
     line_len = 64 
     begin = ['-----BEGIN PUBLIC KEY-----'] 
     end = ['-----END PUBLIC KEY-----'] 
     return '\n'.join(begin + textwrap.wrap(keystr, line_len) + end) 

    def check_pem_key_str(self, keystr,): 
     return False 
     pass 


    def load_pem_pub_key(self, pubkey,): 
     if isinstance(pubkey, str): 
      keystr = pubkey if self.check_pem_key_str(pubkey) else self.form_pem_pub_key(pubkey) 
     self.pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(keystr) 

    def encrypt(self, msg): 
     if not self.pubkey: 
      raise ValueError('Pubic key is empty, please load it first.') 
     # convert string or other type to bytes 
     msg = msg if isinstance(msg, bytes) else str(msg).encode() 
     return bytes2base64(rsa.encrypt(msg, self.pubkey)) 

def encrypt_password(pubkey, password): 
    rsa = RSA() 
    rsa.load_pem_pub_key(pubkey) 
    return rsa.encrypt(password) 

password = 'test' 
pubkey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDC7kw8r6tq43pwApYvkJ5laljaN9BZb21TAIfT/vexbobzH7Q8SUdP5uDPXEBKzOjx2L28y7Xs1d9v3tdPfKI2LR7PAzWBmDMn8riHrDDNpUpJnlAGUqJG9ooPn8j7YNpcxCa1iybOlc2kEhmJn5uwoanQq+CA6agNkqly2H4j6wIDAQAB' 

print(encrypt_password(pubkey, password)) 

(admin) $ python encrypt.py 
nQJvsRspY+zAnywU9YsSTNHb/OoNOLLbADsenBwOlDHnv//UUPpBidA4n4pN7Frm0iIRQAJT1hRNAu/tASZjihtWiOYuwD+XFos2Tmk+SLRIc6VvqvAL9CJFLrzbIS3tgbx9vkyM30Qy6ENFOyOcpR7nV93xY82F1dB7bRsuNGU=