欢迎访问宙启技术站
智能推送

Java函数库中有哪些加密和解密函数?如何使用它们?

发布时间:2023-06-26 17:42:20

Java函数库中包含很多加密和解密函数,其中一些最常见的函数包括:MD5、SHA、AES、DES等。

MD5(Message-Digest Algorithm 5)是一种常用的单向加密算法。在Java中,可以使用java.security.MessageDigest类的实例来调用MD5加密函数。示例代码如下:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Example {

   public static void main(String[] args) {
      String input = "Hello world";

      try {
         MessageDigest md = MessageDigest.getInstance("MD5");
         byte[] messageDigest = md.digest(input.getBytes());

         StringBuilder stringBuilder = new StringBuilder();
         for (int i = 0; i < messageDigest.length; i++) {
            stringBuilder.append(Integer.toHexString((messageDigest[i] & 0xFF) | 0x100).substring(1,3));
         }
         System.out.println("MD5 Hash: " + stringBuilder.toString());
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error: " + e.getMessage());
      }
   }

}

SHA(Secure Hash Algorithm)是更强大的单向加密算法,提供更高的安全性。Java中也提供java.security.MessageDigest类的实例来调用SHA加密函数。示例代码如下:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHAExample {

   public static void main(String[] args) {
      String input = "Hello world";

      try {
         MessageDigest md = MessageDigest.getInstance("SHA-256");
         byte[] messageDigest = md.digest(input.getBytes());

         StringBuilder stringBuilder = new StringBuilder();
         for (int i = 0; i < messageDigest.length; i++) {
            stringBuilder.append(Integer.toHexString((messageDigest[i] & 0xFF) | 0x100).substring(1,3));
         }
         System.out.println("SHA Hash: " + stringBuilder.toString());
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error: " + e.getMessage());
      }
   }

}

AES(Advanced Encryption Standard)是一种对称加密算法,可以加密和解密数据。在Java中,可以使用javax.crypto.Cipher类的实例来调用AES加密和解密函数。示例代码如下:

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

public class AESEncryptionExample {

   private static final String KEY = "This is a secret key";
   private static final String ALGORITHM = "AES/ECB/PKCS5Padding";

   public static void main(String[] args) {
      String input = "Hello world";
      String encrypted = encrypt(input);
      String decrypted = decrypt(encrypted);
      System.out.println("Encrypted: " + encrypted);
      System.out.println("Decrypted: " + decrypted);
   }

   public static String encrypt(String input) {
      try {
         SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES");
         Cipher cipher = Cipher.getInstance(ALGORITHM);
         cipher.init(Cipher.ENCRYPT_MODE, keySpec);
         byte[] encryptedBytes = cipher.doFinal(input.getBytes());
         return Base64.getEncoder().encodeToString(encryptedBytes);
      } catch (Exception ex) {
         System.out.println("Error: " + ex.getMessage());
      }
      return null;
   }

   public static String decrypt(String input) {
      try {
         SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "AES");
         Cipher cipher = Cipher.getInstance(ALGORITHM);
         cipher.init(Cipher.DECRYPT_MODE, keySpec);
         byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(input));
         return new String(decryptedBytes);
      } catch (Exception ex) {
         System.out.println("Error: " + ex.getMessage());
      }
      return null;
   }

}

DES(Data Encryption Standard)是另一种对称加密算法,逐渐被AES所取代。Java中,可以使用javax.crypto.Cipher类的实例来调用DES加密和解密函数。示例代码如下:

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

public class DESEncryptionExample {

   private static final String KEY = "This is a secret key";
   private static final String ALGORITHM = "DES/ECB/PKCS5Padding";

   public static void main(String[] args) {
      String input = "Hello world";
      String encrypted = encrypt(input);
      String decrypted = decrypt(encrypted);
      System.out.println("Encrypted: " + encrypted);
      System.out.println("Decrypted: " + decrypted);
   }

   public static String encrypt(String input) {
      try {
         SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "DES");
         Cipher cipher = Cipher.getInstance(ALGORITHM);
         cipher.init(Cipher.ENCRYPT_MODE, keySpec);
         byte[] encryptedBytes = cipher.doFinal(input.getBytes());
         return Base64.getEncoder().encodeToString(encryptedBytes);
      } catch (Exception ex) {
         System.out.println("Error: " + ex.getMessage());
      }
      return null;
   }

   public static String decrypt(String input) {
      try {
         SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), "DES");
         Cipher cipher = Cipher.getInstance(ALGORITHM);
         cipher.init(Cipher.DECRYPT_MODE, keySpec);
         byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(input));
         return new String(decryptedBytes);
      } catch (Exception ex) {
         System.out.println("Error: " + ex.getMessage());
      }
      return null;
   }

}

使用这些加密和解密函数,可以帮助Java开发者保护敏感信息,保证数据安全。