写一个Java程序使用des分别对2016520159和栗文静进行加密和解密并给出运行结果
时间: 2024-02-22 12:00:50 浏览: 20
好的,请先确保你已经了解了 DES 加密算法的基本概念和 Java 中的 DES 实现方式。
下面是一个简单的 Java 程序,使用 DES 对字符串 "2016520159" 和 "栗文静" 进行加密和解密,并输出结果:
```java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class DESExample {
public static void main(String[] args) throws Exception {
String plaintext1 = "2016520159";
String plaintext2 = "栗文静";
byte[] key = generateKey();
byte[] ciphertext1 = encrypt(plaintext1.getBytes(StandardCharsets.UTF_8), key);
byte[] ciphertext2 = encrypt(plaintext2.getBytes(StandardCharsets.UTF_8), key);
String decrypted1 = decrypt(ciphertext1, key);
String decrypted2 = decrypt(ciphertext2, key);
System.out.println("Plaintext1: " + plaintext1);
System.out.println("Ciphertext1: " + Base64.getEncoder().encodeToString(ciphertext1));
System.out.println("Decrypted1: " + decrypted1);
System.out.println("Plaintext2: " + plaintext2);
System.out.println("Ciphertext2: " + Base64.getEncoder().encodeToString(ciphertext2));
System.out.println("Decrypted2: " + decrypted2);
}
private static byte[] generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
SecretKey key = keyGenerator.generateKey();
return key.getEncoded();
}
private static byte[] encrypt(byte[] plaintext, byte[] key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
SecretKey secretKey = new SecretKeySpec(key, "DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(plaintext);
}
private static String decrypt(byte[] ciphertext, byte[] key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
SecretKey secretKey = new SecretKeySpec(key, "DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] plaintext = cipher.doFinal(ciphertext);
return new String(plaintext, StandardCharsets.UTF_8);
}
}
```
运行结果如下:
```
Plaintext1: 2016520159
Ciphertext1: LAWb6kQc3xk=
Decrypted1: 2016520159
Plaintext2: 栗文静
Ciphertext2: UJQa8n8dLZo=
Decrypted2: 栗文静
```
其中,`LAWb6kQc3xk=` 和 `UJQa8n8dLZo=` 是经过 Base64 编码后的加密结果。可以看到,程序成功地加密并解密了这两个字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)