package cipher; import java.util.Scanner; public class Switch { public static void main(String args[]) { System.out.println("*******欢迎来到凯撒密码加解密系统*******"); System.out.println("请输入你所要使用的功能:1.加密 2.解密"); Scanner t=new Scanner(System.in); int choice=t.nextInt(); switch(choice) { case 1: Encrypt st=new Encrypt(); System.out.println("请输入明文:"); Scanner s1=new Scanner(System.in); String text=s1.nextLine();//测试明文的长度 System.out .println("请输入偏移量:"); Scanner s2=new Scanner(System.in); int key1=s2.nextInt(); st.encryption(key1, text);break; case 2: Decode sr=new Decode(); System.out.println("请输入密文"); Scanner s3=new Scanner(System.in); String code=s3.nextLine();//测试密文的长度 System.out.println("请输入偏移量"); Scanner s4=new Scanner(System.in); int key2=s4.nextInt(); sr.decryption(key2, code);break; } } }
时间: 2024-02-14 21:20:32 浏览: 19
这是一个使用Java语言编写的凯撒密码加解密程序,包含一个Switch类和两个分别用于加密和解密的类Encrypt和Decode。
程序启动后,首先会输出欢迎语和提示用户选择加密或解密功能。用户输入相应的数字后,程序根据用户选择执行相应的操作。
当用户选择加密功能时,程序会要求用户输入明文和偏移量,然后调用Encrypt类中的encryption方法对明文进行加密,并输出加密后的密文。
当用户选择解密功能时,程序会要求用户输入密文和偏移量,然后调用Decode类中的decryption方法对密文进行解密,并输出解密后的明文。
需要注意的是,该程序只能处理纯文本的加解密,对于包含数字、符号等复杂字符的消息无法处理。同时,该程序使用的是凯撒密码,安全性较低,不适用于对机密信息的加密。
相关问题
如何解释这个代码import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import java.util.Base64;import java.util.Scanner;public class DESExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入明文:"); String plainText = scanner.nextLine(); System.out.print("请输入密钥:"); String key = scanner.nextLine(); try { byte[] encrypted = encrypt(plainText.getBytes(), key.getBytes()); System.out.println("加密后的密文:" + Base64.getEncoder().encodeToString(encrypted)); byte[] decrypted = decrypt(encrypted, key.getBytes()); System.out.println("解密后的明文:" + new String(decrypted)); } catch (Exception e) { e.printStackTrace(); } } public static byte[] encrypt(byte[] plainText, byte[] key) throws Exception { DESKeySpec desKeySpec = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return cipher.doFinal(plainText); } public static byte[] decrypt(byte[] cipherText, byte[] key) throws Exception { DESKeySpec desKeySpec = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey); return cipher.doFinal(cipherText); }}
这段代码实现了使用DES算法进行加密和解密的功能。它包含了两个方法:encrypt和decrypt。encrypt方法接受明文和密钥作为参数,使用DES算法对明文进行加密,并返回加密后的密文。decrypt方法接受密文和密钥作为参数,使用DES算法对密文进行解密,并返回解密后的明文。在main方法中,程序从控制台获取用户输入的明文和密钥,然后调用encrypt和decrypt方法进行加密和解密,并输出加密后的密文和解密后的明文。值得注意的是,在加密和解密过程中,使用了ECB模式和PKCS5Padding填充方式。
DES加密算法java实现代码密钥自己输入
以下是DES加密算法的Java实现代码,可供参考:
```java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Scanner;
public class DESDemo {
public static void main(String[] args) throws Exception {
// 从用户输入中获取密钥
Scanner scanner = new Scanner(System.in);
System.out.print("请输入密钥:");
String keyStr = scanner.nextLine();
// 将密钥转换成字节数组
byte[] keyBytes = keyStr.getBytes();
// 生成DES密钥
SecretKey secretKey = new SecretKeySpec(keyBytes, "DES");
// 创建加密器
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// 获取待加密的明文
System.out.print("请输入明文:");
String plainText = scanner.nextLine();
byte[] plainBytes = plainText.getBytes();
// 加密明文
byte[] cipherBytes = cipher.doFinal(plainBytes);
// 将密文进行Base64编码
String cipherText = Base64.getEncoder().encodeToString(cipherBytes);
System.out.println("密文:" + cipherText);
}
}
```
该代码实现了从用户输入中获取密钥并加密明文的功能。另外,该实现使用了ECB模式和PKCS5Padding填充方式。注意:ECB模式是一种不安全的模式,不建议使用。