我们从Python开源项目中,提取了以下45个代码示例,用于说明如何使用rsa.PublicKey()。
def get_pwd_rsa(self, pwd, servertime, nonce): """ Get rsa2 encrypted password, using RSA module from https://pypi.python.org/pypi/rsa/3.1.1, documents can be accessed at http://stuvel.eu/files/python-rsa-doc/index.html """ #n, n parameter of RSA public key, which is published by WEIBO.COM #hardcoded here but you can also find it from values return from prelogin status above weibo_rsa_n = 'EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443' #e, exponent parameter of RSA public key, WEIBO uses 0x10001, which is 65537 in Decimal weibo_rsa_e = 65537 message = str(servertime) + '\t' + str(nonce) + '\n' + str(pwd) #construct WEIBO RSA Publickey using n and e above, note that n is a hex string key = rsa.PublicKey(int(weibo_rsa_n, 16), weibo_rsa_e) #get encrypted password encropy_pwd = rsa.encrypt(message, key) #trun back encrypted password binaries to hex string return binascii.b2a_hex(encropy_pwd)
def encrypt_password(p, st, nonce, pk, rsakv): """ Encrypting the password using rsa algorithm. p: password st: server time nonce: random value pk: public key rsakv: rsa key value """ pk = '0x' + pk pk = int(pk, 16) msg = str(st) + '\t' + str(nonce) + '\n' + p key = rsa.PublicKey(pk, 65537) psw = rsa.encrypt(msg.encode("utf-8"), key) psw = binascii.b2a_hex(psw) return decode(psw)
def receive_hispubkey(mypub=None): """ Try to get his public key from clipboard, saved to "hispub" return True if success else False """ if mypub is None: mypub = storage_get("mypub") try: rawdata = clipboard_read() hispub = d64(rawdata) if not isinstance(hispub, rsa.PublicKey): return False if hispub == mypub: return False # clipboard not changed else: storage_save("hispub",hispub) return True,hispub except: return False
def get(self): # checks if the user can create a new package entry # if so, returns a new secret # user then must post the signed package to this endpoint if not ENGINE.check_package(request.form['owner'], request.form['package']): # try to pull the users public key query = ENGINE.get_key(request.form['owner']) # in doing so, check if the user exists if query == None: return error_payload('Owner does not exist.') # construct the user's public key user_public_key = rsa.PublicKey(int(query[0]), int(query[1])) # create a new secret secret = random_string(53) # sign and store it in the db so no plain text instance exists in the universe server_signed_secret = str(rsa.encrypt(secret.encode('utf8'), KEY[0])) query = ENGINE.set_secret(request.form['owner'], server_signed_secret) # sign and send secret to user user_signed_secret = rsa.encrypt(secret.encode('utf8'), user_public_key) return success_payload(str(user_signed_secret), 'Package available to register.') else: return error_payload('Package already exists.')
def encrypt_passwd(self, passwd, pubkey, servertime, nonce): key = rsa.PublicKey(int(pubkey, 16), int('10001', 16)) message = str(servertime) + '\t' + str(nonce) + '\n' + str(passwd) passwd = rsa.encrypt(message.encode('utf-8'), key) return binascii.b2a_hex(passwd)
def get_pwd_rsa(self, pwd, servertime, nonce): """ Get rsa2 encrypted password, using RSA module from https://pypi.python.org/pypi/rsa/3.1.1, documents can be accessed at http://stuvel.eu/files/python-rsa-doc/index.html """ # n, n parameter of RSA public key, # which is published by WEIBO.COM # hardcoded here but you can also find # it from values return from prelogin status above weibo_rsa_n = ('EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090' 'CB2D245A87AC253062882729293E5506350508E7F9AA' '3BB77F4333231490F915F6D63C55FE2F08A49B353F44' '4AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D7' '8E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4F' 'E9E88A1BBE495927AC4A799B3181D6442443') # e, exponent parameter of RSA public key, # WEIBO uses 0x10001, which is 65537 in Decimal weibo_rsa_e = 65537 message = str(servertime) + '\t' + str(nonce) + '\n' + str(pwd) # construct WEIBO RSA Publickey using n and e above, # note that n is a hex string key = rsa.PublicKey(int(weibo_rsa_n, 16), weibo_rsa_e) # get encrypted password if six.PY3: message = message.encode() encropy_pwd = rsa.encrypt(message, key) # trun back encrypted password binaries to hex string sp = binascii.b2a_hex(encropy_pwd) if six.PY3: sp = sp.decode('utf-8') return sp
def get_pwd_rsa(self, pwd, servertime, nonce): """ Get rsa2 encrypted password, using RSA module from https://pypi.python.org/pypi/rsa/3.1.1, documents can be accessed at http://stuvel.eu/files/python-rsa-doc/index.html """ # n, n parameter of RSA public key, which is published by WEIBO.COM #hardcoded here but you can also find it from values return from prelogin status above #import pdb;pdb.set_trace() weibo_rsa_n = 'EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443' #e, exponent parameter of RSA public key, WEIBO uses 0x10001, which is 65537 in Decimal weibo_rsa_e = 65537 message = str(servertime) + '\t' + str(nonce) + '\n' + str(pwd) #construct WEIBO RSA Publickey using n and e above, note that n is a hex string key = rsa.PublicKey(int(weibo_rsa_n, 16), weibo_rsa_e) #get encrypted password if six.PY3: message = message.encode() encropy_pwd = rsa.encrypt(message, key) #trun back encrypted password binaries to hex string sp = binascii.b2a_hex(encropy_pwd) if six.PY3: sp = sp.decode('utf-8') return sp
def get_pwd(password, servertime, nonce, pubkey): rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey,65537)#create public key message = str(servertime) + '\t' + str(nonce) + '\n' + str(password)#create clear text passwd = rsa.encrypt(message, key)#cipher text passwd = binascii.b2a_hex(passwd)#convert the cipher text into hexadecimal return passwd
def generate_form_data(nonce, pubkey, servertime, rsakv, username, password): rsa_public_key = int(pubkey, 16) key = rsa.PublicKey(rsa_public_key, 65537) message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) passwd = rsa.encrypt(message, key) passwd = binascii.b2a_hex(passwd) username = urllib2.quote(username) username = base64.encodestring(username) form_data = { 'entry': 'weibo', 'gateway': '1', 'from': '', 'savestate': '7', 'useticket': '1', 'pagerefer': 'http://weibo.com/p/1005052679342531/home?from=page_100505&mod=TAB&pids=plc_main', 'vsnf': '1', 'su': username, 'service': 'miniblog', 'servertime': servertime, 'nonce': nonce, 'pwencode': 'rsa2', 'rsakv': rsakv, 'sp': passwd, 'sr': '1366*768', 'encoding': 'UTF-8', 'prelt': '115', 'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', 'returntype': 'META' } form_data = urllib.urlencode(form_data) return form_data # ====================?????cookie====================
def get_sp(self, passwd, pubkey, servertime, nonce): key = rsa.PublicKey(int(pubkey, 16), int('10001', 16)) message = str(servertime) + '\t' + str(nonce) + '\n' + str(passwd) passwd = rsa.encrypt(message.encode('utf-8'), key) return binascii.b2a_hex(passwd).decode('ascii')
def get_passwd(self, pubkey, servertime, nonce): import rsa rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) #???? message = str(servertime) + '\t' + str(nonce) + '\n' + str(self.password) #????js??????? passwd = rsa.encrypt(message, key) #?? passwd = binascii.b2a_hex(passwd) #????????16??? return passwd
def get_passwd(self, pubkey, servertime, nonce): import rsa rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) # ???? message = str(servertime) + '\t' + str(nonce) + '\n' + \ str(self.password) # ????js??????? passwd = rsa.encrypt(message, key) # ?? passwd = binascii.b2a_hex(passwd) # ????????16??? return passwd
def get_password(self, servertime, nonce, pubkey): """ get legal password, encrypt file: http://i.sso.sina.com.cn/js/ssologin.js """ string = (str(servertime) + '\t' + str(nonce) + '\n' + str(self.pass_word)).encode("utf-8") public_key = rsa.PublicKey(int(pubkey, 16), int("10001", 16)) password = rsa.encrypt(string, public_key) password = binascii.b2a_hex(password) return password.decode()
def __init__(self, username, password, shared_secret): self.session = requests.session() self.resp = requests.post("https://store.steampowered.com" + "/login/getrsakey/", data={ "username": str(username), }).json() self.rsa_modulus = self.rsa_params(str(username)).get('rsa_mod') self.rsa_exponent = self.rsa_params(str(username)).get('rsa_exp') self.rsa_timestamp = self.rsa_params(str(username)).get('rsa_timestamp') self.rsa_publickey = rsa.PublicKey(self.rsa_modulus, self.rsa_exponent) self.encrypted_password = base64.b64encode(rsa.encrypt(str(password).encode('UTF-8'), self.rsa_publickey)) self.loginReq = self.loginRequest(str(username), self.encrypted_password, str(shared_secret))
def _set_public_key_string(self, public_key_string): # remove PKCS#8 header if it exists if public_key_string.startswith(PKCS8_HEADER): public_key_string = public_key_string[len(PKCS8_HEADER):] # add the appropriate PEM header/footer public_key_string = self._add_pem_headers(public_key_string, "RSA PUBLIC KEY") self._public_key = PYRSA.PublicKey.load_pkcs1(public_key_string)
def _set_private_key_string(self, private_key_string): self._private_key = PYRSA.PrivateKey.load_pkcs1(private_key_string) self._public_key = PYRSA.PublicKey(self._private_key.n, self._private_key.e)
def get_password(password, servertime, nonce, pubkey): rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) # ????, message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) # ????js??????? message = message.encode("utf-8") passwd = rsa.encrypt(message, key) # ?? passwd = binascii.b2a_hex(passwd) # ????????16??? return passwd
def encode_pwd(self, password, servertime, nonce, pubkey): rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) # ???? message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) # ????js??????? passwd = rsa.encrypt(message, key) # ?? passwd = binascii.b2a_hex(passwd) # ????????16??? return passwd # ????????
def get_password(self, servertime, nonce, pubkey): """ get legal password """ string = (str(servertime) + "\t" + str(nonce) + "\n" + str(self.pass_word)).encode("utf-8") public_key = rsa.PublicKey(int(pubkey, 16), int("10001", 16)) password = rsa.encrypt(string, public_key) password = binascii.b2a_hex(password) return password.decode()
def get_password(password, servertime, nonce, pubkey): rsa_publickey = int(pubkey, 16) key = rsa.PublicKey(rsa_publickey, 65537) message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) message = message.encode("utf-8") passwd = rsa.encrypt(message, key) passwd = binascii.b2a_hex(passwd) return passwd # post data and get the next url
def __get_spwd(self): rsaPublickey = int(self.pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) #???? message = self.servertime + '\t' + self.nonce + '\n' + self.password #????js??????? passwd = rsa.encrypt(message, key) #?? passwd = binascii.b2a_hex(passwd) #????????16??? return passwd
def get_pwd(password, servertime, nonce, pubkey): rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) #???? message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) #??????????? passwd = rsa.encrypt(message, key) #?? passwd = binascii.b2a_hex(passwd) #????????16??? return passwd
def get_sp(password, pubkey, servertime, nonce): # rsa?? key = rsa.PublicKey(int(pubkey, 16), 65537) message = str(servertime) + '\t' + str(nonce) + '\n' + password passwd = rsa.encrypt(message.encode("utf8"), key) # ????????16?? sp = binascii.b2a_hex(passwd) # print(sp) return sp # ?????
def enPass(self): exponent = self.b64tohex(self.exponent) modulus = self.b64tohex(self.modulus) rsaKey = rsa.PublicKey(int(modulus, 16), int(exponent,16)) enPwd = binascii.b2a_hex(rsa.encrypt(self.pwd, rsaKey)) return self.hex2b64(enPwd)
def getSP(self): n=self.pubKey puk = rsa.PublicKey(int( n,16), 65537) #ct=ciphertext ct=(str(self.servertime)+'\t'+str(self.nonce)+'\n'+str(self.p)).encode() return binascii.b2a_hex(rsa.encrypt(ct, puk)).decode()
def get_password(self): # ????? key = rsa.PublicKey(int(self.pre_login_data[2], 16), 65537) # ?? message ????????????????????????? message = ('\t'.join([str(self.pre_login_data[0]), self.pre_login_data[1]]) + '\n' + self.raw_password).encode('utf-8') # print(message) self.password = binascii.b2a_hex(rsa.encrypt(message, key)) # print(self.password)
def getSp(self, servertime, nonce, pubkey): rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) message = str(servertime) + '\t' + str(nonce) + '\n' + str(self.password) passwd = rsa.encrypt(message, key) sp = binascii.b2a_hex(passwd) return sp
def encrypt_passwd(passwd, pubkey, servertime, nonce): key = rsa.PublicKey(int(pubkey, 16), int('10001', 16)) message = str(servertime) + '\t' + str(nonce) + '\n' + str(passwd) passwd = rsa.encrypt(message.encode('utf-8'), key) return binascii.b2a_hex(passwd)
def get_sp_rsa(passwd, servertime, nonce): # ?preloginõ,????,? weibo_rsa_n = 'EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443' weibo_rsa_e = 65537 # 10001?10 message = str(servertime) + '\t' + str(nonce) + '\n' + passwd key = rsa.PublicKey(int(weibo_rsa_n, 16), weibo_rsa_e) encropy_pwd = rsa.encrypt(message, key) return binascii.b2a_hex(encropy_pwd)
def _login(self, email, passwordd, certificate=None, loginName=url.systemname): self._thriftTransport.targetPath(url.LINE_AUTH_QUERY_PATH) session_json = url.get_json(url.parseUrl(url.LINE_SESSION_LINE_QUERY_PATH)) self.certificate = certificate session_key = session_json['session_key'] message = (chr(len(session_key)) + session_key + chr(len(email)) + email + chr(len(passwordd)) + passwordd).encode('utf-8') keyname, n, e = session_json['rsa_key'].split(",") pub_key = rsa.PublicKey(int(n, 16), int(e, 16)) crypto = rsa.encrypt(message, pub_key).encode('hex') self._thriftTransport.targetPath(url.LINE_AUTH_QUERY_PATH) result = self._client.loginWithIdentityCredentialForCertificate( IdentityProvider.LINE, keyname, crypto, True, '127.0.0.1', loginName, certificate) if result.type == 3: url._pincode = result.pinCode self.callback.Pinverified(url._pincode) getAccessKey = url.get_json( url.parseUrl(url.LINE_CERTIFICATE_PATH), allowHeader=True) self.verifier = getAccessKey['result']['verifier'] result = self._client.loginWithVerifierForCerificate(self.verifier) self.certificate = result.certificate self.authToken = result.authToken self.urls.set_Headers('X-Line-Access', result.authToken) self._thriftTransport.setAccesskey(self.authToken) self.onLogin() self._thriftTransport.targetPath(url.LINE_API_QUERY_PATH_FIR) elif result.type == 2: pass elif result.type == 1: self.authToken = result.authToken self.urls.set_Headers('X-Line-Access', result.authToken) self._thriftTransport.setAccesskey(self.authToken) self.onLogin() self._thriftTransport.targetPath(url.LINE_API_QUERY_PATH_FIR)
def get_password(password, servertime, nonce, pubkey): rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) # ???? message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) # ????js??????? message = message.encode("utf-8") passwd = rsa.encrypt(message, key) # ?? passwd = binascii.b2a_hex(passwd) # ????????16??? return passwd
def get_crypt_password(message): rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) passwd = rsa.encrypt(message, key) passwd = binascii.b2a_hex(passwd) return passwd
def getParameter(self): """ ???????????????? su??????sp????? servertime?nonce?pubket???sp???? rsakv???ticket?? """ '''base64?????''' bytesString = self.username.encode(encoding="utf-8") self.su = base64.b64encode(bytesString).decode('utf-8') '''??servertime?nonce?pubket?rsakv??''' params = { 'su': self.su, 'entry': 'openapi', 'callback': 'sinaSSOController.preloginCallBack', 'rsakt': 'mod', 'checkpin': '1', 'client': 'ssologin.js(v1.4.18)', '_': '1499082911503' } parameter_resp = requests.get(self.parameter_url, params=params) parameter = parameter_resp.text.split(',') self.servertime = parameter[1].split(':')[1] self.pcid = parameter[2].split(':')[1][1:-1] self.nonce = parameter[3].split(':')[1][1:-1] self.pubket = parameter[4].split(':')[1][1:-1] self.rsakv = parameter[5].split(':')[1][1:-1] '''????rsa??''' rsa_e = '65537' key = rsa.PublicKey(int(self.pubket, 16), int(rsa_e)) pw_string = str(self.servertime) + '\t' + str(self.nonce) + '\n' + str(self.password) ps = pw_string.encode(encoding="utf-8") pw_encypted = rsa.encrypt(ps, key) passwd = binascii.b2a_hex(pw_encypted) self.sp = passwd.decode()
def get_encrypted_pw(self,data): """ ?????? """ rsa_e = 65537 #0x10001 pw_string = str(data['servertime']) + '\t' + str(data['nonce']) + '\n' + str(self.password) key = rsa.PublicKey(int(data['pubkey'],16),rsa_e) pw_encypted = rsa.encrypt(pw_string.encode('utf-8'), key) self.password = '' #??password passwd = binascii.b2a_hex(pw_encypted) self.log.info("Password compilation completed...") # print(passwd) return passwd
def __crypt(self, mail, passwd, RSA): message = (chr(len(RSA.sessionKey)) + RSA.sessionKey + chr(len(mail)) + mail + chr(len(passwd)) + passwd).encode('utf-8') pub_key = rsa.PublicKey(int(RSA.nvalue, 16), int(RSA.evalue, 16)) crypto = rsa.encrypt(message, pub_key).encode('hex') return crypto
def userlogin(self,username,password): session = requests.Session() url_prelogin = 'http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.5)&_=1364875106625' url_login = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)' #get servertime,nonce, pubkey,rsakv resp = session.get(url_prelogin) json_data = re.search('\((.*)\)', resp.content).group(1) data = json.loads(json_data) servertime = data['servertime'] nonce = data['nonce'] pubkey = data['pubkey'] rsakv = data['rsakv'] # calculate su su = base64.b64encode(urllib.quote(username)) #calculate sp rsaPublickey= int(pubkey,16) key = rsa.PublicKey(rsaPublickey,65537) message = str(servertime) +'\t' + str(nonce) + '\n' + str(password) sp = binascii.b2a_hex(rsa.encrypt(message,key)) postdata = { 'entry': 'weibo', 'gateway': '1', 'from': '', 'savestate': '7', 'userticket': '1', 'ssosimplelogin': '1', 'vsnf': '1', 'vsnval': '', 'su': su, 'service': 'miniblog', 'servertime': servertime, 'nonce': nonce, 'pwencode': 'rsa2', 'sp': sp, 'encoding': 'UTF-8', 'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', 'returntype': 'META', 'rsakv' : rsakv, } resp = session.post(url_login,data = postdata) # print resp.headers login_url = re.findall('replace\(\'(.*)\'\)',resp.content) # respo = session.get(login_url[0]) self.session = session