In our previous blog, we discussed how to encrypt and decrypt passwords in Java using the AES algorithm. In this follow-up post, we’ll delve deeper into various encryption algorithms, specifically AES, DES, and Blowfish. We’ll explain each algorithm in detail and provide code examples for their implementation.
Â
Types of Encryption Algorithms
AES (Advanced Encryption Standard)
AES is a symmetric encryption algorithm widely used across the globe. It was established by the U.S. National Institute of Standards and Technology (NIST) in 2001. AES encrypts data in blocks of 128 bits using keys of 128, 192, or 256 bits.
Key Features:
Symmetric Key Algorithm: The same key is used for both encryption and decryption.
Block Cipher: Encrypts data in fixed-size blocks (128 bits).
Highly Secure: Resistant to all known cryptographic attacks.
Â
AES Code Example:
public class AesEncryptionHelper {
private static final String ALGORITHM = “AES”;
// Generate a secret key
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(128);
return keyGen.generateKey();
}
// Encrypt the password
public static String encrypt(String password, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedPassword = cipher.doFinal(password.getBytes());
return Base64.getEncoder().encodeToString(encryptedPassword);
}
// Decrypt the password
public static String decrypt(String encryptedPassword, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedPassword = Base64.getDecoder().decode(encryptedPassword);
byte[] originalPassword = cipher.doFinal(decodedPassword);
return new String(originalPassword);
}
}
Â
Output
Â
DES (Data Encryption Standard)
DES is an older symmetric-key algorithm developed in the 1970s. It uses a 56-bit key to encrypt data in 64-bit blocks. Although DES was once a widely adopted standard, it is now considered insecure due to its small key size, which is vulnerable to brute-force attacks.
Key Features:
Symmetric Key Algorithm: The same key is used for both encryption and decryption.
Block Cipher: Encrypts data in fixed-size blocks (64 bits).
Outdated Security: Vulnerable to brute-force attacks due to the small key size.
Â
DES Code Example:
public class DesEncryptionHelper {
private static final String ALGORITHM = “DES”;
// Generate a secret key
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(56);
return keyGen.generateKey();
}
// Encrypt the password
public static String encrypt(String password, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedPassword = cipher.doFinal(password.getBytes());
return Base64.getEncoder().encodeToString(encryptedPassword);
}
// Decrypt the password
public static String decrypt(String encryptedPassword, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedPassword = Base64.getDecoder().decode(encryptedPassword);
byte[] originalPassword = cipher.doFinal(decodedPassword);
return new String(originalPassword);
}
}
Â
Output:
Blowfish
Blowfish is a symmetric-key block cipher designed by Bruce Schneier in 1993. It uses a variable-length key from 32 bits to 448 bits, making it flexible and secure. Blowfish is known for its speed and effectiveness and is used in various encryption products.
Key Features:
Symmetric Key Algorithm: The same key is used for both encryption and decryption.
Block Cipher: Encrypts data in fixed-size blocks (64 bits).
Flexible Key Length: Supports key lengths from 32 bits to 448 bits.
Â
Blowfish Code Example:
public class BlowfishEncryptionHelper {
private static final String ALGORITHM = “Blowfish”;
// Generate a secret key
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(128); // Key size can be from 32 to 448 bits
return keyGen.generateKey();
}
// Encrypt the password
public static String encrypt(String password, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedPassword = cipher.doFinal(password.getBytes());
return Base64.getEncoder().encodeToString(encryptedPassword);
}
// Decrypt the password
public static String decrypt(String encryptedPassword, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedPassword = Base64.getDecoder().decode(encryptedPassword);
byte[] originalPassword = cipher.doFinal(decodedPassword);
return new String(originalPassword);
}
}
Â
Output:
Comparing AES, DES, and Blowfish
Feature
AES
DES
Blowfish
Key Length
128, 192, or 256 bits
56 bits
32 to 448 bits
Block Size
128 bits
64 bits
64 bits
Security
Highly secure
Insecure (vulnerable)
Secure with flexible key
Performance
Fast and efficient
Slower due to smaller key
Fast and flexible
Adoption
Widely adopted
Largely obsolete
Used in various products
Â
Conclusion
Understanding different encryption algorithms is essential for choosing the right one for your application’s security needs. AES, DES, and Blowfish each have their strengths and weaknesses. AES is highly secure and widely adopted, DES is outdated and insecure, while Blowfish offers flexibility in key length and is known for its speed.
Source: Read MoreÂ