# Real-World Use Cases

### How Cryptography Is Used in Modern Systems

Cryptography plays a vital role in securing various modern systems, ensuring privacy, integrity, and authenticity. Below are some key areas where cryptography is extensively used:

#### Blockchain

* **Description**: Blockchain technology uses cryptographic hashing to secure and verify transactions in a decentralized ledger. Each block contains a hash of the previous block, creating a chain of blocks that is resistant to tampering.
* **Cryptographic Techniques**: Hash functions (e.g., SHA-256), digital signatures, and consensus algorithms (e.g., Proof of Work) are fundamental to blockchain security.

#### Email Security

* **Description**: Cryptography is used to secure email communications through encryption and digital signatures. This ensures that only intended recipients can read the emails and verify their authenticity.
* **Cryptographic Techniques**: Public-key encryption (e.g., PGP), symmetric encryption, and digital signatures are commonly used to protect email content and verify sender identity.

#### File Encryption

* **Description**: File encryption protects sensitive data stored on disk by converting it into an unreadable format, which can only be decrypted by someone with the correct key. This is essential for data protection in various scenarios, from personal files to enterprise data.
* **Cryptographic Techniques**: Symmetric encryption algorithms (e.g., AES) are often used for encrypting files due to their efficiency and strong security properties.

***

### Interactive Examples: Encrypting Files and Securing Messages

#### Encrypting Files with AES

Below is an example of how to use the AES algorithm for encrypting and decrypting files in Python using the `pycryptodome` library.

**Encrypting a File**

```python
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import os

def encrypt_file(file_name, key):
    cipher = AES.new(key, AES.MODE_EAX)
    with open(file_name, 'rb') as file:
        plaintext = file.read()
    
    ciphertext, tag = cipher.encrypt_and_digest(plaintext)
    
    with open(file_name + '.enc', 'wb') as file:
        file.write(cipher.nonce)
        file.write(tag)
        file.write(ciphertext)

key = get_random_bytes(16)  # 16 bytes key for AES-128
encrypt_file('example.txt', key)
print("File encrypted successfully.")
```

### Decrypting a File

```python
from Crypto.Cipher import AES

def decrypt_file(encrypted_file_name, key):
    with open(encrypted_file_name, 'rb') as file:
        nonce = file.read(16)
        tag = file.read(16)
        ciphertext = file.read()
    
    cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
    plaintext = cipher.decrypt_and_verify(ciphertext, tag)
    
    with open(encrypted_file_name[:-4], 'wb') as file:
        file.write(plaintext)

decrypt_file('example.txt.enc', key)
print("File decrypted successfully.")
```

### Securing Messages with Public-Key Encryption

Here’s an example of how to secure and verify a message using RSA public-key encryption and digital signatures.

#### Encrypting a Message

```python
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

def encrypt_message(message, public_key):
    cipher = PKCS1_OAEP.new(public_key)
    ciphertext = cipher.encrypt(message)
    return ciphertext

key = RSA.generate(2048)
public_key = key.publickey()
message = b"Secure message"
ciphertext = encrypt_message(message, public_key)
print(f"Encrypted Message: {ciphertext.hex()}")
```

#### Decrypting a Message

```python
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

def decrypt_message(ciphertext, private_key):
    cipher = PKCS1_OAEP.new(private_key)
    plaintext = cipher.decrypt(ciphertext)
    return plaintext

private_key = key
decrypted_message = decrypt_message(ciphertext, private_key)
print(f"Decrypted Message: {decrypted_message.decode()}")
```

#### Explanation:

1\. **File Encryption/Decryption**: Demonstrates how to use AES to encrypt and decrypt files, ensuring data is securely protected.

2\. **Message Encryption/Decryption**: Shows how to use RSA for securing and decrypting messages, providing confidentiality and authentication.

Cryptography’s application in real-world systems like blockchain, email security, and file encryption highlights its critical role in protecting data and ensuring secure communications. By understanding and implementing these cryptographic techniques, you can effectively secure various types of digital information.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sc24.gitbook.io/sc24-crypto-python-workshop/tutorial/real-world-use-cases.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
