编程练习 P124 6.8 JAVA为上题编写一个解密程序,即输入密文,输出明文

加密程序代码:

import java.util.Scanner;

public class Encrypt {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要加密的字符串:");
        String input = scanner.nextLine(); // 从键盘读取要加密的字符串
        String result = ""; // 存储加密后的字符串
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c >= 'a' && c < 'z') { // 小写字母
                c++;
            } else if (c == 'z') { // 小写字母z
                c = 'a';
            } else if (c >= 'A' && c < 'Z') { // 大写字母
                c++;
            } else if (c == 'Z') { // 大写字母Z
                c = 'A';
            }
            result += c; // 将加密后的字符添加到结果中
        }
        System.out.println("加密后的字符串为:" + result);
    }
}

解密程序代码:

import java.util.Scanner;

public class Decrypt {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要解密的字符串:");
        String input = scanner.nextLine(); // 从键盘读取要解密的字符串
        String result = ""; // 存储解密后的字符串
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c > 'a' && c <= 'z') { // 小写字母
                c--;
            } else if (c == 'a') { // 小写字母a
                c = 'z';
            } else if (c > 'A' && c <= 'Z') { // 大写字母
                c--;
            } else if (c == 'A') { // 大写字母A
                c = 'Z';
            }
            result += c; // 将解密后的字符添加到结果中
        }
        System.out.println("解密后的字符串为:" + result);
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,这是一个很好的编程问题。以下是一个简单的 Java 类,它可以根据用户 ID 生成密钥,将明文转换为密文密文转换为明文: ``` import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; public class CryptoUtils { private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding"; public static SecretKey generateKey(String userId) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); SecureRandom secureRandom = new SecureRandom(userId.getBytes()); keyGenerator.init(128, secureRandom); return keyGenerator.generateKey(); } public static byte[] encrypt(SecretKey key, byte[] input) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(input); } public static byte[] decrypt(SecretKey key, byte[] input) throws Exception { Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(input); } } ``` 这个类使用 AES 算法进行加密和解密。它包括三个方法: - `generateKey`:根据用户 ID 生成一个密钥。 - `encrypt`:将明文转换为密文。 - `decrypt`:将密文转换为明文。 你可以使用这个类来加密和解密数据。例如,下面的代码演示了如何使用它: ``` String userId = "123456"; String plaintext = "Hello, world!"; SecretKey key = CryptoUtils.generateKey(userId); byte[] ciphertext = CryptoUtils.encrypt(key, plaintext.getBytes()); byte[] decrypted = CryptoUtils.decrypt(key, ciphertext); String decryptedText = new String(decrypted); System.out.println(decryptedText); ``` 这个代码片段将输出 "Hello, world!"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_39719874

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值