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.

Create Encryption of files.md

Abdul Wahab Junaid113601bd450f
diff --git a/Cryptography/Cryptography Course/Course/Encryption of files.md b/Cryptography/Cryptography Course/Course/Encryption of files.md
new file mode 100644
index 0000000..bfc9eae
--- /dev/null
+++ b/Cryptography/Cryptography Course/Course/Encryption of files.md
@@ -0,0 +1,169 @@
+### **Cryptography - Encryption of Files**
+
+File encryption is a process used to protect the confidentiality of files by converting them into unreadable data, accessible only to those who possess the correct decryption key. This is a critical aspect of cybersecurity, as sensitive information stored on computers or transmitted over the internet must be kept private to avoid unauthorized access.
+
+The process of encrypting a file ensures that even if it is intercepted or accessed by unauthorized parties, the data remains secure and unreadable without the decryption key.
+
+### **How File Encryption Works**
+
+File encryption works by applying an algorithm that transforms readable data (plaintext) into an unreadable format (ciphertext). The encryption process uses a key, and only those who possess the corresponding decryption key can convert the ciphertext back into plaintext.
+
+1. **Input (Plaintext)**: The file (e.g., text document, image, audio, or any other digital file) that needs to be encrypted.
+2. **Encryption Algorithm**: A mathematical function that transforms the plaintext into ciphertext.
+3. **Encryption Key**: A secret value used by the encryption algorithm to perform the encryption. It can be the same for both encryption and decryption (symmetric encryption) or different (asymmetric encryption).
+4. **Output (Ciphertext)**: The encrypted file, which is unreadable without the proper decryption key.
+
+### **Types of File Encryption**
+
+There are two main types of file encryption techniques: **symmetric encryption** and **asymmetric encryption**.
+
+1. **Symmetric Encryption (Private Key Encryption)**:
+ - In symmetric encryption, the same key is used for both encryption and decryption.
+ - **Algorithms**: AES (Advanced Encryption Standard), DES (Data Encryption Standard), RC4, etc.
+ - **Advantages**:
+ - Fast encryption and decryption process.
+ - Suitable for encrypting large files.
+ - **Disadvantages**:
+ - Key distribution is a challenge since the same key must be shared securely between the sender and recipient.
+
+ **Example (AES Encryption for Files)**:
+ - **Step 1**: A secret key is generated.
+ - **Step 2**: The file is encrypted using the AES algorithm with the secret key.
+ - **Step 3**: The file is saved in an encrypted format (ciphertext).
+ - **Step 4**: To decrypt, the recipient must have the same secret key.
+
+2. **Asymmetric Encryption (Public Key Encryption)**:
+ - In asymmetric encryption, two different keys are used: a **public key** (for encryption) and a **private key** (for decryption).
+ - **Algorithms**: RSA, ElGamal, ECC (Elliptic Curve Cryptography).
+ - **Advantages**:
+ - Secure key distribution, as only the public key is needed to encrypt the file. The private key is kept secret.
+ - Suitable for applications like digital signatures and secure email.
+ - **Disadvantages**:
+ - Slower than symmetric encryption.
+ - Less efficient for large files due to computational overhead.
+
+ **Example (RSA for File Encryption)**:
+ - **Step 1**: The recipient shares their public key.
+ - **Step 2**: The sender encrypts the file using the recipient’s public key.
+ - **Step 3**: The file is transmitted securely.
+ - **Step 4**: The recipient decrypts the file with their private key.
+
+### **File Encryption Software and Tools**
+
+There are various software tools available to encrypt and decrypt files. Some tools use symmetric encryption, while others utilize asymmetric encryption. Below are some popular tools for file encryption:
+
+1. **VeraCrypt**:
+ - VeraCrypt is an open-source disk encryption software that allows users to create encrypted volumes for files or encrypt entire drives.
+ - It supports **AES**, **Serpent**, and **Twofish** encryption algorithms.
+
+2. **GPG (GNU Privacy Guard)**:
+ - GPG is an open-source implementation of the **OpenPGP** standard, used for encrypting and signing files, emails, and messages.
+ - It uses **asymmetric encryption** (RSA, ElGamal) to securely encrypt files.
+
+3. **7-Zip**:
+ - 7-Zip is a popular file archiver that also supports strong encryption (AES-256) for compressing and encrypting files.
+ - You can use a password to encrypt files when creating compressed archives.
+
+4. **BitLocker** (Windows) and **FileVault** (Mac):
+ - These are built-in full disk encryption systems provided by Microsoft (BitLocker) and Apple (FileVault).
+ - They encrypt entire disks and require a password or a recovery key to decrypt the contents.
+
+5. **OpenSSL**:
+ - OpenSSL is a widely-used toolkit that can be used to encrypt files using various algorithms, such as **AES**, **RSA**, and **DES**.
+ - It can be used both for symmetric and asymmetric encryption of files.
+
+### **Encrypting Files Using Python (Example with AES)**
+
+Here is a simple example of how to encrypt and decrypt files using the **AES (Advanced Encryption Standard)** algorithm with Python:
+
+```python
+from Crypto.Cipher import AES
+from Crypto.Random import get_random_bytes
+import os
+
+# Function to encrypt a file
+def encrypt_file(input_file, output_file, key):
+ cipher = AES.new(key, AES.MODE_GCM)
+
+ # Read the contents of the input file
+ with open(input_file, 'rb') as file:
+ plaintext = file.read()
+
+ # Encrypt the file contents
+ ciphertext, tag = cipher.encrypt_and_digest(plaintext)
+
+ # Write the encrypted file
+ with open(output_file, 'wb') as file:
+ [file.write(x) for x in (cipher.nonce, tag, ciphertext)]
+
+# Function to decrypt a file
+def decrypt_file(input_file, output_file, key):
+ with open(input_file, 'rb') as file:
+ nonce, tag, ciphertext = [file.read(x) for x in (16, 16, -1)]
+
+ cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
+
+ # Decrypt the file contents
+ plaintext = cipher.decrypt_and_verify(ciphertext, tag)
+
+ # Write the decrypted file
+ with open(output_file, 'wb') as file:
+ file.write(plaintext)
+
+# Example Usage
+key = get_random_bytes(16) # Generate a random 128-bit key
+input_file = 'sample.txt'
+encrypted_file = 'sample_encrypted.aes'
+decrypted_file = 'sample_decrypted.txt'
+
+# Encrypt the file
+encrypt_file(input_file, encrypted_file, key)
+print("File Encrypted")
+
+# Decrypt the file
+decrypt_file(encrypted_file, decrypted_file, key)
+print("File Decrypted")
+```
+
+### **Steps for File Encryption and Decryption in the Code**:
+
+1. **Encryption**:
+ - Generate a **random key** using `get_random_bytes()`.
+ - Use **AES GCM mode** for encryption (which also provides authentication).
+ - The file's plaintext content is read and encrypted using the AES cipher.
+ - The **nonce**, **authentication tag**, and **ciphertext** are written to the output file.
+
+2. **Decryption**:
+ - Read the **nonce**, **authentication tag**, and **ciphertext** from the encrypted file.
+ - Decrypt the ciphertext using the AES cipher and the same key.
+ - Verify the integrity of the decrypted data using the authentication tag.
+ - Write the decrypted plaintext to the output file.
+
+### **Benefits of File Encryption**
+
+1. **Confidentiality**:
+ - Encryption ensures that sensitive data stored in files is protected from unauthorized access.
+
+2. **Data Integrity**:
+ - Encryption ensures the data has not been altered during transmission or storage.
+
+3. **Security in Transit**:
+ - When sending files over the internet, encryption ensures that even if an attacker intercepts the files, they cannot read them.
+
+4. **Compliance**:
+ - Many industries have regulations (such as GDPR, HIPAA) that require encryption to protect sensitive data.
+
+### **Drawbacks of File Encryption**
+
+1. **Performance Overhead**:
+ - Encryption, especially for large files, can be computationally expensive and may slow down file processing and transfer speeds.
+
+2. **Key Management**:
+ - Managing encryption keys can be complex. If the key is lost or compromised, the encrypted data cannot be decrypted.
+
+3. **Compatibility**:
+ - Encrypted files may not be accessible without the proper software or encryption tool, which can cause issues in sharing encrypted files with others.
+
+### **Conclusion**
+
+File encryption is a vital aspect of modern cybersecurity, ensuring that sensitive files remain confidential and protected from unauthorized access. Using strong encryption algorithms, such as **AES** or **RSA**, along with good key management practices, can safeguard your files from potential threats. Whether encrypting individual files or entire drives, file encryption plays a crucial role in maintaining data security in various applications, including email encryption, cloud storage, and file sharing.