Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

5.8 KB
Raw Blame

Creating RSA keys involves generating two keys: a public key and a private key. The public key is used for encryption, while the private key is used for decryption. Below, I'll walk through the process of generating RSA keys using Python, including both the public and private keys.

We will use Python's Crypto library (from pycryptodome package), which simplifies cryptographic operations, including RSA key generation.

Steps for RSA Key Generation

  1. Generate two prime numbers $( p )$ and $( q )$.
  2. Compute the modulus $( n = p \times q )$.
  3. Calculate the totient $( \phi(n) = (p-1)(q-1) )$.
  4. Choose a public exponent $( e )$, usually a small prime like 65537.
  5. Calculate the private exponent $( d )$, which is the modular inverse of $( e ) mod ( \phi(n) )$.

Here’s the Python code to generate RSA keys:

RSA Key Generation Code in Python

from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import PKCS1_OAEP
import binascii

# Generate RSA Keys
def generate_rsa_keys(bits=2048):
    # Generate a new RSA key pair
    key = RSA.generate(bits)
    
    # Public key
    public_key = key.publickey().export_key()
    
    # Private key
    private_key = key.export_key()
    
    return public_key, private_key

# Save RSA keys to files
def save_keys_to_files(public_key, private_key):
    with open("rsa_public.pem", "wb") as pub_file:
        pub_file.write(public_key)
        
    with open("rsa_private.pem", "wb") as priv_file:
        priv_file.write(private_key)

# Display the keys
def display_keys(public_key, private_key):
    print("Public Key:")
    print(public_key.decode())
    print("\nPrivate Key:")
    print(private_key.decode())

# Generate RSA keys
public_key, private_key = generate_rsa_keys(bits=2048)

# Save keys to files
save_keys_to_files(public_key, private_key)

# Display the keys
display_keys(public_key, private_key)

Explanation of the Code

  1. Key Generation:

    • The RSA.generate(bits) method generates an RSA key pair of the specified size in bits (2048 bits is typical).
    • The public key is extracted from the generated RSA key object using the .publickey() method, and it is exported in PEM format using .export_key().
    • The private key is directly exported using .export_key().
  2. Saving the Keys:

    • The save_keys_to_files() function writes the public and private keys to files named rsa_public.pem and rsa_private.pem.
  3. Displaying the Keys:

    • The display_keys() function prints the public and private keys to the console in PEM format.

Example Output of RSA Keys

Public Key:
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7OoS+L4O56ipd6zjM2nV5
...
-----END PUBLIC KEY-----

Private Key:
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCATB+ggEBA7OoS+L4O56ipd6zjM2nV5M4
...
-----END PRIVATE KEY-----

Additional Notes

  • Key Length: The key length (2048 bits) is commonly used and provides a good balance of security and performance. However, longer key sizes (e.g., 4096 bits) can be used for higher security.

  • Public and Private Keys:

    • Public Key: This key can be freely shared with others. It is used for encryption.
    • Private Key: This key must be kept secret. It is used for decryption.
  • PEM Format: The keys are saved in PEM format (Base64 encoded with delimiters), which is commonly used in many cryptographic applications.

Using the Keys for Encryption and Decryption

Once the keys are generated, you can use them to encrypt and decrypt messages using the RSA algorithm. Here is an example of how to use the RSA keys for encryption and decryption:

RSA Encryption and Decryption with Python

# Encryption with Public Key
def rsa_encrypt(message, public_key):
    # Import the public key
    public_key_obj = RSA.import_key(public_key)
    # Create a cipher object using the public key
    cipher = PKCS1_OAEP.new(public_key_obj)
    # Encrypt the message
    encrypted_msg = cipher.encrypt(message.encode())
    return encrypted_msg

# Decryption with Private Key
def rsa_decrypt(encrypted_msg, private_key):
    # Import the private key
    private_key_obj = RSA.import_key(private_key)
    # Create a cipher object using the private key
    cipher = PKCS1_OAEP.new(private_key_obj)
    # Decrypt the message
    decrypted_msg = cipher.decrypt(encrypted_msg)
    return decrypted_msg.decode()

# Test Encryption and Decryption
message = "Hello, this is a secret message!"
print(f"Original Message: {message}")

# Encrypt the message using the public key
encrypted_message = rsa_encrypt(message, public_key)

# Decrypt the message using the private key
decrypted_message = rsa_decrypt(encrypted_message, private_key)

print(f"Decrypted Message: {decrypted_message}")

Explanation of Encryption/Decryption

  1. Encryption:

    • The rsa_encrypt() function uses the public key to encrypt a message. The message is first encoded as bytes, and then the PKCS1_OAEP cipher is used to encrypt the message.
  2. Decryption:

    • The rsa_decrypt() function uses the private key to decrypt the message. It first decodes the ciphertext and then decrypts it using the private key with the PKCS1_OAEP cipher.

Running the Example

If you run the above script, you’ll get output similar to the following:

Original Message: Hello, this is a secret message!
Decrypted Message: Hello, this is a secret message!

Conclusion

This Python example shows how to generate RSA keys, save them to files, and use them for encryption and decryption. The pycryptodome library simplifies RSA key generation and the encryption/decryption process. It’s a useful tool for secure communication and cryptographic applications.