Find the dividend when remainder is given

时间:2016-02-14 09:58:12

标签: java encryption

I have written a program in which I'm encrypting user input. The input is only four digit long. The encryption formula is as follow:

  1. Ask user to enter 4 digits
  2. iterate those one by one and add 7 into those.
  3. Get remainder of each number by dividing by 10.
  4. swap 1 with 3 and 2 with 4.

suppose I have entered 1234. After adding the number 7 in each digit, the number looks like 891011. The remainder of these number are 8901. Now I swap those and the final number looks like 0189.This is all about the encrypted program.

USER INPUT : 0189

Now I have to write another separate program which decrypt user's input and find out the actual number. The problem I'm facing is, I have to find the dividend first. But given data I have only is divisor and remainder (In this case 10 and 0). Please guide me if I'm wrong. Thanks in Advance.

3 个答案:

答案 0 :(得分:5)

翻译表没有显示任何重复,所以这里没问题:

setContentView

所以要解密:

  • 取消洗牌数字
  • 每个数字减去7,如果结果为负,则添加10(或使用转换表)

答案 1 :(得分:2)

我们做一些计算:

  • 每个原始数字的范围为0到9。

  • 添加7后,每个范围从7到16。

  • 除以10之后,7到9之间的任何数字都是相同的;任何x数字范围从10到16将在0到6之间。
  • 要查找原稿,请为每个最终数字执行

    if (x <= 6) original = x + 10 - 7; if (x > 6) original = x - 7;

答案 2 :(得分:1)

我在JAVA中使用一个简单的程序尝试了它并且没有问题。

以下是代码段,如果你想试一试:

public static void main (String[] args)
{
    Scanner in = new Scanner(System.in);
    /* Start Encrypting */
    String encrypted = encrypt(in.nextInt(), 4);
    System.out.println("Encrypted: " + encrypted);
    /* Start Decrypting */
    String decrypted = decrypt(encrypted, 4);
    System.out.println("Decrypted: " + decrypted);
}

/* Encryption Logic */
public static String encrypt(int x, int size) {
    int[] output = new int[size];
    /* Add 7, Mod 10 Logic */
    for(int i = size - 1; x > 0; i--) {
        output[i] = ((x % 10) + 7) % 10; 
        x /= 10;
    }
    /* Swap Logic */
    for(int i = 0; i < size/2; i++) {
        int temp = output[i];
        output[i] = output[i+2];
        output[i+2] = temp;
    }
    /* Return Result */
    return Arrays.toString(output).replace(", ","").substring(1,size+1);
}

/* Decryption Logic */
public static String decrypt(String x, int size) {
    /* Get Chars */
    char[] input = x.toCharArray();
    int[] output = new int[size];
    /* Sub 7, Mod 10 Logic */
    for(int i = 0; i < input.length; i++) {
        output[i] = (((int)(input[i]) - 48) + 3) % 10;
    }
    /* Swap Logic */
    for(int i = 0; i < size/2; i++) {
        int temp = output[i];
        output[i] = output[i+2];
        output[i+2] = temp;
    }
    /* Return Result */
    return Arrays.toString(output).replace(", ","").substring(1,size+1);
}

输入:

1234

输出:

Encrypted: 0189
Decrypted: 1234