Java小案例----数字密码的加密与解密

在数据传输中,为了网络安全,数字的加密极为重要,以下将说明Java是如何将四位数字密码进行加密和解密的过程!

本例题如下:

 

1.对数字的加密:

需要对数字加密,具体思路就是将数据先进行加五操作,然后进行对十取余,最后在进行逆置操作。首先就得输入数字,比如输入一个整形的四位数字,如果当靠一个四位数字可能操作会受限,所以为了对数字有一个更好的操作,所以我们需要将四位数字的每一位都放进一个数组里面,方面处理!需定义一个打包方法,将四位数字打包为一个数字!

    public static int[] pack(int result) {
        int arr[] = new int[4];
//        arr[0] = result / 1000 % 10;
//        arr[1] = result / 100 % 10;
//        arr[2] = result / 10 % 10;
//        arr[3] = result % 10;
//        由以上规律不难推到下面的批量操作!
        for (int i = 0; i < arr.length; i++) {
            arr[i] = result%10;
            result /= 10;
        }
        return arr;
}

 接着便可以对其进行进一步的操作,按照需求,需要将其数字每一位都进行加五以及对十取余的操作,最后在进行数字的逆置,便完成对数字的解密工作。于是定义了如下方法,专门对其进行加密操作:

 public static String lock(int[] arr) {
        String result = "";
        //以下为加五操作以及对十取余操作
        for (int i = 0; i < arr.length; i++) {
            arr[i] += 5;
            arr[i] %= 10;
        }
        //以下为数组逆置操作
        for (int i = 0,j=arr.length-1; i < j; i++,j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        //以下为将数组重新拼接为字符串,在主函数中会将其转为整形!
        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }
        return result;
    }

 注意:此时result返回的是字符串类型,可以通过调用类方法:

Integer.parseInt();//可以将字符串转换为整形!

 将字符串转换为整形还有许多方法,可以参考其他博主的博客!

完成以上步骤之后便完成了对数字的加密工作,那么如何将其进行解密呢?也是本案例中最为复杂的地方。

2.对数字的解密:

同样需要将传入的整形类型打包为数组类型方便操作,方法同上。而后就是要将刚刚的加密操作反过来操作,那么问题来了,对十取余的逆运算是什么呢?

在上课的时候也没有听说过还有对十取余的逆运算,那么如何解决呢?

其实对于这道题是比较简单的,可以通过找规律的方法!

 依据规则,而且在数组当中,因为存的数据为原始数据的每一位数,所以不可能超过十,那么进行加五之后,取余之前的范围就为:

5,6,7,8,9,10,11,12,13,14(5~14)

用以上的数据取余之后便可得到以下:

5,6,7,8,9,0,1,2,3,4

 由上面的规律便可知道,当其数据在(5~9)的范围内,对十取余之后不变,当数据在(10~14)范围内时,数据只取各位的数字。于是你便可以定义如下的方法对其进行解码操作!

 public static String solve(int[] arr) {
        String result = "";
        //以下为对数据进行逆置操作
        for (int i = 0,j=arr.length-1; i < j; i++,j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
        //若在5~9范围内,则对其数据进行保留,然后进行减五操作,得到原数据。
            if(arr[i]>4 && arr[i]<10){
                arr[i] -= 5;
            }
        //若数据在0~4范围内(也就是取余之后范围在10~14的数据),
        //则对其先进行加十操作,然后再进行减五操作。
            else{
                arr[i] += 10;
                arr[i] -= 5;
            }
        }
        //最后对其进行拼接操作!
        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }
        return result;
    }

完成以上操作便可以实现对数据的加密与解密啦!以下为完整代码!

package com.wxy.demo1;

import java.util.Scanner;

public class 数字加密 {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int arr[] = new int[4];

        System.out.println("请输入四位数!");
        int result = sc.nextInt();
        //此处为检测是否输入的数据为四位数,否则就一直重试!
        while (result / 10000 != 0) {
            System.out.println("您的输入非法!请重新输入一个四位数数字!");
            result = sc.nextInt();
        }
        arr = pack(result);
        System.out.println("请选择你的模式");
        int i = 0;
        System.out.println("输入1为加密,输入2为解密");
        i = sc.nextInt();
        switch (i) {
            case 1 -> result = Integer.parseInt(lock(arr));
            case 2 -> result = Integer.parseInt(solve(arr));
        }
        System.out.println(result);
    }

    public static int[] pack(int result) {
        int arr[] = new int[4];
//        arr[0] = result / 1000 % 10;
//        arr[1] = result / 100 % 10;
//        arr[2] = result / 10 % 10;
//        arr[3] = result % 10;
        for (int i = 0; i < arr.length; i++) {
            arr[i] = result%10;
            result /= 10;
        }
        return arr;
    }

    public static String lock(int[] arr) {
        String result = "";
        for (int i = 0; i < arr.length; i++) {
            arr[i] += 5;
            arr[i] %= 10;
        }
        for (int i = 0,j=arr.length-1; i < j; i++,j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }
        return result;
    }

    public static String solve(int[] arr) {
        String result = "";
        for (int i = 0,j=arr.length-1; i < j; i++,j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]>5 && arr[i]<10){
                arr[i] -= 5;
            }
            else{
                arr[i] += 10;
                arr[i] -= 5;
            }
        }//因为取余之后的范围为5,14.所以可以采用规律法。
        for (int i = 0; i < arr.length; i++) {
            result += arr[i];
        }
        return result;
    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java实现DES加密算法的简单案例: ```java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class DESExample { private static final String ALGORITHM = "DES"; private static final String KEY = "mysecretkey"; // 密钥,必须是8个字符 public static void main(String[] args) throws Exception { String plaintext = "Hello, world!"; System.out.println("原文:" + plaintext); byte[] encryptedBytes = encrypt(plaintext); System.out.println("加密后的内容:" + new String(encryptedBytes)); String decryptedText = decrypt(encryptedBytes); System.out.println("解密后的内容:" + decryptedText); } private static byte[] encrypt(String plaintext) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); keyGenerator.init(56); SecretKey secretKey = keyGenerator.generateKey(); byte[] key = KEY.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(key, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); return cipher.doFinal(plaintext.getBytes()); } private static String decrypt(byte[] encryptedBytes) throws Exception { byte[] key = KEY.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(key, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); return new String(decryptedBytes); } } ``` 在这个案例中,我们使用了Java加密javax.crypto,其中包含了DES加密算法的实现。在主方法中,我们首先定义了一个字符串作为原文,然后调用encrypt方法对原文进行加密,并输出加密后的内容。接着调用decrypt方法对加密后的内容进行解密,并输出解密后的内容。 在encrypt方法中,我们首先使用KeyGenerator生成一个56位的密钥,然后将密钥转换成SecretKeySpec对象。接着使用Cipher对象对原文进行加密,并返回加密后的字节数组。 在decrypt方法中,我们将密钥转换成SecretKeySpec对象,然后使用Cipher对象对加密后的字节数组进行解密,并返回解密后的字符串。 需要注意的是,DES加密算法的密钥必须是8个字符,这里我们使用了一个固定的字符串作为密钥。在实际应用中,我们应该使用更为复杂和安全的密钥生成方式,如使用随机数生成密钥。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值