Cryptography in Java: Data Encryption and Hashing

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Cryptography in Java: Encryption and Hashing



In the modern world of information security, cryptography plays a key role. Java provides powerful built-in tools for implementing cryptographic operations through Java Cryptography Architecture (JCA) and Java Cryptography Extension (JCE). In this article, we will thoroughly examine the main mechanisms: symmetric encryption, asymmetric encryption, and hashing, as well as provide practical code examples.



Basic Concepts of Cryptography

Before moving on to the code, it is important to understand the difference between the main cryptographic primitives:

  • Symmetric encryption — uses the same key for encrypting and decrypting data. It is fast but requires secure key transmission.
  • Asymmetric encryption — uses a pair of keys: a public key (for encryption) and a private key (for decryption). It is slower but solves the key exchange problem.
  • Hashing — an irreversible transformation of data into a fixed-length string. It is used for integrity verification and password storage.


AES Symmetric Encryption in Java

AES (Advanced Encryption Standard) is the most popular symmetric algorithm. In Java, working with it is done through the Cipher class. Let's look at an example of encrypting and decrypting a string:



import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import java.util.Base64;

public class AesExample { public static void main(String[] args) throws Exception { // Generating an AES-128 key KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey secretKey = keyGen.generateKey(); // Original message String originalMessage = "Привет, криптография!"; System.out.println("Original message: " + originalMessage); // Encryption Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedBytes = cipher.doFinal(originalMessage.getBytes("UTF-8")); String encryptedMessage = Base64.getEncoder().encodeToString(encryptedBytes); System.out.println("Encrypted message: " + encryptedMessage); // Decryption cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedMessage)); String decryptedMessage = new String(decryptedBytes, "UTF-8"); System.out.println("Decrypted message: " + decryptedMessage); }}


The example uses ECB (Electronic Codebook) mode with PKCS5Padding padding. For production, it is recommended to use more secure modes such as CBC or GCM.



RSA Asymmetric Encryption

RSA is a classic asymmetric algorithm. In Java, the KeyPairGenerator and Cipher classes are used to work with it. It is important to remember: RSA can only encrypt data up to the size of the key (usually 2048 bits). For large amounts of data, RSA is combined with symmetric encryption (hybrid scheme).



import javax.crypto.Cipher;import java.security.*;import java.util.Base64;

public class RsaExample { public static void main(String[] args) throws Exception { // Generating an RSA-2048 key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair keyPair = keyGen.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); String secretMessage = "Секретные данные"; System.out.println("Original message: " + secretMessage); // Encryption with public key

Recommendations