Warning: This is a DEMONSTRATION for educational purposes only!
VIS-Cipher Advanced is not intended for production use and has not undergone rigorous security auditing by cryptography experts. Do NOT use this code to protect real-world sensitive data without thorough security review and adaptation by qualified professionals. For production systems, always rely on well-established, vetted, and standardized cryptographic libraries and protocols.
VIS-Cipher Advanced is a Python-based demonstration of a robust symmetric encryption system. It showcases modern cryptographic practices, aiming for a balance of security, efficiency, and clarity in implementation. This system is designed to be more than just a basic encryption example; it incorporates features crucial for real-world security, such as authenticated encryption, robust key derivation, and error handling.
Key Features:
- Symmetric-Key Encryption: Utilizes the Advanced Encryption Standard (AES) in Galois/Counter Mode (GCM), a widely respected and efficient symmetric cipher.
- AES-256: Employs AES with a 256-bit key, offering a high level of security against brute-force attacks.
- Authenticated Encryption (AES-GCM): Provides both confidentiality (keeping data secret) and integrity (ensuring data hasn't been tampered with). GCM mode includes a Message Authentication Code (MAC) to verify data integrity.
- Password-Based Key Derivation (PBKDF2HMAC-SHA256): Derives a strong encryption key from a user-provided password using PBKDF2HMAC-SHA256. PBKDF2 is designed to be computationally expensive, making brute-force password attacks significantly harder.
- Random Salt: Uses a randomly generated salt during key derivation. Salts prevent rainbow table attacks and enhance password security.
- Random Initialization Vector (IV) / Nonce: Generates a unique, cryptographically secure random IV for each encryption operation, essential for the security of GCM mode.
- PKCS7 Padding: Applies PKCS7 padding to plaintext before encryption to ensure compatibility with block cipher requirements.
- Error Handling: Implements comprehensive error handling for various stages of encryption and decryption, including key derivation failures, invalid data, authentication failures, and more.
- String and Bytes Plaintext Support: Can encrypt both string and byte-based plaintext data.
- Clear and Well-Commented Python Code: Designed for readability and educational purposes, with detailed comments explaining each step.
VIS-Cipher Advanced operates as a symmetric-key authenticated encryption system. This means the same secret key is used for both encryption and decryption, and the encryption process also provides assurance of data integrity and authenticity.
Here's a breakdown of the technical components and processes:
The security of password-based encryption heavily relies on deriving a strong, cryptographically secure key from a potentially weak user-provided password. VIS-Cipher Advanced uses PBKDF2HMAC-SHA256 for this purpose.
- PBKDF2 (Password-Based Key Derivation Function 2): PBKDF2 is a key derivation function that applies a pseudorandom function (in our case, HMAC-SHA256) repeatedly to the password along with a salt. The high number of iterations (default: 150,000) makes it computationally expensive to brute-force passwords, significantly increasing security against dictionary attacks and rainbow table attacks.
- HMAC-SHA256 (Hash-based Message Authentication Code with SHA-256): HMAC-SHA256 is used as the pseudorandom function within PBKDF2. SHA-256 is a strong cryptographic hash function, and HMAC ensures that the hashing process is keyed, further enhancing security.
- Salt: A randomly generated, unique salt is used for each encryption operation (and stored with the encrypted data for decryption). The salt is crucial because it prevents attackers from pre-calculating hashes for common passwords (rainbow tables). Even if two users use the same password, their salts will be different, resulting in different encryption keys and ciphertexts.
- Iterations: The number of iterations (default: 150,000) controls the computational cost of key derivation. Higher iteration counts increase security against brute-force attacks but also increase the time taken for encryption and decryption. A balance must be struck between security and performance.
- Key Length: The derived key is 256 bits (32 bytes) long, suitable for AES-256 encryption.
Process:
- A random salt (16 bytes) is generated if not provided (for encryption). For decryption, the stored salt is used.
- PBKDF2HMAC-SHA256 is invoked with the user's password, the salt, and the specified number of iterations.
- PBKDF2HMAC-SHA256 repeatedly hashes the password and salt using HMAC-SHA256 for the given number of iterations.
- The output of PBKDF2HMAC-SHA256 is a 256-bit (32-byte) encryption key.
VIS-Cipher Advanced uses AES-256 in Galois/Counter Mode (GCM) for encryption.
- AES (Advanced Encryption Standard): AES is a widely adopted and highly secure symmetric block cipher. It operates on blocks of data (128 bits in AES) and uses a secret key to transform plaintext into ciphertext.
- AES-256: VIS-Cipher Advanced uses AES with a 256-bit key. This key size is considered very strong and provides excellent security against brute-force attacks.
- GCM (Galois/Counter Mode): GCM is a modern and efficient authenticated encryption mode of operation for block ciphers like AES. It provides:
- Confidentiality: Encryption of the plaintext using AES in counter mode.
- Integrity and Authentication: Generation of a Message Authentication Code (MAC), also known as a GCM tag, which is appended to the ciphertext. This tag is used to verify the integrity and authenticity of the ciphertext during decryption. Any tampering with the ciphertext or the tag will be detected during decryption.
- Initialization Vector (IV) / Nonce: GCM requires a unique and unpredictable IV for each encryption operation. VIS-Cipher Advanced generates a cryptographically secure random 16-byte IV for every encryption. Using a random IV is critical for the security of GCM.
- PKCS7 Padding: Before encryption with AES (a block cipher), the plaintext is padded using PKCS7 padding to ensure that its length is a multiple of the AES block size (16 bytes). Padding is necessary for block ciphers to handle plaintext of arbitrary lengths.
Process:
- The
vis_generate_key_advancedfunction is called to derive the encryption key from the password. - A random 16-byte Initialization Vector (IV) is generated.
- PKCS7 padding is applied to the plaintext to make its length a multiple of 16 bytes.
- An AES cipher object is initialized in GCM mode using the derived key and the random IV.
- The padded plaintext is encrypted using AES-GCM.
- AES-GCM generates the ciphertext and an authentication tag (GCM tag).
- The salt, IV, ciphertext, and GCM tag are collected and encoded in Base64 for easier storage and transmission.
- The encrypted data (salt, IV, ciphertext, tag) is returned as a dictionary.
Decryption in VIS-Cipher Advanced reverses the encryption process using AES-256-GCM and the same derived key.
Process:
- The encrypted data (salt, IV, ciphertext, tag), stored as Base64 strings, is decoded back to bytes.
- The
vis_generate_key_advancedfunction is called again, this time providing the stored salt from the encrypted data. This ensures that the same encryption key is re-derived from the password. - An AES cipher object is initialized in GCM mode using the re-derived key, the stored IV, and crucially, the stored GCM tag. Providing the tag during decryption is essential for GCM to perform authentication.
- The ciphertext is decrypted using AES-GCM.
- GCM performs authentication tag verification. If the tag is valid (meaning the ciphertext and tag have not been tampered with and the correct key was used), decryption proceeds. If the tag is invalid,
cryptographylibrary raises anInvalidTagexception, indicating authentication failure and that decryption should not proceed as the data cannot be trusted. - If authentication is successful, the decrypted padded bytes are obtained.
- PKCS7 unpadding is applied to remove the padding added during encryption, recovering the original plaintext bytes.
- The decrypted plaintext bytes are returned.
While not a production-ready system, VIS-Cipher Advanced is a robust demonstration because it incorporates several key features and best practices that are essential for secure encryption systems:
- Uses Industry-Standard Algorithms: It relies on well-established and widely vetted cryptographic algorithms: AES-256-GCM and PBKDF2HMAC-SHA256. These are considered strong and secure when used correctly.
- Authenticated Encryption: The use of AES-GCM provides not only confidentiality but also data integrity and authenticity. This is crucial in real-world scenarios where data tampering is a significant threat. Simply encrypting data without authentication is often insufficient.
- Robust Key Derivation: PBKDF2HMAC-SHA256 with a high iteration count and salt makes password-based key derivation significantly more secure against brute-force attacks compared to simpler methods like directly hashing passwords.
- Random Salt and IV: The use of random salts and IVs is essential for cryptographic security. They prevent various attacks and ensure that encryption is probabilistic, meaning the same plaintext encrypted multiple times with the same key will produce different ciphertexts.
- Error Handling: The code includes error handling for common cryptographic exceptions, making it more robust and preventing unexpected crashes or insecure behavior in error scenarios.
- Handles String and Bytes: It demonstrates the capability to encrypt both text data (strings) and binary data (bytes), making it more versatile.
- AES-256: Choosing AES-256 provides a very high security margin. 256-bit keys are extremely resistant to brute-force attacks with current and foreseeable computing technology (excluding potential quantum computing advancements, which are addressed in security caveats).
- GCM Mode: GCM mode was selected for its efficiency and its combined authenticated encryption properties. It is often hardware-accelerated in modern processors, making it relatively fast.
- PBKDF2HMAC-SHA256 with 150,000 Iterations: The iteration count of 150,000 is a reasonable balance between security and performance. For extremely high-security applications, even higher iteration counts might be considered, but at the cost of increased processing time. SHA-256 is a robust hash algorithm for PBKDF2.
- Random Salt and IV (using
os.urandom):os.urandomis used to generate cryptographically secure random bytes for salts and IVs, which is crucial for security. - PKCS7 Padding: PKCS7 padding is a standard and widely used padding scheme for block ciphers, ensuring interoperability and correct decryption.
It is crucial to understand the limitations and security caveats of VIS-Cipher Advanced:
- Demonstration System - Not Production Ready: This code is for demonstration and educational purposes only. It has not been subjected to the rigorous security analysis required for production cryptographic systems. Do NOT use it to protect real-world sensitive data without expert cryptographic review.
- Password Security: The security of this system fundamentally relies on the strength of the password chosen by the user. Weak passwords will lead to weak encryption. Encourage users to use strong, unique, and randomly generated passwords. Consider integrating password strength meters and guidance in a real application. Password managers are highly recommended for users to manage strong passwords securely.
- Side-Channel Attacks: This demonstration code does not explicitly address side-channel attacks (e.g., timing attacks, power analysis attacks). In high-security scenarios, constant-time implementations and other side-channel countermeasures may be necessary.
- Post-Quantum Vulnerability: AES and SHA-256 (and thus AES-GCM and PBKDF2HMAC-SHA256) are potentially vulnerable to attacks from future quantum computers. For applications requiring long-term security, consider investigating and transitioning to post-quantum cryptography algorithms as they become standardized and mature.
- Key Management: Password-based key derivation is a simplified form of key management. Real-world secure systems often require more sophisticated key management practices, including secure key exchange, key storage, and key lifecycle management. For symmetric encryption, secure key exchange remains a significant challenge.
- No Formal Security Audit: VIS-Cipher Advanced has not been formally audited by independent cryptography experts. A professional security audit is essential before deploying any cryptographic system in a production environment.
- Rely on Vetted Libraries for Production: For production systems, always use well-established and thoroughly vetted cryptographic libraries (like the
cryptographylibrary in Python, which is used here) and follow security best practices recommended by cryptography experts. Avoid "rolling your own crypto" in production unless you have deep cryptographic expertise and have undergone rigorous security reviews.
- Prerequisites: Ensure you have Python 3.6+ installed.
- Install
cryptographylibrary:pip install cryptography
- Download or clone the
VIS-CYPHER.pyfile. - Run the Python script:
The script includes example usage in the
python VIS-CYPHER.py
if __name__ == "__main__":block that demonstrates encryption and decryption of both string and byte data, as well as demonstrations of authentication failure and incorrect password handling.
Example Usage (from the if __name__ == "__main__": block):
password = "mySuperSecretPassword123" # *** IMPORTANT: Use a strong password in real applications! ***
plaintext_string = "This is a secret message to be encrypted with VIS-Cipher Advanced. I am testing string plaintext."
plaintext_bytes = b"This is secret binary data to be encrypted with VIS-Cipher Advanced. I am testing byte plaintext."
# Encryption (String Plaintext)
encrypted_string_data = vis_encrypt_advanced(plaintext_string, password)
# Decryption (String Plaintext)
decrypted_string_plaintext_bytes = vis_decrypt_advanced(encrypted_string_data, password)
decrypted_string_plaintext = decrypted_string_plaintext_bytes.decode('utf-8')
# Encryption (Bytes Plaintext)
encrypted_bytes_data = vis_encrypt_advanced(plaintext_bytes, password)
# Decryption (Bytes Plaintext)
decrypted_bytes_plaintext = vis_decrypt_advanced(encrypted_bytes_data, password)This project is licensed under the MIT License - see the LICENSE file for details.
VIS-Cipher Advanced is provided "as is" without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. This is a demonstration system and is not intended for production use without expert security review.