Cryptography in Java: Encryption and Hashing — Detailed Guide
In the modern world of information security, cryptography is the foundation of data protection. Java provides a powerful built-in toolkit for implementing encryption and hashing through the javax.crypto and java.security packages. In this article, we will thoroughly examine the main cryptographic operations in Java: symmetric and asymmetric encryption, password hashing, and working with digital signatures.
1. Fundamentals of Cryptography in Java
Java Cryptography Architecture (JCA) is a framework that allows developers to easily integrate cryptographic algorithms. Key classes:
javax.crypto.Cipher— for encryption and decryption.java.security.MessageDigest— for hashing.java.security.KeyPairGenerator— for key generation.javax.crypto.spec.SecretKeySpec— for creating symmetric keys.
To work with cryptography, import:
import javax.crypto.*;import javax.crypto.spec.*;import java.security.*;2. Symmetric Encryption (AES)
Symmetric encryption uses a single key for both encryption and decryption. AES (Advanced Encryption Standard) is the most popular algorithm. Example of encrypting a string:
import javax.crypto.*;import javax.crypto.spec.*;import java.util.Base64;
public class AesExample { public static void main(String[] args) throws Exception { String plainText = "Secret message"; String secretKey = "0123456789abcdef"; // 16 bytes for AES-128
// Creating the key SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// Encryption cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); System.out.println("Encrypted: " + encryptedText);
// Decryption cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText)); String decryptedText = new String(decryptedBytes); System.out.println("Decrypted: " + decryptedText); }}Important: The ECB mode is not recommended for large amounts of data. Use AES/GCM/NoPadding for better security.
3. Asymmetric Encryption (RSA)
Asymmetric encryption uses a pair of keys: a public key (for encryption) and a private key (for decryption). RSA is a classic algorithm. Example:
import javax.crypto.*;import java.security.*;import java.util.Base64;
public class RsaExample { public static void main(String[] args) throws Exception { // Generating a key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair pair = keyGen.generateKeyPair(); PublicKey publicKey = pair.getPublic(); PrivateKey privateKey = pair.getPrivate();
// Encryption with the public key Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedBytes = cipher.doFinal("Secret data".getBytes()); String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); System.out.println("Encrypted with RSA: " + encryptedText);
// Decryption with the private key cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText)); Also in library
Working with S3 and Cloud Storage in C++: AWS SDK for C++ Library
PDF Generation in Kotlin: Best Libraries and Code Examples
Lottie Android for Kotlin: Complete Guide with Examples
Elasticsearch Client for Java: Complete Guide to Working with Java Client
OAuth client for C# (.NET): IdentityModel library and OAuth 2.0 integration