import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Scanner;
public class DESEncryptor {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入密钥(8字节): ");
String key = scanner.nextLine();
System.out.print("请输入要加密的数据: ");
String data = scanner.nextLine();
if (key.length() != 8) {
System.out.println("密钥长度必须为8字节。");
return;
}
try {
Cipher cipher = Cipher.getInstance("DES");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
String encryptedString = Base64.getEncoder().encodeToString(encryptedData);
System.out.println("加密后的数据: " + encryptedString);
} catch (Exception e) {
System.out.println("加密出现错误: " + e.getMessage());
}
}
}