AES, DES, RC4, RSA, Diffie-Hellman Examples
AES, DES, RC4, RSA, Diffie-Hellman Examples
RSA encryption leverages the mathematical principles of modular arithmetic and the difficulty of factoring large integers to secure communication. It begins by generating two large prime numbers, p and q, and computes their product, the modulus n = p*q . The public key is calculated by choosing a value e that is relatively prime to (p-1)*(q-1), usually a small prime like 65537, and the private key is the multiplicative inverse of e mod (p-1)(q-1). Encryption involves raising the message to the power of e modulo n to produce the ciphertext, while decryption involves raising the ciphertext to the power of the private key d modulo n to retrieve the original message . RSA's security derives from the difficulty of factoring the large modulus n into its constituent primes, a problem for which there is no efficient solution for sufficiently large numbers, making it impractical for an adversary to compute the private key from the public key .
The initialization vector (IV) plays a critical role in AES encryption by ensuring distinct encryption results even when the same plaintext is encrypted multiple times with the same key. In AES's CBC mode, the IV is used as a random input that initializes the encryption process by being XORed with the first plaintext block before encryption . Consequently, even if the same message is encrypted under the same key, differing IVs result in differing ciphertexts. This prevents attackers from exploiting patterns in the ciphertext and enhances the overall security by avoiding predictable ciphertexts . The randomness and uniqueness of the IV mitigate risks associated with identical plaintext blocks producing identical ciphertext blocks, a vulnerability present in modes without an IV .
Using short key lengths in symmetric encryption algorithms like DES has significant security implications as it makes the encryption susceptible to brute force attacks. DES utilizes a 56-bit key, which was once considered secure, but due to advances in computational power, it can now be cracked in a matter of hours or less through systematic key testing of all possible combinations . This vulnerability makes DES inadequate for modern applications where data security is paramount, as attackers can easily decrypt intercepted communications. The advent of faster computers and distributed processing renders short key lengths impractically insecure for protecting sensitive information. As a result, stronger algorithms like AES, which supports key lengths of 128, 192, and 256 bits, have replaced DES in most applications to ensure data security against both current and future computational threats .
The RSA algorithm utilizes asymmetric keys — a pair consisting of a public key and a private key — for encryption and decryption. This key pair is generated from two large prime numbers, creating a modulus for encryption and decryption operations . When a sender encrypts a message, they use the recipient's public key so that the ciphertext can only be decrypted by the recipient's private key, ensuring confidentiality . For decryption, the recipient uses their private key to convert the ciphertext back into the plaintext. The use of asymmetric keys allows for secure communication as only the intended recipient has access to the private key needed to decrypt the message, and the public key alone cannot retrieve the private key due to the mathematical difficulty of factorizing the large modulus . This ensures both sender authenticity and message confidentiality over unsecured channels, making RSA a cornerstone for secure digital communications .
AES (Advanced Encryption Standard) and DES (Data Encryption Standard) differ mainly in block size, key length, and modes of operation. AES, in the provided implementation, uses a block size of 128 bits in CBC (Cipher Block Chaining) mode with PKCS5Padding . It typically supports key sizes of 128, 192, or 256 bits, which provides significant security improvements over DES. On the other hand, DES uses a block size of 64 bits and is implemented in ECB (Electronic Codebook) mode with the same padding . DES typically supports a 56-bit key length, making it less secure as compared to AES. Moreover, AES's utilization of CBC mode provides better security by incorporating an initialization vector (IV), avoiding identical blocks of plaintext producing identical blocks of ciphertext as in ECB mode, which DES uses .
ElGamal encryption ensures data security by using a public and private key pair in conjunction with secure random numbers. The public parameters include a large prime p, a generator b, and the public key c = b^secretKey mod p . For encryption, a random number r is selected and used to compute brmodp = b^r mod p and the ciphertext EC = X * c^r mod p . For decryption, Alice uses her secret key to compute crmodp = brmodp^secretKey mod p and its multiplicative inverse d = crmodp^(-1) mod p to retrieve the plaintext X from EC = X * c^r mod p by multiplying EC by d and reducing modulo p . The security of ElGamal is based on the difficulty of computing discrete logarithms, as knowing the generator and its large exponent modulo a prime does not feasibly disclose the exponent without extensive resources .
AES encryption ensures message confidentiality through symmetric key cryptographic techniques, where the same secret key is used for both encryption and decryption. In the provided program, AES uses a randomly generated secret key to encrypt the message 'Hello, AES!' . To enhance security, AES is initialized with Cipher Block Chaining (CBC) mode and PKCS5Padding to ensure that the plaintext is properly padded and divided into blocks securely. A 16-byte initialization vector (IV) is also randomly generated to ensure that even if the same plaintext is encrypted multiple times, it will produce different ciphertexts . These precautions prevent unauthorized access and ensure confidentiality by making it challenging to derive the original message without both the correct key and the IV.
The Diffie-Hellman key exchange method uses the concept of generating a shared secret between two parties without actually transmitting the secret itself. In the implementation, Alice and Bob each generate their own private keys (a and b) and calculate their respective public values (A = g^a mod p and B = g^b mod p) using the same base g and a large prime number p . These public values are then shared with each other. When Alice receives Bob's public value B, she raises it to her private key to compute the shared secret, secretA = B^a mod p. Similarly, Bob uses Alice's public value A and raises it to his private key to compute the shared secret, secretB = A^b mod p . This results in the same shared secret because (g^b)^a mod p = (g^a)^b mod p. The security of this exchange relies on the difficulty of computing discrete logarithms, which makes it computationally infeasible for an eavesdropper to derive the shared secret from the public values alone .
Digital signatures confirm the authenticity and integrity of messages by allowing a sender to sign a message with their private key, creating a signature unique to the message and key. Verification involves several steps: first, the sender uses a signature algorithm to hash the message and encrypt the hash with their private key, creating a digital signature . Upon receiving the message, the recipient uses the sender's public key to decrypt the signature and retrieve the hash. The recipient then hashes the original message independently and compares it to the decrypted hash . If they match, it confirms the message's authenticity (proving the sender's identity) and integrity (showing that the message has not been altered). This ensures trust in communication across unsecured networks . The use of asymmetric cryptography in this process ensures that only the sender with the corresponding private key could have created a valid signature .
The RC4 stream cipher operates by initializing a permutation array, S, of size 256 with a key scheduling algorithm based on the provided key, which can be between 1 and 256 bytes . The S array is generated by swapping bytes according to pseudo-random numbers derived from the key. In operation, RC4 produces a pseudo-random key stream, which is XORed with the plaintext to produce the ciphertext. Unlike block ciphers like AES and DES that process fixed-size blocks of data, RC4 processes the data one byte at a time which makes it a stream cipher . This allows RC4 to be more efficient and faster for certain applications, particularly in scenarios where the data arrives in stream form. However, RC4 has known vulnerabilities and is less secure than modern block ciphers like AES .