Dr. Greg Bernstein
October 12th, 2021
Wikipedia: TLS, The Transport Layer Security (TLS) Protocol Version 1.3 – the security of the Web rests on public key cryptography.
Wikipedia: SSH, Comparing SSH Keys – A better way to connect to servers is via public key cryptography!
Wikipedia: S/MIME, Secure/Multipurpose Internet Mail Extensions (S/MIME) Version 4.0 Message Specification
How many symmetric keys are needed for N nodes to communicate?
\(N(N-1)/2\) keys
We’ve got a problem…
Need a secure channel to distribute the key
Need a key to create a secure channel
If a key is compromised there is no security
From Wikipedia: Public Key Cryptography classified history
1970: James H. Ellis, a British cryptographer at the UK Government Communications Headquarters (GCHQ), conceived of the possibility of “non-secret encryption”, (now called public key cryptography), but could see no way to implement it.
1973: Clifford Cocks (GCHQ) implemented what has become known as the RSA encryption algorithm, giving a practical method of “non-secret encryption”,
From Wikipedia: Public Key Cryptography classified history
These discoveries were not publicly acknowledged for 27 years, until the research was declassified by the British government in 1997
From Wikipedia: Public Key Cryptography Public Discovery
From Wikipedia: Public Key Cryptography Public Discovery
Only \(N\) Keys! No secure channel needed!
Some Imports
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
See Python Cryptography RSA for recommended parameters
alice_private_key_rsa = rsa.generate_private_key(public_exponent=65537, key_size=2048)
alice_public_key_rsa = alice_private_key_rsa.public_key()
See Python Cryptography RSA for recommended parameters
bob_private_key_rsa = rsa.generate_private_key(public_exponent=65537, key_size=2048)
bob_public_key_rsa = bob_private_key_rsa.public_key()
See Python Cryptography RSA for recommended approach
myPad = padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(), label=None)
Alice uses Bob’s public key to encrypt
message = b"Hi Bob, Alice here. I got your public key from trusted source. Just testing!"
bob_ciphertext = bob_public_key_rsa.encrypt(message, myPad)
def bytes2ascii(bs):
# Printable ASCII is 32-126
outstring = ""
for b in bs:
if b >= 32 and b <= 126:
outstring += chr(b)
else:
outstring += "X"
return outstring
print(bytes2ascii(bob_ciphertext))
# XX%XX$XXl?+^XRVk9`aX?tXXXwWXIXX-XXyXX*XXXXXXX#M?XXXXXXXXXXXxX'XX)XZ&XXX)XWX2X|XeeXXXrXXX"XXXXYXX}wXX>XJXX(<XXXXX$XXvXXX(XXXX,XXXXGXXt7&X$!XdXXX XXXX@XX.XX ZoXlX8X3XfXX:X<XTXXXXGXwXcXbXX?XQ\XWXXoXDXXXXgXXXDXXXYXXZXHXXXXX$X3XXXXXilXX0IX\XXXXgXxXXOXXXXXX%@amX
Bob uses his private key to decrypt
plaintext = bob_private_key_rsa.decrypt(bob_ciphertext, myPad)
print(plaintext)
Nothing is Perfect
Using public and private information securely come up with a common secret key for symmetric key cryptography algorithms such as AES. The algorithm takes advantage of the difficulty in computing discrete logarithms.
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
alice_private_key = X25519PrivateKey.generate()
alice_public_key = alice_private_key.public_key()
bob_private_key = X25519PrivateKey.generate()
bob_public_key = bob_private_key.public_key()
# Alice creates a symmetric key with her private key and Bob's public key
shared_key_alice = alice_private_key.exchange(bob_public_key)
print(shared_key_alice)
# Bob creates a symmetric key with her private key and Alice's public key
shared_key_bob = bob_private_key.exchange(alice_public_key)
print(shared_key_bob)
shared_key_alice == shared_key_bob
From NCyTE Applied Cryptography
From RFC8446
/* RSASSA-PKCS1-v1_5 algorithms */
rsa_pkcs1_sha256(0x0401),
rsa_pkcs1_sha384(0x0501),
rsa_pkcs1_sha512(0x0601),
/* ECDSA algorithms */
ecdsa_secp256r1_sha256(0x0403),
ecdsa_secp384r1_sha384(0x0503),
ecdsa_secp521r1_sha512(0x0603),
/* RSASSA-PSS algorithms with public key OID rsaEncryption */
rsa_pss_rsae_sha256(0x0804),
rsa_pss_rsae_sha384(0x0805),
rsa_pss_rsae_sha512(0x0806),
/* EdDSA algorithms */
ed25519(0x0807),
ed448(0x0808),
/* RSASSA-PSS algorithms with public key OID RSASSA-PSS */
rsa_pss_pss_sha256(0x0809),
rsa_pss_pss_sha384(0x080a),
rsa_pss_pss_sha512(0x080b),
ssh-keygen -f keyFile
ssh-keygen -t ed25519 -f keyFile2
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA5E8II0QJ/2Pd9Wjky8DMG+Xf9AQH61lITQkwLvBS1t greg@DESKTOP-71U86TK
From sshd the server can have an “authorized key file”
~/.ssh/authorized_keys
and ~/.ssh/authorized_keys2
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5X6gfWaQnc1PRKYC3t3U7dCEkN5wE+1HqnH6OwTtH0YMcvc3liILwp0qoao52NV1e3zRQiTeIWXa278UdSEiDmn9+VtFcZRNJYYlop5CuGOIvDt30vRrxeMNLI2Cg+zvcNOFaLxpnmJyajklScgnJwJj3qN6fCj6Wn6I+H8VYyBzRguaT3We0XSKZ5PzsW7tWRuFut/cBpMcbUWbgtBZvIGoU7LXnsNvDE+DLqAnEjeLJdBKRvjiLLHKcZVVS2j2Ua1U1mVFtNC0d2TO03LGqB4CNQhJ91y1n2TN8FS7764VbSjK1TFKNUJUN/1a+yU4BBNZWtHy8rxVw/Xx60VJJ gregb@DESKTOP-NOE6DKP
Manually add your public key that you generated on your machine to the authorized_keys
file on the server.
Use the ssh-copy-id command to help you do this without the manual file editing.
Example use: ssh-copy-id -i keyFile2 greg@192.168.1.220