java键盘输入加密方法

package encipher;
import java.util.Scanner;
public class digitalEncryptor {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);// 键盘输入
System.out.println("请输入数字!");
double num = sc.nextDouble();
int result;
result = (int) ((num * 10 + 5) / 2 + 3.14159);
System.out.println("加密结果:" + result);
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java可以使用JCE(Java Cryptography Extension)提供的API实现DES加密解密算法,以下是一个简单的示例: ```java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; 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 plainText = scanner.nextLine(); System.out.print("请输入密钥(8个字符):"); String key = scanner.nextLine(); byte[] encrypted = encrypt(plainText, key); System.out.println("加密后的密文:" + new String(encrypted)); String decrypted = decrypt(encrypted, key); System.out.println("解密后的明文:" + decrypted); } private static byte[] encrypt(String plainText, String key) throws Exception { // 生成密钥 KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); keyGenerator.init(56); SecretKey secretKey = keyGenerator.generateKey(); // 加密 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encrypted = cipher.doFinal(plainText.getBytes()); return encrypted; } private static String decrypt(byte[] encrypted, String key) throws Exception { // 生成密钥 KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); keyGenerator.init(56); SecretKey secretKey = keyGenerator.generateKey(); // 解密 Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decrypted = cipher.doFinal(encrypted); return new String(decrypted); } } ``` 在上述代码中,我们通过 `KeyGenerator` 生成了一个 DES 密钥,并使用 `Cipher` 对象实现了 DES 加密和解密。注意,为了确保安全性,DES 的密钥长度必须为 8 个字符,否则会抛出异常。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值