As developers, it is important to understand the different methods of data encryption and how they can be used to secure data. In this article, we will compare symmetrical encryption, asymmetrical encryption, and hashing and explore the differences between these methods.
Symmetrical Encryption
Symmetrical encryption, also known as shared secret encryption, is a method of encrypting data using a single secret key. This key is used to both encrypt and decrypt the data.
# Example of symmetrical encryption in Python
def symmetrical_encrypt(plaintext, key):
ciphertext = ""
for i in range(len(plaintext)):
ciphertext += chr(ord(plaintext[i]) ^ ord(key[i % len(key)]))
return ciphertext
def symmetrical_decrypt(ciphertext, key):
plaintext = ""
for i in range(len(ciphertext)):
plaintext += chr(ord(ciphertext[i]) ^ ord(key[i % len(key)]))
return plaintext
One major advantage of symmetrical encryption is its speed. Since the same key is used for both encryption and decryption, the process is relatively fast. However, the key must be shared between the sender and the receiver — this can be a security risk if the key is intercepted or not stored securely.
Asymmetrical Encryption
Asymmetrical encryption, also known as public key encryption, uses a pair of keys: a public key and a private key. The public key is used to encrypt the data, while the private key is used to decrypt it.
# Example of asymmetrical encryption in Python
from cryptography.hazmat.primitives.asymmetric import rsa, padding
def asymmetrical_encrypt(plaintext, public_key):
ciphertext = public_key.encrypt(
plaintext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return ciphertext
def asymmetrical_decrypt(ciphertext, private_key):
plaintext = private_key.decrypt(
ciphertext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return plaintext
One major advantage of asymmetrical encryption is that the private key does not need to be shared, making it a more secure option for sending sensitive information. However, it is slower than symmetrical encryption due to the additional computation required for the two different keys.
Hashing
Hashing generates a fixed-size output (a hash) from a variable-size input. It is commonly used for storing passwords, as it is one-way and cannot be reversed — it is not possible to obtain the original data from the hash. Instead, the original data is re-hashed and compared to verify authenticity.
# Example of hashing in Python
import hashlib
def hash_data(data):
return hashlib.sha256(data.encode()).hexdigest()
# Generate a hash for the password "password123"
password_hash = hash_data("password123")
print(password_hash) # Output: "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"
# Verify the password by re-hashing it and comparing to the stored hash
def verify_password(password, password_hash):
return hash_data(password) == password_hash
verify_password("password123", password_hash) # Output: True
verify_password("wrongpassword", password_hash) # Output: False
Hashing is a fast and secure method for storing data, but it is not suitable for sending sensitive information as it cannot be decrypted. It is also important to note that two different inputs can theoretically result in the same hash — known as a collision — so it is not a reliable method for verifying data integrity alone.
Conclusion
Symmetrical encryption is fast but requires the shared secret key to be kept secure. Asymmetrical encryption is more secure but slower due to the use of two different keys. Hashing is fast and secure for storing data, but not suitable for sending sensitive information and cannot be reversed. Understanding the strengths and limitations of each method is key to choosing the right one for the task at hand.