对一个数进行加密和解密操作

加密:将一串数字先得到它的每一位数,然后将每一位加上5,再对10取余数,最后将所有数字反转,得到加密后的结果。
解密:将加密后的数字还原为原来的数字,由于还原的过程中需要用到对10取余数时的商,所以对10取余时候的商也要知道。

package Practices;

import java.util.Scanner;

public class PwdData {
    public static void main(String[] args) {
        //数字加密
        //某系统的数字密码:比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上
        //5,再对10求余,最后将所有数字反转,得到一串新数。
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入加密的数字:");
        int pwd = sc.nextInt();
        System.out.println("加密后的数字为:" + jiami(pwd));
        System.out.print("商为:[");
        int[] number = shang(pwd);
        for (int i = 0; i < number.length; i++)
            System.out.print(i==number.length-1 ? number[i] : number[i] + ",");//三元运算符
        System.out.println("]");
        System.out.println("解密后的数字为:" + jiemi(jiami(pwd),shang(pwd)));
    }
    public static int jiami(int pwd){
        int count = 1;
        int rs = 0;
        int n = pwd;
        while(pwd/10!=0) {
            count++;
            pwd /= 10;
        }
        int[] arr = new int[count];
        for (int i = 0; i < count; i++) {
            if (n != 0) {
                arr[i] = n % 10;
                n = n / 10;
            }
        }
        for (int j = 0; j < arr.length; j++) {
            arr[j] += 5;
            arr[j] %= 10;
            rs += arr[j]*Math.pow(10,count-1);
            count--;
        }return rs;
    }
    public static int[] shang(int pwd){
        int count = 1;
        int rs = 0;
        int n = pwd;
        while(pwd/10!=0) {
            count++;
            pwd /= 10;
        }
        int[] arr = new int[count];
        for (int i = 0; i < count; i++) {
            if (n != 0) {
                arr[i] = n % 10;
                n = n / 10;
            }
        }
        for (int j = 0; j < arr.length; j++) {
            arr[j] += 5;
            arr[j] /= 10;
        }
        return arr;
    }
    public static int jiemi(int pwd,int[] a){
        int count = 1;
        int rs = 0;
        int n = pwd;
        while(pwd/10!=0) {
            count++;
            pwd /= 10;
        }
        int[] arr = new int[count];
        for (int i = 0; i < count; i++) {
            if (n != 0) {
                arr[i] = n % 10;
                n = n / 10;
            }
        }
        for (int j = 0; j < arr.length; j++) {
            arr[j] += a[j] * 10;
            arr[j] -= 5;
            rs += arr[j]*Math.pow(10,count-1);
            count--;
        }return rs;
    }
}

运行结果如下所示:
实例一:
在这里插入图片描述
实例二:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java中对文本文件进行加密解密操作可以使用加密算法解密算法来实现。常用的加密算法有DES、AES、RSA等,解密算法则是对应的解密算法。下面是一个简单的示例,使用DES算法对文本文件进行加密解密加密操作: ```java import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.SecureRandom; public class FileEncryptor { public static void encrypt(String key, File inputFile, File outputFile) throws Exception { FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; SecureRandom secureRandom = new SecureRandom(key.getBytes()); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes()); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, secureRandom); int length; while ((length = inputStream.read(buffer)) != -1) { byte[] output = cipher.update(buffer, 0, length); if (output != null) { outputStream.write(output); } } byte[] output = cipher.doFinal(); if (output != null) { outputStream.write(output); } inputStream.close(); outputStream.flush(); outputStream.close(); } } ``` 解密操作: ```java import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.SecureRandom; public class FileDecryptor { public static void decrypt(String key, File inputFile, File outputFile) throws Exception { FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024]; SecureRandom secureRandom = new SecureRandom(key.getBytes()); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes()); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, secretKey, secureRandom); int length; while ((length = inputStream.read(buffer)) != -1) { byte[] output = cipher.update(buffer, 0, length); if (output != null) { outputStream.write(output); } } byte[] output = cipher.doFinal(); if (output != null) { outputStream.write(output); } inputStream.close(); outputStream.flush(); outputStream.close(); } } ``` 示例中使用的是DES算法,可以根据需要替换为其他算法。对于密钥,建议使用安全的方式存储,例如通过密码学哈希函对其进行散列,以保证安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值