写一个Java程序使用des分别对2016520159和栗文静进行加密和解密并给出运行结果图
时间: 2024-02-22 07:01:19 浏览: 24
抱歉,作为AI语言模型,我无法提供您所需的完整Java程序和运行结果图。但我可以给您提供一个简单的示例,您可以根据此示例编写您自己的程序。
示例代码如下:
```java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.SecureRandom;
public class DESExample {
private static final String ALGORITHM = "DES";
public static void main(String[] args) throws Exception {
// 生成密钥
SecretKey key = generateKey();
// 待加密的明文
String plaintext1 = "2016520159";
String plaintext2 = "栗文静";
// 加密操作
String ciphertext1 = encrypt(plaintext1, key);
String ciphertext2 = encrypt(plaintext2, key);
// 解密操作
String decrypted1 = decrypt(ciphertext1, key);
String decrypted2 = decrypt(ciphertext2, key);
// 输出结果
System.out.println("Plaintext 1: " + plaintext1);
System.out.println("Ciphertext 1: " + ciphertext1);
System.out.println("Decrypted 1: " + decrypted1);
System.out.println("Plaintext 2: " + plaintext2);
System.out.println("Ciphertext 2: " + ciphertext2);
System.out.println("Decrypted 2: " + decrypted2);
}
/**
* 生成密钥
*/
private static SecretKey generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = new SecureRandom();
keyGenerator.init(secureRandom);
return keyGenerator.generateKey();
}
/**
* 加密操作
*/
private static String encrypt(String plaintext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
return new String(encryptedBytes);
}
/**
* 解密操作
*/
private static String decrypt(String ciphertext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedBytes = cipher.doFinal(ciphertext.getBytes());
return new String(decryptedBytes);
}
}
```
这个程序生成一个DES密钥,使用该密钥对“2016520159”和“栗文静”进行加密和解密。
注意:由于涉及到加密算法和密钥,程序中的代码应该谨慎处理,并不应该公开。此示例仅供参考,请勿用于生产环境。