1. AES symmetric encryption and decryption
package ; import ; import ; import ; import ; import ; import ; import ; import .Base64; /** * AES symmetric encryption * * @author wangbo * @date 2024/11/14 */ public class AesUtil { private AesUtil() { } /** * Generate AES key * * @param password encryption password * @return key * @throws NoSuchAlgorithmException The key generation algorithm does not support exceptions */ private static SecretKey generateAesKey(String password) throws NoSuchAlgorithmException { KeyGenerator keyGenerator = ("AES"); (128); byte[] passwordBytes = (StandardCharsets.UTF_8); MessageDigest digest = ("SHA-256"); byte[] keyBytes = (passwordBytes); return new SecretKeySpec(keyBytes, "AES"); } /** * Symmetric encryption algorithm AES encryption * * @param plaintext text to be encrypted * @param password encryption password * @return Encrypted ciphertext * @throws Exception Encryption exception */ public static String encryptWithAes(String plaintext, String password) throws Exception { Cipher cipher = ("AES"); SecretKey secretKey = generateAesKey(password); (Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedBytes = ((StandardCharsets.UTF_8)); return ().encodeToString(encryptedBytes); } /** * Symmetric encryption algorithm AES decryption * * @param ciphertext encrypted ciphertext * @param password encryption password * @return Decrypted plain text * @throws Exception Decryption exception */ public static String decryptWithAes(String ciphertext, String password) throws Exception { Cipher cipher = ("AES"); SecretKey secretKey = generateAesKey(password); (Cipher.DECRYPT_MODE, secretKey); byte[] decryptedBytes = (().decode(ciphertext)); return new String(decryptedBytes, StandardCharsets.UTF_8); } public static void main(String[] args) throws Exception { // AES symmetric encryption example String plaintext = "Hello, world!"; String password = "secretPassword"; // AES encryption String encryptWithAes = encryptWithAes(plaintext, password); ("AES Encrypted: " + encryptWithAes); // AES decryption String decryptWithAes = decryptWithAes(encryptWithAes, password); ("AES Decrypted: " + decryptWithAes); } }
2. RSA asymmetric encryption and decryption
package ; import ; import ; import .*; import .PKCS8EncodedKeySpec; import .X509EncodedKeySpec; import .Base64; /** * RSA asymmetric encryption * * @author wangbo * @date 2024/11/14 */ public class RsaUtil { private RsaUtil() { } /** * Generate RSA key pairs * * @return key pair * @throws NoSuchAlgorithmException The key generation algorithm does not support exceptions */ public static KeyPair generateRsaKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = ("RSA"); (2048); return (); } /** * Get the Base64 encoded string of RSA public key * * @return RSA public key Base64 encoded string */ public static String getRsaPublicKeyString(PublicKey publicKey) { KeyFactory keyFactory; try { keyFactory = ("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(()); return ().encodeToString((publicKeySpec).getEncoded()); } catch (Exception e) { (); return null; } } /** * Restore the string encoded according to Base64 to RSA public key * * @param publicKeyString RSA public key Base64 encoded string * @return RSA public key */ public static PublicKey getPublicKey(String publicKeyString) { try { KeyFactory keyFactory = ("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(().decode(publicKeyString)); return (publicKeySpec); } catch (Exception e) { (); return null; } } /** * Get the Base64 encoded string of RSA private key * * @return RSA private key Base64 encoded string */ public static String getRsaPrivateKeyString(PrivateKey privateKey) { KeyFactory keyFactory; try { keyFactory = ("RSA"); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(()); return ().encodeToString((privateKeySpec).getEncoded()); } catch (Exception e) { (); return null; } } /** * Restore the string encoded according to Base64 to RSA private key * * @param privateKeyString RSA private key Base64 encoded string * @return RSA private key */ public static PrivateKey getPrivateKey(String privateKeyString) { try { KeyFactory keyFactory = ("RSA"); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(().decode(privateKeyString)); return (privateKeySpec); } catch (Exception e) { (); return null; } } /** * Asymmetric encryption algorithm RSA encryption * * @param plaintext * @param publicKey public key * @return Encrypted ciphertext * @throws Exception Encryption exception */ public static String encryptWithRsa(String plaintext, PublicKey publicKey) throws Exception { Cipher cipher = ("RSA"); (Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = ((StandardCharsets.UTF_8)); return ().encodeToString(encryptedBytes); } /** * Asymmetric encryption algorithm RSA decryption * * @param ciphertext * @param privateKey private key * @return Decrypted plain text * @throws Exception Decryption exception */ public static String decryptWithRsa(String ciphertext, PrivateKey privateKey) throws Exception { Cipher cipher = ("RSA"); (Cipher.DECRYPT_MODE, privateKey); byte[] decryptedBytes = (().decode(ciphertext)); return new String(decryptedBytes, StandardCharsets.UTF_8); } public static void main(String[] args) throws Exception { // RSA asymmetric encryption example String plaintext = "Hello, RSA!"; //Generate RSA key pair KeyPair keyPair = generateRsaKeyPair(); //Get public key string String publicKeyString = getRsaPublicKeyString(()); ("RSA publicKeyString: " + publicKeyString); //Public key restore PublicKey publicKey = getPublicKey(publicKeyString); // Get private key string String privateKeyString = getRsaPrivateKeyString(()); ("RSA privateKeyString: " + privateKeyString); //Restore the private key PrivateKey privateKey = getPrivateKey(privateKeyString); // RSA public key encryption String encryptedRSA = encryptWithRsa(plaintext, publicKey); ("RSA Encrypted: " + encryptedRSA); // RSA private key decryption String decryptedRSA = decryptWithRsa(encryptedRSA, privateKey); ("RSA Decrypted: " + decryptedRSA); } }
3. Hash algorithm encryption
package ; import .slf4j.Slf4j; import ; import ; import ; import ; import ; /** * Hash algorithm encryption * * @author wangbo * @date 2024/11/14 */ @Slf4j public class HashUtil { private HashUtil() { } /** * Hash Algorithm SHA-256 * * @param plaintext * @return hash value * @throws NoSuchAlgorithmException The hashing algorithm does not support exceptions */ public static String hashWithSha256(String plaintext) throws NoSuchAlgorithmException { MessageDigest digest = ("SHA-256"); byte[] hashBytes = ((StandardCharsets.UTF_8)); return bytesToHex(hashBytes); } /** * Convert byte array to hexadecimal string * * @param bytes byte array * @return Hexadecimal string */ private static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { String hex = (0xFF & b); if (() == 1) { ('0'); } (hex); } return (); } /** * SHA password encryption * * @param sourceStr SourceStr * @return Encrypted string */ public static String sha(String sourceStr) { try { MessageDigest sha = ("SHA"); ((StandardCharsets.UTF_8)); return new BigInteger(1, ()).toString(32); } catch (NoSuchAlgorithmException e) { ("SHA string encryption exception", e); return null; } } /** * MD5 password encryption * * @param sourceStr SourceStr * @return Encrypted string */ public static String md5(String sourceStr) { try { MessageDigest md5 = ("MD5"); ((StandardCharsets.UTF_8)); return new BigInteger(1, ()).toString(16); } catch (NoSuchAlgorithmException e) { ("MD5 string encryption exception", e); return null; } } public static void main(String[] args) throws NoSuchAlgorithmException { String plaintext = "Hello Hash!"; String hashValue = hashWithSha256(plaintext); ("SHA-256 Hash: " + hashValue); String sha = sha(plaintext); ("SHA: " + sha); String md5 = md5(plaintext); ("MD5: " + md5); //Spring's tool class performs MD5 encryption String md5Spring = DigestUtils.md5DigestAsHex((StandardCharsets.UTF_8)); ("MD5Spring: " + md5Spring); //Commons-codec's tool class performs MD5 encryption String md5Commons = .md5Hex(plaintext); ("MD5Commons: " + md5Commons); } }
If you use commons-codec's tool class for MD5 encryption, you may need to introduce the following dependencies:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${}</version> </dependency>
4. Base64 codec
package ; import ; import .Base64; /** * Base64 * * @author wangbo * @date 2024/11/14 */ public class Base64Util { private Base64Util() { } /** * Base64 encoding * * @param plainText Content * @return Hexadecimal string */ public static String encodeBase64(String plainText) { byte[] plainBytes = (StandardCharsets.UTF_8); return ().encodeToString(plainBytes); } /** * Base64 decoding * * @param base64Text hexadecimal string * @return content */ public static String decodeBase64(String base64Text) { byte[] base64Bytes = ().decode(base64Text); return new String(base64Bytes, StandardCharsets.UTF_8); } public static void main(String[] args) { // Base64 encoding String base64Text = Base64Util.encodeBase64("Hello Base64!"); ("Base64 Encoded: " + base64Text); // Base64 decoding String decodedText = Base64Util.decodeBase64(base64Text); ("Base64 Decoded: " + decodedText); } }
This is the end of this article about the summary of commonly used text encryption and decryption tools in Java. For more related contents of Java encryption and decryption tools, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!