def __init__(self, pubkey, e=65537): """ create a Rsa object :param pubkey: public key It can be an integer or a string. When it is a string, its format is either a hexadecimal string or a PEM format :param e: The general value is 65537. """ if isinstance(pubkey, int): self.key = RSA.RsaKey(n=pubkey, e=e) else: if not isinstance(pubkey, str): raise ValueError('pubkey must be str or int.') if '----' in pubkey: try: self.key = RSA.import_key(pubkey) except Exception as e: print(e) else: if pubkey == pubkey.lower(): pubkey = int(pubkey, 16) self.key = RSA.RsaKey(n=pubkey, e=e) else: pubkey = '-----BEGIN PUBLIC KEY-----\n' + pubkey + '\n-----END PUBLIC KEY-----' try: self.key = RSA.import_key(pubkey) except Exception as e: print(e)
def get_password(self, pubkey, password): # publickey = rsa.PublicKey(int(pubkey, 16), int('10001', 16)) # res = rsa.encrypt(password.encode(), publickey) # 生成公钥 recipient_key = RSA.RsaKey(n=int(pubkey, 16), e=65537) # 创建rsa对象 cipher_rsa = PKCS1_v1_5.new(recipient_key) # 加密数据+转换成16进制 msg = binascii.b2a_hex(cipher_rsa.encrypt(password.encode())).decode() # print(msg) return msg
def _generateRSAKey(bits, randfunc=None, e=65537): """Modified version of pycryptodome's Crypto.RSA.generate to allow keys of any size.""" if e % 2 == 0 or e < 3: raise ValueError( "RSA public exponent must be a positive, odd integer larger than 2." ) if randfunc is None: randfunc = Random.get_random_bytes d = n = Integer(1) e = Integer(e) while n.size_in_bits() != bits and d < (1 << (bits // 2)): # Generate the prime factors of n: p and q. # By construciton, their product is always # 2^{bits-1} < p*q < 2^bits. size_q = bits // 2 size_p = bits - size_q min_p = min_q = (Integer(1) << (2 * size_q - 1)).sqrt() if size_q != size_p: min_p = (Integer(1) << (2 * size_p - 1)).sqrt() def filter_p(candidate): return candidate > min_p and (candidate - 1).gcd(e) == 1 p = _generateProbablePrime(exact_bits=size_p, randfunc=randfunc, prime_filter=filter_p) min_distance = Integer(1) << max(0, bits // 2 - 100) def filter_q(candidate): return (candidate > min_q and (candidate - 1).gcd(e) == 1 and abs(candidate - p) > min_distance) q = _generateProbablePrime(exact_bits=size_q, randfunc=randfunc, prime_filter=filter_q) n = p * q lcm = (p - 1).lcm(q - 1) d = e.inverse(lcm) if p > q: p, q = q, p u = p.inverse(q) return RSA.RsaKey(n=n, e=e, d=d, p=p, q=q, u=u)
def __init__(self, pubkey, e=65537): # self.key = RSA.RsaKey(n=pubkey,e=e) if isinstance(pubkey, int): #判断是否为int self.key = RSA.RsaKey(n=pubkey, e=e) else: if not isinstance(pubkey, str): #判断是否为str raise ValueError('pubkey must be str or int.') if '----' in pubkey: try: self.key = RSA.import_key(pubkey) except Exception as e: print(e) else: if pubkey == pubkey.lower() or pubkey == pubkey.lower(): pubkey = int(pubkey, 16) self.key = RSA.RsaKey(n=pubkey, e=e) else: pubkey = '-----BEGIN PUBLIC KEY-----\n' + pubkey + '\n-----END PUBLIC KEY-----' #pingjie try: self.key = RSA.import_key(pubkey) except Exception as e: print(e)
def handshake(client_socket, addr): data = client_socket.recv(1024) connection_package = ChatMessageProtocol.from_bytes(data) username = connection_package.body.body data = client_socket.recv(1024) key_package = ChatMessageProtocol.from_bytes(data) n, e = key_package.body.body.split(";") client_pub_key = RSA.RsaKey(n=int(n), e=int(e)) client = ClientData(client_socket, username, addr, client_pub_key) connected_clients[client.socket] = client welcome_package = create_package(f"Welcome {username} to the server", True, MessageTypes.Message.value, client_pub_key) client_socket.send(bytes(welcome_package))
# /爬虫中的用法 有些网站会在请求过程中发送公钥,然后加密参数后传回后台来实现数据的加密。 import binascii from Cryptodome.PublicKey import RSA from Cryptodome.Cipher import PKCS1_OAEP data = '我是心蓝哈哈哈哈'.encode() # 网页上收到的n值 pubkey_n = '8d7e6949d411ce14d7d233d7160f5b2cc753930caba4d5ad24f923a505253b9c39b09a059732250e56c594d735077cfcb0c3508e9f544f101bdf7e97fe1b0d97f273468264b8b24caaa2a90cd9708a417c51cf8ba35444d37c514a0490441a773ccb121034f29748763c6c4f76eb0303559c57071fd89234d140c8bb965f9725' # e常常为65537 pbukey_e = 65537 # 生成公钥 recipient_key = RSA.RsaKey(n=int(pubkey_n, 16), e=65537) #16进制转为10进制 # 创建rsa对象 cipher_rsa = PKCS1_OAEP.new(recipient_key) # 加密数据 msg = cipher_rsa.encrypt(data) print(msg) # 输出16进制字符串 print(binascii.b2a_hex(msg))
def crypt_password(password, pubkey): public_key = RSA.RsaKey(n=int(pubkey, 16), e=65537) cipher = PKCS1_v1_5.new(public_key) res = cipher.encrypt(password.encode()) return binascii.b2a_hex(res).decode()
def create_pub_key(pub_key_dict): n = pub_key_dict['n'] e = pub_key_dict['e'] return RSA.RsaKey(n=n,e=e)
import binascii from Cryptodome.PublicKey import RSA from Cryptodome.Cipher import PKCS1_OAEP # 有些网站会在请求中发送公钥,然后加密参数后传给后台实现语句的加密 data = '今天星期五'.encode() # 网上收到的n值 pubkey_n = '8d7e6949d411ce14d7d233d7160f5b2cc753930caba4d5ad24f923a505253b9c39b09a059732250e56c594d735077cfcb0c3508e9f544f101bdf7e97fe1b0d97f273468264b8b24caaa2a90cd9708a417c51cf8ba35444d37c514a0490441a773ccb121034f29748763c6c4f76eb0303559c57071fd89234d140c8bb965f9725' # e常常为65537 pubkey_e = 65537 # 生成公钥 pubkey_key = RSA.RsaKey(n=(int(pubkey_n, 16)), e=pubkey_e) # print(type(pubkey_key)) # print(dir(pubkey_key)) print(pubkey_key.export_key()) # print(pubkey_key) cipher = PKCS1_OAEP.new(pubkey_key) msg = cipher.encrypt(data) # print(msg) # print(binascii.b2a_hex(msg))
def __init__(self, n, e): self._rsa = RSA.RsaKey(n=n, e=e)