Beispiel #1
0
def send_keys_handshake(sock: socket.socket,
                        sending_key: rsa.PublicKey) -> None:
    """
    :param sock: socket to send to
    :param sending_key: this key will be send to client (server)
    :param ftp_session_key: encrypted key to encrypt files
    """
    key = sending_key.save_pkcs1()
    msg = dp.write_json({'id': 'handshake', 'key': key.hex()})
    send(sock, msg.encode())
Beispiel #2
0
def public_key_to_str(public_key):
    """ This function produces a string that represents the public key in PEM 
        format. This was it can be written to a database or transferred accross 
        an internet connection.

    Args:
        public_key (rsa.PublicKey): The key that is to be interpreted to a 
                                    PEM-format string.
    Returns:
        bytearray: A string of bytes representing the key in PEM format.
    """
    return PublicKey.save_pkcs1(public_key, format='PEM')
Beispiel #3
0
 def _gen_rsa_pubkey(self):
     # 将pub key string 转换为 pub rsa key
     # p.debug("Now turn key string to rsa key")
     try:
         rsa_pubkey = PublicKey(self._modulus, self._exponent)
         # 赋值到_pub_rsa_key
         self._pub_rsa_key = PublicKey.load_pkcs1(rsa_pubkey.save_pkcs1())
         # print("self._pub_rsa_key:{}".format(rsa_pubkey))
         # p.error("self._pub_rsa_key:{}".format(self._pub_rsa_key))
     except Exception as e:
         # p.error(e)
         # p.error("Invalid public_key")
         raise e
Beispiel #4
0
 def from_pub(pub: PublicKey) -> 'KeyId':
     hasher = sha256()
     hasher.update(pub.save_pkcs1(format="DER"))
     return KeyId(hexlify(hasher.digest()).decode("UTF-8"))
from rsa import PublicKey, PrivateKey, newkeys, encrypt, decrypt

(public, private) = newkeys(512)

with open("./public.pem", "wb") as f:
    pk = PublicKey.save_pkcs1(public)
    f.write(pk)

with open("./private.pem", "wb") as f:
    pk = PrivateKey.save_pkcs1(private)
    f.write(pk)

#loading the keys
'''
with open("./public.pem","rb") as f:
    data = f.read()
    public = PublicKey.load_pkcs1(data)
    
    
with open("./private.pem","rb") as f:
    data = f.read()
    private = PrivateKey.load_pkcs1(data)
    
'''

message = 'Bob says hi to Alice!'.encode('utf8')
crypto = encrypt(message, public)
print(crypto)
message = decrypt(crypto, private)
print(message.decode('utf8'))
Beispiel #6
0
from rsa import verify, sign, encrypt, decrypt, PublicKey, PrivateKey, newkeys, pkcs1

PUB_KEY_DST = '../../tests/testing_data/user2_test_pub.pem'
PRIV_KEY_DST = '../../tests/testing_data/user2_test.pem'

(public,private) = newkeys(4096, poolsize=4)

with open(PUB_KEY_DST, 'wb+') as f:
    pk = PublicKey.save_pkcs1(public, format='PEM')
    f.write(pk)

with open(PRIV_KEY_DST, 'wb+') as f:
    pk = PrivateKey.save_pkcs1(private, format='PEM')
    f.write(pk)
Beispiel #7
-1
def public_key_to_file(public_key, filepath):
    """ Writes a public key to a file in PEM format. This function will create a
        file if one does not exist and it will erase the contents of the file if
        it does exist.

    Args:
        public_key (rsa.PublicKey): The key that is to be written to the file.
        filepath (string): A path to the file where you wish to write the key.
    Returns:
        None
    """
    with open(filepath, 'wb+') as f:
        pk = PublicKey.save_pkcs1(public_key, format='PEM')
        f.write(pk)