本文整理匯總了Python中rsa.encrypt方法的典型用法代碼示例。如果您正苦於以下問題:Python rsa.encrypt方法的具體用法?Python rsa.encrypt怎麽用?Python rsa.encrypt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rsa
的用法示例。
在下文中一共展示了rsa.encrypt方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: aes_encrypt
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def aes_encrypt(self, data):
count = 0
encrypt = []
if common.is_python2x():
for i in data:
encrypt.append(chr(ord(i) ^ ord(self.aes[count % len(self.aes)])))
count += 1
return b"".join(encrypt)
else:
for i in data:
encrypt.append(i ^ self.aes[count % len(self.aes)])
count += 1
return bytes(encrypt)
#aes_obj_enc = AES.new(self.aes, AES.MODE_CBC, self.iv)
#return aes_obj_enc.encrypt(pad(data))
示例2: to_encrypt
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def to_encrypt(cls, message, public_path=None, public_key=None, be_base64=True):
# 非對稱加密
"""
:param message: 待加密字符串或者字節
:param public_path: 公鑰路徑 二選一
:param public_key: 公鑰字符串 二選一
:return: base64密文字符串
"""
message = cls.check_message(message)
public_key_obj = cls.load_public_key(public_path, public_key)
cryto_msg = rsa.encrypt(message, public_key_obj) # 生成加密文本
if be_base64: return base64.b64encode(cryto_msg).decode("utf-8") # 將加密文本轉化為 base64 編碼
return cryto_msg # 將字節類型的 base64 編碼轉化為字符串類型
### 當前時間
示例3: Creat_Return_Token
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def Creat_Return_Token(self, token_crypto):
tag = bytes("NQZ",encoding="utf8")
# with open('../project/Helper/pubkey.pem','r') as f:
# pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
with open('../project/Helper/privkey.pem','r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
token_message = token_crypto
# token_crypto = rsa.encrypt(token_message.encode(), pubkey)
# 不進行公鑰加密
# 直接反饋加上標準內容的信息
token_crypto = bytes(token_crypto, encoding='utf8') + tag
signature = rsa.sign(token_message.encode(), privkey, 'SHA-1')
print("token message encode = ", token_message.encode())
# 利用私鑰對信息進行簽名
signature = base64.encodestring(signature)
return (token_crypto, signature)
# 返回生成的token 和 sign 簽名值
示例4: __crypt
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
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
示例5: encrypt
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def encrypt(fingerprint, data, *, use_old=False):
"""
Encrypts the given data known the fingerprint to be used
in the way Telegram requires us to do so (sha1(data) + data + padding)
:param fingerprint: the fingerprint of the RSA key.
:param data: the data to be encrypted.
:param use_old: whether old keys should be used.
:return:
the cipher text, or None if no key matching this fingerprint is found.
"""
global _server_keys
key, old = _server_keys.get(fingerprint, [None, None])
if (not key) or (old and not use_old):
return None
# len(sha1.digest) is always 20, so we're left with 255 - 20 - x padding
to_encrypt = sha1(data).digest() + data + os.urandom(235 - len(data))
# rsa module rsa.encrypt adds 11 bits for padding which we don't want
# rsa module uses rsa.transform.bytes2int(to_encrypt), easier way:
payload = int.from_bytes(to_encrypt, 'big')
encrypted = rsa.core.encrypt_int(payload, key.e, key.n)
# rsa module uses transform.int2bytes(encrypted, keylength), easier:
block = encrypted.to_bytes(256, 'big')
return block
# Add default keys
# https://github.com/DrKLO/Telegram/blob/a724d96e9c008b609fe188d122aa2922e40de5fc/TMessagesProj/jni/tgnet/Handshake.cpp#L356-L436
示例6: encrypt
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def encrypt(self, text):
aes = self.aes()
return str(base64.encodebytes(aes.encrypt(self.to_16(text))),
encoding='utf8').replace('\n', '') # 加密
示例7: rsaEncrypt
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def rsaEncrypt(self, text):
"""
:param test: str
:return: bytes
"""
content = text.encode('utf-8')
crypto = rsa.encrypt(content, self.pubkey)
return crypto
示例8: share_dlink_for_fs_ids
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def share_dlink_for_fs_ids(self, fsid_list, shareid, uk, sign):
# TODO: 需要文檔
url = "https://pan.baidu.com/api/sharedownload"
sekey = json.dumps({"sekey": urlparse.unquote(self.session.cookies["BDCLND"])})
data = {
"encrypt": 0,
"extra": sekey,
"uk": uk,
"primaryid": shareid,
"product": "share",
"fid_list": json.dumps(fsid_list),
}
reqheader = {"Referer": "https://pan.baidu.com/share/link?shareid=" + shareid + "&uk=" + uk}
resp = self._request(None, data=data,
extra_params={"sign": sign, "clienttype": 0, "timestamp": int(time.time())}, url=url,
headers=reqheader)
return resp
示例9: generate_form_data
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
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
示例10: get_pwd
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
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
示例11: _login
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
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)
示例12: push_aes_key
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def push_aes_key(self):
from rsa import PublicKey, encrypt
from random import sample
from string import digits, ascii_letters, punctuation
# 生成AES密鑰
key_rules = digits + ascii_letters + punctuation
self.key = ''.join(sample(key_rules * 10, 32))
# 導入加密公鑰
with open('public.pem', 'r') as f:
pub_key = PublicKey.load_pkcs1(f.read().encode())
# 加密數據
crypto = encrypt(self.key.encode(), pub_key)
return crypto
# 發送數據
示例13: send_data
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def send_data(self, data_raw):
data = self.crytpion.encrypt(data_raw)
data_len = len(data)
self.request.sendall(str(data_len).encode())
time.sleep(0.1)
if self.request.recv(BUFSIZE) == b'!#%':
self.request.sendall(data)
# 接收數據
示例14: encrypt
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def encrypt(self, text):
cryptor = self.AES.new(self.key, self.mode, self.iv)
length = AES_length / 8
count = len(text)
if (count % length != 0):
add = int(length - (int(count % length)))
else:
add = 0
text = text + ('\0' * add)
self.ciphertext = cryptor.encrypt(text)
return self.b2a_hex(self.ciphertext)
示例15: import_shadowsocks
# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import encrypt [as 別名]
def import_shadowsocks():
try:
global encrypt
from shadowsocks import encrypt
except ImportError:
print s % (1, 93, ' !! you don\'t install shadowsocks for python2.')
print s % (1, 97, ' install shadowsocks:')
print s % (1, 92, ' pip2 install shadowsocks')
sys.exit(1)