فهم مبادئ التشفير وتطبيقاته لضمان سرية وسلامة البيانات في برمجياتك.
تركز هذه الوحدة على فهم مبادئ التشفير (Cryptography) وكيفية تطبيقه داخل البرمجيات لضمان سرية البيانات وسلامتها ومنع التلاعب أو الوصول غير المصرح به.
سيتعلم المتدرب الفرق بين أنواع التشفير، ومتى نستخدم كل نوع، بالإضافة إلى الممارسات الصحيحة لإدارة المفاتيح، وتجنب الأخطاء الشائعة التي قد تؤدي إلى كسر الحماية.
بنهاية هذه الوحدة سيكون المتدرب قادرًا على:
التعريف: التشفير هو عملية تحويل البيانات من صيغة مفهومة (Plaintext) إلى صيغة غير مفهومة (Ciphertext) باستخدام خوارزمية ومفتاح.
الأهداف الأساسية للتشفير:
الوصف: نفس المفتاح يُستخدم للتشفير وفك التشفير.
أمثلة على الخوارزميات:
مميزات:
عيوب:
مثال برمجي بلغة Python باستخدام AES:
from cryptography.fernet import Fernet
# Function to generate a key
def generate_key():
return Fernet.generate_key()
# Function to encrypt a message
def encrypt_message(message, key):
f = Fernet(key)
encrypted_message = f.encrypt(message.encode())
return encrypted_message
# Function to decrypt a message
def decrypt_message(encrypted_message, key):
f = Fernet(key)
decrypted_message = f.decrypt(encrypted_message).decode()
return decrypted_message
# Example Usage
key = generate_key()
print(f"Generated Key: {key.decode()}")
original_message = "This is a secret message."
encrypted = encrypt_message(original_message, key)
print(f"Encrypted Message: {encrypted.decode()}")
decrypted = decrypt_message(encrypted, key)
print(f"Decrypted Message: {decrypted}")
# Note: In a real application, the key should be securely stored and managed,
# and not hardcoded or transmitted insecurely.
الوصف: يستخدم زوجًا من المفاتيح: مفتاح عام (Public Key) للتشفير، ومفتاح خاص (Private Key) لفك التشفير.
أمثلة على الخوارزميات:
مميزات:
عيوب:
مثال برمجي بلغة Python باستخدام RSA:
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
# Generate RSA key pair
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
public_key = private_key.public_key()
# Convert keys to PEM format for storage/sharing (optional, but good practice)
pem_private = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption()
)
pem_public = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
print(f"Private Key (PEM):\n{pem_private.decode()}")
print(f"Public Key (PEM):\n{pem_public.decode()}")
# Message to encrypt
message = b"This is a secret message for RSA." # Must be bytes for encryption
# Encrypt the message using the public key
encrypted_message = public_key.encrypt(
message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print(f"Encrypted Message: {encrypted_message.hex()}")
# Decrypt the message using the private key
decrypted_message = private_key.decrypt(
encrypted_message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print(f"Decrypted Message: {decrypted_message.decode()}")
الوصف: تحول أي بيانات إلى "بصمة" ذات طول ثابت (Hash Value). لا يمكن عكسها لاستعادة البيانات الأصلية.
أمثلة على الخوارزميات:
مميزات:
عيوب:
مثال برمجي بلغة Python باستخدام SHA-256:
import hashlib
def hash_data(data):
# Ensure data is bytes
data_bytes = data.encode('utf-8')
# Create a SHA-256 hash object
sha256_hash = hashlib.sha256()
# Update the hash object with the data
sha256_hash.update(data_bytes)
# Get the hexadecimal representation of the hash
return sha256_hash.hexdigest()
# Example Usage for data integrity
file_content = "This is the content of my important file."
file_hash = hash_data(file_content)
print(f"File Content Hash: {file_hash}")
# Simulate tampering
tampered_content = "This is the content of my important file. (tampered)"
tampered_hash = hash_data(tampered_content)
print(f"Tampered Content Hash: {tampered_hash}")
if file_hash != tampered_hash:
print("Integrity check failed: Data has been tampered with!")
else:
print("Integrity check passed: Data is intact.")
# Example Usage for password storage with salt (simplified)
def hash_password(password, salt):
salted_password = password + salt
return hashlib.sha256(salted_password.encode('utf-8')).hexdigest()
user_password = "MySuperSecurePassword123"
# Salt should be unique and randomly generated for each user and stored with the hash
password_salt = "randomsalt123"
hashed_pw = hash_password(user_password, password_salt)
print(f"\nHashed Password: {hashed_pw}")
إدارة المفاتيح هي عملية حيوية لضمان أمان التشفير. تتضمن:
ملاحظة: أمان نظام التشفير يعتمد بشكل كبير على أمان المفاتيح. مفتاح ضعيف أو مُدار بشكل سيء يجعل أقوى الخوارزميات عديمة الفائدة.
تجنب هذه الأخطاء لضمان فعالية التشفير في تطبيقاتك: