Master the Top 5 Encryption Algorithms: MD5, SHA-256, DES, AES, RSA Explained
This article introduces the five most common encryption algorithms—MD5, SHA‑256, DES, AES, and RSA—explaining their classifications, security properties, typical use cases, and providing complete Java implementations for each, while also showing how they underpin HTTPS security.
Introduction
In everyday development we often need to integrate third‑party APIs or expose our own, which inevitably involves signing, verification, encryption, and decryption.
Typical scenarios include storing user passwords securely, transmitting sensitive data such as bank cards or ID numbers, and signing payment requests.
Common Encryption Algorithms
Encryption algorithms can be divided into irreversible (hash) and reversible categories. Reversible encryption further splits into symmetric and asymmetric algorithms.
Irreversible (Hash) Algorithms
Hash algorithms produce a fixed‑length digest that cannot be reversed to the original data.
Common hash functions include MD5, SHA‑1, SHA‑224, SHA‑256, etc., and they are used for digital signatures, message authentication, and password storage. Some hash‑based schemes like HMAC also involve a secret key.
MD5
MD5 generates a 128‑bit digest, typically represented as a 32‑character hexadecimal string. Although fast, MD5 is considered insecure due to collision attacks.
<code>public class MD5 {
private static final String MD5_ALGORITHM = "MD5";
public static String encrypt(String data) throws Exception {
MessageDigest messageDigest = MessageDigest.getInstance(MD5_ALGORITHM);
byte[] digest = messageDigest.digest(data.getBytes());
Formatter formatter = new Formatter();
for (byte b : digest) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static void main(String[] args) throws Exception {
String data = "Hello World";
String encryptedData = encrypt(data);
System.out.println("Encrypted data: " + encryptedData);
}
}
</code>MD5 is fast but vulnerable to brute‑force and rainbow‑table attacks; using a salt can mitigate but stronger hashes are recommended.
SHA‑256
SHA‑256, part of the SHA‑2 family, produces a 256‑bit digest and offers better collision resistance than MD5.
<code>public class SHA256 {
private static final String SHA_256_ALGORITHM = "SHA-256";
public static String encrypt(String data) throws Exception {
MessageDigest messageDigest = MessageDigest.getInstance(SHA_256_ALGORITHM);
byte[] digest = messageDigest.digest(data.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(Integer.toHexString((b & 0xFF) | 0x100), 1, 3);
}
return sb.toString();
}
public static void main(String[] args) throws Exception {
String data = "Hello World";
String encryptedData = encrypt(data);
System.out.println("Encrypted data: " + encryptedData);
}
}
</code>SHA‑2 hashes are longer and provide stronger resistance to brute‑force attacks; adding a salt remains best practice.
Symmetric Encryption Algorithms
Symmetric algorithms use the same secret key for encryption and decryption, making key management critical.
DES
DES uses a 56‑bit key and is fast but insecure against modern attacks.
<code>public class DES {
private static final String DES_ALGORITHM = "DES";
public static String encrypt(String data, String key) throws Exception {
KeySpec keySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory factory = SecretKeyFactory.getInstance(DES_ALGORITHM);
SecretKey secretKey = factory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
public static String decrypt(String encryptedData, String key) throws Exception {
KeySpec keySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory factory = SecretKeyFactory.getInstance(DES_ALGORITHM);
SecretKey secretKey = factory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decoded = Base64.getDecoder().decode(encryptedData);
byte[] decrypted = cipher.doFinal(decoded);
return new String(decrypted);
}
public static void main(String[] args) throws Exception {
String data = "Hello World";
String key = "12345678";
String encrypted = encrypt(data, key);
System.out.println("Encrypted: " + encrypted);
String decrypted = decrypt(encrypted, key);
System.out.println("Decrypted: " + decrypted);
}
}
</code>Because of its short key, DES is generally replaced by 3DES or AES.
AES
AES supports 128/192/256‑bit keys and is the current standard for symmetric encryption.
<code>public class AES {
private static final String AES_ALGORITHM = "AES";
private static final String AES_TRANSFORMATION = "AES/CBC/PKCS5Padding";
private static final String AES_KEY = "1234567890123456";
private static final String AES_IV = "abcdefghijklmnop";
public static String encrypt(String data) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(AES_KEY.getBytes(), AES_ALGORITHM);
IvParameterSpec ivSpec = new IvParameterSpec(AES_IV.getBytes());
Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encrypted);
}
public static String decrypt(String encryptedData) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(AES_KEY.getBytes(), AES_ALGORITHM);
IvParameterSpec ivSpec = new IvParameterSpec(AES_IV.getBytes());
Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] decoded = Base64.getDecoder().decode(encryptedData);
byte[] decrypted = cipher.doFinal(decoded);
return new String(decrypted, StandardCharsets.UTF_8);
}
public static void main(String[] args) throws Exception {
String data = "Hello World";
String encrypted = encrypt(data);
System.out.println("Encrypted: " + encrypted);
String decrypted = decrypt(encrypted);
System.out.println("Decrypted: " + decrypted);
}
}
</code>AES’s longer keys provide a large key space, making brute‑force attacks impractical.
Asymmetric Encryption Algorithms
Asymmetric algorithms use a public key for encryption and a private key for decryption.
RSA
<code>public class RSA {
private static final String RSA_ALGORITHM = "RSA";
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
kpg.initialize(2048);
return kpg.generateKeyPair();
}
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encrypted);
}
public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
byte[] decoded = Base64.getDecoder().decode(encryptedData);
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decrypted = cipher.doFinal(decoded);
return new String(decrypted, StandardCharsets.UTF_8);
}
public static void main(String[] args) throws Exception {
KeyPair kp = generateKeyPair();
PublicKey pub = kp.getPublic();
PrivateKey pri = kp.getPrivate();
String data = "Hello World";
String encrypted = encrypt(data, pub);
System.out.println("Encrypted: " + encrypted);
String decrypted = decrypt(encrypted, pri);
System.out.println("Decrypted: " + decrypted);
}
}
</code>RSA offers high security and enables digital signatures and key exchange, but its operations are slower than symmetric algorithms.
Summary
The five algorithms covered—MD5, SHA‑256, DES, AES, and RSA—represent the most frequently used primitives for hashing, symmetric, and asymmetric encryption.
HTTPS, for example, combines these techniques: asymmetric RSA to exchange a symmetric key, symmetric AES (or similar) for bulk data encryption, and hash functions for integrity verification.
Understanding when and how to apply each algorithm helps build robust, secure applications.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.