strangerRidingCaml

4. Public-Key Cryptography 본문

Modern cryptography

4. Public-Key Cryptography

woddlwoddl 2024. 5. 6. 16:19
728x90
Public-Key Cryptography

Public-Key Cryptography

Introduction to Number Theory Concepts

  • Prime numbers: Numbers that are only divisible by 1 and themselves.
  • Modular arithmetic: Arithmetic operations performed on remainders when divided by a specified modulus.

RSA Encryption and Digital Signatures

  • RSA encryption: A public-key encryption algorithm based on the difficulty of factoring large composite numbers.
  • Digital signatures: RSA can also be used to generate digital signatures, providing authenticity and integrity for messages.

Diffie-Hellman Key Exchange

  • Key exchange protocol: Allows two parties to establish a shared secret over an insecure channel.
  • Computational Diffie-Hellman: The original method for key exchange using modular exponentiation.

Elliptic Curve Cryptography (ECC)

  • Elliptic curve: A curve defined by the equation y^2 = x^3 + ax + b, often used in cryptography due to its mathematical properties.
  • ECC encryption: A public-key encryption scheme based on the difficulty of the elliptic curve discrete logarithm problem.

Laboratory Activities

Lab 1: RSA Encryption and Decryption in Python


from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

def rsa_encrypt(plaintext, public_key):
    cipher = PKCS1_OAEP.new(public_key)
    ciphertext = cipher.encrypt(plaintext)
    return ciphertext

def rsa_decrypt(ciphertext, private_key):
    cipher = PKCS1_OAEP.new(private_key)
    plaintext = cipher.decrypt(ciphertext)
    return plaintext

key = RSA.generate(2048)
public_key = key.publickey()
private_key = key

plaintext = b"Hello World"
ciphertext = rsa_encrypt(plaintext, public_key)
decrypted_text = rsa_decrypt(ciphertext, private_key)

print("Plaintext:", plaintext)
print("Ciphertext:", ciphertext)
print("Decrypted text:", decrypted_text)

This lab demonstrates the encryption and decryption of text using the RSA algorithm in Python.

Lab 2: Diffie-Hellman Key Exchange in Python


def mod_exp(base, exp, modulus):
    result = 1
    base = base % modulus
    while exp > 0:
        if exp % 2 == 1:
            result = (result * base) % modulus
        exp = exp >> 1
        base = (base * base) % modulus
    return result

p = 23
g = 5

alice_private_key = 6
bob_private_key = 15

alice_public_key = mod_exp(g, alice_private_key, p)
bob_public_key = mod_exp(g, bob_private_key, p)

shared_secret_alice = mod_exp(bob_public_key, alice_private_key, p)
shared_secret_bob = mod_exp(alice_public_key, bob_private_key, p)

print("Shared secret for Alice:", shared_secret_alice)
print("Shared secret for Bob:", shared_secret_bob)

This lab demonstrates the Diffie-Hellman key exchange protocol in Python.

'Modern cryptography' 카테고리의 다른 글

6. Cryptographic Protocols  (0) 2024.05.06
5. Hash Functions and Digital Signatures  (0) 2024.05.06
3. Symmetric Cryptography  (0) 2024.05.06
2. Classical Cryptography  (0) 2024.05.06
1. Introduction to Cryptography  (0) 2024.05.06