Basic cryptography involves techniques to protect information by converting readable data into an unreadable format, which can only be reversed with a specific key. This is fundamental to guaranteeing the security of sensitive data, such as passwords and personal information. Below are some basic cryptographic concepts and techniques. 1. **Caesar's Cipher (Simple Substitution)** One of the oldest techniques, where each letter of the original text is replaced by another letter displaced by a fixed number in the alphabet. ```py def caesar_cipher(text, shift): result = "" for char in text: if char.isalpha(): base_shift = 65 if char.isupper() else 97 result += chr((ord(char) - base_shift + shift) % 26 + base_shift) else: result += char return result text = "Secret" print(caesar_cipher(text, 3)) # Output: Vhfuhw ``` 2. **Substitution Cipher** Each letter of the text is replaced by another letter based on a fixed correspondence table. 3. **Symmetric Encryption** Involves the use of a single key to encrypt and decrypt data. Examples include algorithms such as AES (Advanced Encryption Standard) and DES (Data Encryption Standard). Example of using symmetric encryption with AES in Python (using the `cryptography` library): ```py from cryptography.fernet import Fernet # Generate a key and encrypt a message key = Fernet.generate_key() cipher = Fernet(key) message = "Secret message" encrypted_message = cipher.encrypt(message.encode()) print(encrypted_message) # Decrypt the message decrypted_message = cipher.decrypt(encrypted_message).decode() print(decrypted_message) ``` 4. Asymmetric encryption Uses a pair of keys: a public key to encrypt the data and a private key to decrypt it. The RSA algorithm is a common example of asymmetric cryptography. Example of using RSA in Python (using the `cryptography` library): ```py from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization # Generate an RSA key pair private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048 ) # Extract the public key public_key = private_key.public_key() # Export the keys in PEM format private_key_pem = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption() ) public_key_pem = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) print(private_key_pem) print(public_key_pem) ``` 5. Hash functions A hash function is used to transform data into a fixed value, such as SHA-256. It is used to guarantee data integrity, as small changes to the data result in completely different hashes. Example of using SHA-256 in Python: ```py import hashlib mensagem = "Mensagem secreta" hash = hashlib.sha256(mensagem.encode()).hexdigest() print(hash) # Resultado: hash de 64 caracteres ``` These techniques are the basis for security in computer systems. You can use them in conjunction with other security measures to protect important information. If anyone here wants to share their knowledge of python, they can register here on the site and they will be free to publish their articles and chat with others in the chat rooms.

(0) Comments