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

Java中加密和解密函数的使用方法

发布时间:2023-10-10 22:38:03

在Java中,我们可以使用各种加密算法来对数据进行加密和解密。以下是一些常见的加密和解密函数的使用方法:

1. 对称加密算法:

对称加密算法使用相同的密钥来进行加密和解密。常见的对称加密算法有AES和DES。

- AES加密:

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

     public class AESExample {
         public static void main(String[] args) throws Exception {
             String plainText = "Hello, World!";
             String encryptionKey = "SecretKey123456";

             // 创建AES加密器
             Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
             SecretKeySpec secretKey = new SecretKeySpec(encryptionKey.getBytes(), "AES");
             cipher.init(Cipher.ENCRYPT_MODE, secretKey);

             // 加密
             byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes());
             String encryptedText = Base64.getEncoder().encodeToString(encryptedTextBytes);
             System.out.println("加密后的文本:" + encryptedText);

             // 解密
             cipher.init(Cipher.DECRYPT_MODE, secretKey);
             byte[] decryptedTextBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
             String decryptedText = new String(decryptedTextBytes);
             System.out.println("解密后的文本:" + decryptedText);
         }
     }
     

- DES加密:

     import javax.crypto.Cipher;
     import javax.crypto.SecretKey;
     import javax.crypto.SecretKeyFactory;
     import javax.crypto.spec.DESKeySpec;
     import java.util.Base64;

     public class DESExample {
         public static void main(String[] args) throws Exception {
             String plainText = "Hello, World!";
             String encryptionKey = "SecretKey";

             // 创建DES加密器
             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
             DESKeySpec keySpec = new DESKeySpec(encryptionKey.getBytes());
             SecretKey secretKey = keyFactory.generateSecret(keySpec);
             Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
             cipher.init(Cipher.ENCRYPT_MODE, secretKey);

             // 加密
             byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes());
             String encryptedText = Base64.getEncoder().encodeToString(encryptedTextBytes);
             System.out.println("加密后的文本:" + encryptedText);

             // 解密
             cipher.init(Cipher.DECRYPT_MODE, secretKey);
             byte[] decryptedTextBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
             String decryptedText = new String(decryptedTextBytes);
             System.out.println("解密后的文本:" + decryptedText);
         }
     }
     

2. 非对称加密算法:

非对称加密算法使用一对密钥,即公钥和私钥,其中公钥用于加密,私钥用于解密。常见的非对称加密算法有RSA。

- RSA加密:

     import javax.crypto.Cipher;
     import java.security.KeyFactory;
     import java.security.KeyPair;
     import java.security.KeyPairGenerator;
     import java.security.interfaces.RSAPrivateKey;
     import java.security.interfaces.RSAPublicKey;
     import java.security.spec.PKCS8EncodedKeySpec;
     import java.security.spec.X509EncodedKeySpec;
     import java.util.Base64;

     public class RSAExample {
         public static void main(String[] args) throws Exception {
             String plainText = "Hello, World!";

             // 生成RSA公私钥对
             KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
             KeyPair keyPair = keyPairGenerator.generateKeyPair();
             RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
             RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

             // 创建RSA加密器
             Cipher cipher = Cipher.getInstance("RSA");
             cipher.init(Cipher.ENCRYPT_MODE, publicKey);

             // 加密
             byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes());
             String encryptedText = Base64.getEncoder().encodeToString(encryptedTextBytes);
             System.out.println("加密后的文本:" + encryptedText);

             // 解密
             cipher.init(Cipher.DECRYPT_MODE, privateKey);
             byte[] decryptedTextBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
             String decryptedText = new String(decryptedTextBytes);
             System.out.println("解密后的文本:" + decryptedText);
         }
     }
     

需要注意的是,加密和解密过程中都需要指定对应的加密算法、工作模式和填充方式,以确保加密和解密过程的一致性。此外,还需要正确处理密钥的生成、保存和传输,以确保其安全性。