本文整理汇总了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)