5 Most Common Encryption Algorithms Every Developer Should Know

This article reviews five widely used encryption algorithms—MD5, SHA‑256, DES, AES, and RSA—explaining their classifications, Java implementations, strengths, weaknesses, and how they combine in HTTPS to secure data transmission.

LouZai
LouZai
LouZai
5 Most Common Encryption Algorithms Every Developer Should Know

Introduction

Payment systems frequently involve signing, verification, encryption, and decryption. Typical scenarios include storing hashed passwords, encrypting sensitive data such as bank cards or ID numbers, and generating signatures for API requests.

Algorithm Classification

Encryption algorithms are divided into irreversible (hash) algorithms and reversible algorithms . Reversible algorithms further split into symmetric and asymmetric categories.

Irreversible (Hash) Algorithms

MD5

MD5 (Message‑Digest Algorithm 5) produces a 128‑bit hash represented by 32 hexadecimal characters. Example Java implementation:

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);
    }
}

MD5 is fast but considered insecure because collisions can be generated and brute‑force attacks are feasible. Adding a salt mitigates but does not fully resolve the issue; SHA‑256 is recommended instead.

SHA‑256

SHA‑256, part of the SHA‑2 family, outputs a 256‑bit hash. It offers longer hash values and stronger collision resistance. Example Java implementation:

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);
    }
}

SHA‑256’s longer output makes brute‑force and rainbow‑table attacks significantly harder. Salting remains essential for protecting stored hashes.

Symmetric Encryption Algorithms

DES

DES (Data Encryption Standard) uses a 56‑bit key and operates with permutations, substitutions, and XOR operations. Example Java implementation:

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 secretKeyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
        SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
        Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }
    public static String decrypt(String encryptedData, String key) throws Exception {
        KeySpec keySpec = new DESKeySpec(key.getBytes());
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
        SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
        byte[] decodedData = Base64.getDecoder().decode(encryptedData);
        Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedData = cipher.doFinal(decodedData);
        return new String(decryptedData);
    }
    public static void main(String[] args) throws Exception {
        String data = "Hello World";
        String key = "12345678";
        String encryptedData = encrypt(data, key);
        System.out.println("Encrypted data: " + encryptedData);
        String decryptedData = decrypt(encryptedData, key);
        System.out.println("Decrypted data: " + decryptedData);
    }
}

DES is fast but its short key makes it vulnerable to brute‑force and differential attacks; modern applications prefer 3DES or AES.

AES

AES (Advanced Encryption Standard) supports 128‑, 192‑, or 256‑bit keys and is widely adopted. Example Java implementation using CBC mode and PKCS5 padding:

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"; // 16‑byte key
    private static final String AES_IV = "abcdefghijklmnop"; // 16‑byte IV
    public static String encrypt(String data) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(AES_KEY.getBytes(), AES_ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(AES_IV.getBytes());
        Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedData);
    }
    public static String decrypt(String encryptedData) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(AES_KEY.getBytes(), AES_ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(AES_IV.getBytes());
        Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] decodedData = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedData = cipher.doFinal(decodedData);
        return new String(decryptedData, StandardCharsets.UTF_8);
    }
    public static void main(String[] args) throws Exception {
        String data = "Hello World";
        String encryptedData = encrypt(data);
        System.out.println("Encrypted data: " + encryptedData);
        String decryptedData = decrypt(encryptedData);
        System.out.println("Decrypted data: " + decryptedData);
    }
}

AES’s longer keys provide a larger key space, making brute‑force attacks impractical, though they increase storage requirements. Key management remains the main challenge for symmetric encryption.

Asymmetric Encryption Algorithm

RSA

RSA, invented in 1978 by Rivest, Shamir, and Adleman, is the most common public‑key algorithm. Example Java implementation:

public class RSA {
    private static final String RSA_ALGORITHM = "RSA";
    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM);
        keyPairGenerator.initialize(2048);
        return keyPairGenerator.generateKeyPair();
    }
    public static String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedData);
    }
    public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
        byte[] decodedData = Base64.getDecoder().decode(encryptedData);
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedData = cipher.doFinal(decodedData);
        return new String(decryptedData, StandardCharsets.UTF_8);
    }
    public static void main(String[] args) throws Exception {
        KeyPair keyPair = generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        String data = "Hello World";
        String encryptedData = encrypt(data, publicKey);
        System.out.println("Encrypted data: " + encryptedData);
        String decryptedData = decrypt(encryptedData, privateKey);
        System.out.println("Decrypted data: " + decryptedData);
    }
}

RSA offers high security and enables digital signatures and key exchange, but encryption/decryption is slower than symmetric algorithms, and larger key sizes increase computational overhead.

HTTPS Workflow Overview

Client initiates an HTTPS request.

Server returns its public‑key certificate.

Client validates the certificate using the CA’s public key and verifies the server’s signature (asymmetric encryption + hash).

Client generates a random symmetric key.

Client encrypts the symmetric key with the server’s public key and sends it.

Server decrypts the symmetric key with its private key.

Both parties use the symmetric key for bulk data encryption, while hash algorithms ensure message integrity.

Thus HTTPS combines asymmetric encryption for key exchange, symmetric encryption for data transfer, and hash algorithms for integrity checks.

Conclusion

The article presented five common encryption algorithms—MD5, SHA‑256, DES, AES, and RSA—showing their Java implementations, typical use cases, advantages, and limitations, and illustrated how they work together in HTTPS to protect data in transit.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaencryptionHTTPSasymmetric encryptionsymmetric encryptionhash algorithm
LouZai
Written by

LouZai

10 years of front‑line experience at leading firms (Xiaomi, Baidu, Meituan) in development, architecture, and management; discusses technology and life.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.