Location>code7788 >text

Java encryption and decryption

Popularity:532 ℃/2025-02-22 20:48:52

In Java, encryption and decryption are usually done throughJava Cryptography Architecture (JCA)andJava Cryptography Extension (JCE)accomplish. The following are examples of common encryption and decryption operations, including symmetric encryption (such as AES) and asymmetric encryption (such as RSA).


1. Symmetric encryption (AES example)

Symmetric encryption uses the same key for encryption and decryption.

import ;
 import ;
 import ;
 import .Base64;

 public class AESExample {
     public static void main(String[] args) throws Exception {
         // Generate key
         KeyGenerator keyGen = ("AES");
         (128); // 128-bit key
         SecretKey secretKey = ();

         // Encryption
         String originalText = "Hello, World!";
         Cipher cipher = ("AES");
         (Cipher.ENCRYPT_MODE, secretKey);
         byte[] encryptedBytes = (());
         String encryptedText = ().encodeToString(encryptedBytes);
         ("After encryption: " + encryptedText);

         // Decrypt
         (Cipher.DECRYPT_MODE, secretKey);
         byte[] decryptedBytes = (().decode(encryptedText));
         String decryptedText = new String(decryptedBytes);
         ("Decrypted: " + decryptedText);
     }
 }

2. Asymmetric encryption (RSA example)

Asymmetric encryption uses public key encryption and private key decryption.

import ;
 import ;
 import ;
 import .Base64;

 public class RSAExample {
     public static void main(String[] args) throws Exception {
         // Generate key pair
         KeyPairGenerator keyPairGen = ("RSA");
         (2048); // 2048-bit key
         KeyPair keyPair = ();

         // Encryption
         String originalText = "Hello, World!";
         Cipher cipher = ("RSA");
         (Cipher.ENCRYPT_MODE, ());
         byte[] encryptedBytes = (());
         String encryptedText = ().encodeToString(encryptedBytes);
         ("After encryption: " + encryptedText);

         // Decrypt
         (Cipher.DECRYPT_MODE, ());
         byte[] decryptedBytes = (().decode(encryptedText));
         String decryptedText = new String(decryptedBytes);
         ("Decrypted: " + decryptedText);
     }
 }

3. Hash algorithm (SHA-256 example)

The hashing algorithm is used to generate irreversible digest information.

import ;
 import ;

 public class SHA256Example {
     public static void main(String[] args) throws Exception {
         String originalText = "Hello, World!";
         MessageDigest digest = ("SHA-256");
         byte[] hashBytes = (());
         String hashText = ().formatHex(hashBytes);
         ("SHA-256 hash: " + hashText);
     }
 }

4. Things to note

  1. Key Management: The security of keys is crucial, and it is recommended to use secure key storage and management methods (such as KeyStore).
  2. Algorithm selection: Choose the appropriate encryption algorithm according to your needs (such as AES for symmetric encryption and RSA for asymmetric encryption).
  3. Fill mode: The filling mode must be specified during encryption (such asAES/CBC/PKCS5Padding) to ensure consistency with the decryption party.
  4. Exception handling: The encryption and decryption operation may throw an exception, and appropriate exception handling is required.

5. Common encryption algorithms

type algorithm describe
Symmetric encryption AES Advanced encryption standard, fast speed
Symmetric encryption DES/3DES Data encryption standard, older
Asymmetric encryption RSA Based on large numbers, high security
Asymmetric encryption ECC Elliptic curve encryption, short key efficiency
Hash algorithm SHA-256 Generate 256-bit hash
Hash algorithm MD5 Generate 128-bit hash, which is no longer safe

With the above examples and instructions, you can implement basic encryption and decryption operations in Java. If more complex features are needed (such as digital signatures, certificate management, etc.), the advanced features of JCA and JCE can be further studied. 😊