FCoDS - W7 - Public Key Cryptography
FCoDS - W7 - Public Key Cryptography
of Data Security
W7 - Public Key Cryptography
and
We will cover:
● Generating RSA keys
● Extracting the public key
● Encryption and Decryption
OpenSSL tools: Generating RSA keys
Example:
generate a
1024-bit
public/private
key pair
private.pem:
Base64 encoding
of DER
generated binary
output
Actual content of private.pem
Extracting the public key
Content of
public.pem
Encryption & decryption
Plaintext
Encryption
Decryption
Paddings for RSA
● Secret-key encryption uses encryption modes to encrypt
plaintext longer than block size.
● RSA used in hybrid approach (Content key length << RSA key
length)
● To encrypt:
○ short plaintext: treat it a number, raise it to the power of e (modulo n)
○ large plaintext: use hybrid approach (treat the content key as a number and
raise it to the power of e (modulo n)
pub = key.publickey()
pub_pem = pub.export_key(format='PEM')
with open('public.pem', 'wb') as f:
f.write(pub_pem)
f.close()
Public-Key Cryptography APIs:
Encryption
• To encrypt a message using public keys, we need to decide what
padding scheme
• For better security, it is recommended that OAEP is used
• Lines in code (example on next slide):
• Line (1): import the public key from the public-key file
• Line (2): create a cipher object using the public key
Public-Key Cryptography APIs:
Encryption (Contd.)
key = RSA.importKey(open('public.pem').read())
cipher = PKCS1_OAEP.new(key)
ciphertext = cipher.encrypt(message)
with open('ciphertext.bin', 'wb') as f:
f.write(ciphertext)
f.close()
Public-Key Cryptography APIs:
Decryption
Uses the private key and the decrypt() API
prikey_pem = open('private.pem').read()
prikey = RSA.importKey(prikey_pem, passphrase='dees')
cipher = PKCS1_OAEP.new(prikey)
message = cipher.decrypt(ciphertext)
print(message)
Public-Key Cryptography APIs:
Digital Signature
• In Python code, one can use PyCryptodome library’s
Crypto.Signature package
• Four supported digital signature algorithms:
• RSASSA-PKCS1-v1_5
• RSASSA-PSS
• DSA
• RSASSA-PSS
• Show example with RSASSA-PSS
Public-Key Cryptography APIs:
Digital Signature using PSS
• Probabilistic Signature Scheme (PSS) is a cryptographic signature
scheme designed by Mihir Bellare and Phillip Rogaway
• RSA-PSS is standardized as part of PKCS#1 v2.1
• Sign a message in combination with some random input.
• For same input:
• two signatures are different
• both can be used to verify
Public-Key Cryptography APIs:
Digital Signature using PSS
(Contd.)
• Lines in code example:
• line (1): create a signature object
• line (2): generate the signature for the hash of a message
• Authentication
• HTTPS and TLS/SSL
• Chip Technology Used in Credit Cards
Applications: Authentication
• Typical way to conduct authentication is to use passwords
• Disadvantage:
• A sends password to B: B can get hacked and A may use same password for
multiple accounts