Introduction to Java Encryption Algorithms

There are three types of Java encryption algorithms:

1. One-way encryption algorithm

2. Symmetric encryption algorithm

3. Asymmetric encryption algorithm

One-way Encryption Algorithm

One-way encryption is an irreversible encryption algorithm, and the encrypted ciphertext cannot be decrypted. Common algorithms include MD5, SHA, and HMAC.

MD5 (Message-Digest Algorithm)

No matter how long the data is, it is encoded into 128-bit data, and the same data always yields the same result.

Usage: Can be used for file verification, password encryption, and hashing data.

Code:

public static String getMD5Str(String s) {    try {        MessageDigest md = MessageDigest.getInstance("MD5");        byte[] bytes = md.digest(s.getBytes("utf-8"));        return toHex(bytes);    }    catch (Exception e) {        throw new RuntimeException(e);    }}public static String toHex(byte[] bytes) {    final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();    StringBuilder ret = new StringBuilder(bytes.length * 2);    for (int i=0; i<bytes.length; i++) {        ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);        ret.append(HEX_DIGITS[bytes[i] & 0x0f]);    }    return ret.toString();}public static void main(String[] args) {    System.out.println(getMD5Str("123456")); // Output E10ADC3949BA59ABBE56E057F20F883E}

Note:

1. The length of the resulting string is 32, and each character is a hexadecimal number. A hexadecimal number converted to binary is four bits, so a 32-length string corresponds to 32 * 4 = 128 bits.

2. The result will definitely be 32 characters, with 16 characters being a truncated part of the result.

3. MD5 is often used in conjunction with base64 encoding, but base64 encoding is not an encryption algorithm; it is just a data encoding method.

SHA (Secure Hash Algorithm)

SHA is more secure than MD5, and the encryption result is 160-bit data, with similar usage to MD5.

The SHA family consists of five algorithms: SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512, with the latter four sometimes referred to as SHA-2.

HMAC (Hash-based Message Authentication Code)

Usage: Commonly used for verifying API request parameters.

Symmetric Encryption Algorithm

The same key can be used for both encryption and decryption, with common algorithms being DES, AES, and PBE.

DES (Data Encryption Standard)

The DES algorithm has three input parameters: Key, Data, and Mode. The Key is 7 bytes, totaling 56 bits, which is the working key for the DES algorithm; Data is 8 bytes, totaling 64 bits, which is the data to be encrypted or decrypted; Mode refers to the operating mode of DES, which can be either encryption or decryption.

AES (Advanced Encryption Standard)

PBE (Password-Based Encryption)

Asymmetric Encryption Algorithm

Requires two keys. The public key encrypts data, while the private key decrypts it; the private key is used for signing, and the public key is used for signature verification; common algorithms include RSA and DH.

Leave a Comment