Asymmetric Cryptography

The Asymmetric cryptography use two key, one to encrypt message and one to decrypt it. The Public Key is used to encrypt the message while the Private Key to decrypt the encrypted message. This two keys are generated using RSA cryptosystem, for example using python you can generate the keys in this way:

import rsa

public_key, private_key = rsa.newkeys(1024)  # 1024 bits

print(private_key.save_pkcs1("PEM"))
"""The print return something like:
b'-----BEGIN RSA PRIVATE KEY-----
MIICYAIBAAKBgQCMmErj1wy8cr0dPffEviqxcWbsGPp+hrLlIxc3P8bOj3q53fWX
3dm+sp6gOsJl9Ca+qd3uonuci0kvpqhRvAh1yFbFwYulqcNy1niI9v05PHfLOKam
vcwMViynk9G2xphKCdqA3sexyToDJrDbMBBgntjraxf1eumxweUf9wDHeQIDAQAB
AoGATMQ63/zj72GW8QiM6NgM56ZL1E4vODbEJ2jpnDkujEq7cBFJsApqgj11b3el
/ZU6MpD9pPcDF47V/za4CLmOAjbspVZOECciwmxJueNJnBkN8YtiOHhuH1gwLnSi
13NRGJ+k7tzaqXcfC4XCu5DOCnWupZkVTy6TH4BhgOdYEqECRQCh9nnsu7LizjeX
WVaAQvRvh6IrJ2D0J4abejsN4F+IXuDmtjvRr7ms0t8TFHpucmhyzuYHgNwhepKe
c2Bca0NULcCd3wI9AN45xgRffv9GuiIdAD336KKQMhA5E/6jNQu9a1tMGzCxPIuS
gLh3FKyrUOZj2aTp7y8IIrSOt6Iv/mmVpwJFAISiGMTCDUNpRuMdmRXePthaxXX7
gI1RFQ2b8mJxvSorMJge4ivccPxTj7TLT1vFOz3Rq1S3tQ2BmDJEH8RvXqq/8P4X
AjxpnjPK6EhHs6nLhNUUnrrmaHoi0735DECt770Siz1xCwunml2rTo1484NjvJk4
lCalx8wW3K3SW9UjAYkCRCyrHE6CPXUvNwEfr+XvcF4T9A/BuxUg8R9DSLy7TbQs
rvNEwm5defUV7kgHerQdpfezSJ++kIYXl0egFW31wcYYVbIl
-----END RSA PRIVATE KEY-----'
"""

print(public_key.save_pkcs1("PEM"))
"""
b'-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAIyYSuPXDLxyvR0998S+KrFxZuwY+n6GsuUjFzc/xs6Pernd9Zfd2b6y
nqA6wmX0Jr6p3e6ie5yLSS+mqFG8CHXIVsXBi6Wpw3LWeIj2/Tk8d8s4pqa9zAxW
LKeT0bbGmEoJ2oDex7HJOgMmsNswEGCe2OtrF/V66bHB5R/3AMd5AgMBAAE=
-----END RSA PUBLIC KEY-----'
"""

I love python! Anyway be aware of use rsa package, indeed in the help of the package is written:

“WARNING: this implementation does not use compression of the cleartext input to prevent repetitions, or other common security improvements. Use with care.”

Encrypt and Decrypt

Suppose to be a spy manager, you have many spies around the world. In order to receive reports from your spy you can share the public key public_key in a whatever way (like above).

So for example James Bond every day check a specific log site where you place the public key, he copy the key and he run these python code:

jb_report = """Hello, I'm James Bond, I've found a suspicious activity here, the destiny of the Humans is in my hands."""
jb_report_enc = rsa.encrypt(jb_report.encode(), public_key)
# jb_report_enc will be:
b'Lb^?\xb7\x1e\xa0\xdd\xf6B\x06U\x86?\xe6\x84\x9b\x1f\x14J\xbc\x94sf\xa1z\xc8"\x05X\x95c\xa7p\xce. \x7f\xc5\x90\x92\xfc\x9ei\xf6n%\xe4=S\x1e\xae\xb3Tu\xba$\x930\x92\xd8=\xeb\xf8\x12\xac\xb6\xe7\xc81\x81)\x8c\x02U[L<\xd1\x03\xe2\x90\x87\x10\x8f\xe7Kh\xcb?Mn(\xea\xadn\xa9#Q\xd7@\xa4\x95:\xd8##\x8eK\xdem=K\xc4\xb8u\xc3\x19\x96A\x91.r\xe9\xa9y\x08\x82'

These bytes are the message that James Bond will share in the log site.

The spy manager received that can decrypt using its own secret key in this way:

decoded = rsa.decrypt(jb_report_enc, private_key).decode()
# decoded data will be again:
"""Hello, I'm James Bond, I've found a suspicious activity here, the destiny of the Humans is in my hands."""

Now you can be the next spy!