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.
Implementation of One-Time Pad Cipher
The One-Time Pad (OTP) is an encryption method where the plaintext is combined with a random key (or "pad") that is the same length as the message. Each character of the message is XOR-ed with the corresponding character of the key. This encryption is theoretically unbreakable if the key is truly random, used only once, and kept secret.
Here’s how we can implement the One-Time Pad Cipher in Python.
Steps:
- Key Generation: A truly random key is generated that matches the length of the plaintext.
- Encryption: The plaintext is XOR-ed with the key to produce the ciphertext.
- Decryption: The ciphertext is XOR-ed with the same key to recover the plaintext.
Python Code for One-Time Pad Cipher
import os
# Function to generate a random key of the same length as the plaintext
def generate_key(length):
return os.urandom(length) # os.urandom() generates cryptographically secure random bytes
# Function to encrypt the plaintext using XOR operation with the key
def encrypt(plaintext, key):
# Convert the plaintext into bytes and XOR it with the key
ciphertext = bytes([p ^ k for p, k in zip(plaintext.encode(), key)])
return ciphertext
# Function to decrypt the ciphertext using XOR operation with the same key
def decrypt(ciphertext, key):
# XOR the ciphertext with the key to retrieve the plaintext
plaintext = bytes([c ^ k for c, k in zip(ciphertext, key)])
return plaintext.decode() # Decode the bytes back into a string
# Example usage
plaintext = "HELLO" # Example plaintext
key = generate_key(len(plaintext)) # Generate a random key of the same length as the plaintext
print(f"Plaintext: {plaintext}")
print(f"Generated Key: {key}")
# Encrypt the plaintext
ciphertext = encrypt(plaintext, key)
print(f"Ciphertext (in bytes): {ciphertext}")
# Decrypt the ciphertext
decrypted_text = decrypt(ciphertext, key)
print(f"Decrypted Text: {decrypted_text}")
Explanation:
Key Generation: The
generate_keyfunction usesos.urandom(length)to generate a random key that is the same length as the message.os.urandom()provides cryptographically secure random bytes.Encryption: The
encryptfunction XORs each byte of the plaintext with the corresponding byte of the key. Thezip()function pairs each byte of the plaintext and the key together, and the XOR operation is performed. The result is the ciphertext in the form of a byte sequence.Decryption: The
decryptfunction performs the reverse of the encryption process. Since XOR-ing the ciphertext with the same key recovers the original plaintext, we apply the XOR operation again to retrieve the message.
Example Output:
Plaintext: HELLO
Generated Key: b'\x8d\x0f\x11\xff\xdb'
Ciphertext (in bytes): b'\x0e\x1f\x1d\xfd\xf0'
Decrypted Text: HELLO
- Generated Key: The key is a random series of bytes, generated each time the script is run.
- Ciphertext: The ciphertext is the result of XOR-ing each character of the plaintext with the corresponding key character. It appears as unreadable binary data.
- Decrypted Text: The decryption process correctly recovers the original plaintext "HELLO" from the ciphertext, as long as the key is kept secure and used only once.
Security Considerations:
Key Security: The security of the One-Time Pad relies on the key being kept secret and being used only once. If the key is reused or if an attacker gains access to the key, the ciphertext can be easily decrypted.
Key Length: The key must be exactly as long as the plaintext. This ensures that each bit of the message is combined with a truly random key bit, making it impossible for an attacker to deduce any patterns.
Key Distribution: One of the main practical challenges of the One-Time Pad is the secure distribution of the key. The key must be transmitted securely to the recipient without interception.
Conclusion:
The One-Time Pad provides perfect secrecy if the key is randomly generated, used only once, and securely exchanged. However, the practical challenges of key management and distribution make it difficult to use in real-world applications. Despite this, it remains a key concept in cryptographic theory and demonstrates the importance of key randomness and secrecy in secure communication.