Java 创建一个简单的加密程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21290382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:55:39  来源:igfitidea点击:

Creating a simple encryption program

javaencryption

提问by Christopher Baldwin

I'd like to use java to make a cipher of sorts, but im not sure how to go about it.

我想使用 java 来制作各种密码,但我不知道如何去做。

Basically, I'd want the machine to accept a string of text, say "Abcd"

基本上,我希望机器接受一串文本,比如“Abcd”

and then a key, say '4532'

然后是一个键,说“4532”

The program should move the characters forward in the alphabet if the number matching the place of the letter is even, and backward if it's odd.

如果匹配字母位置的数字是偶数,程序应该在字母表中向前移动字符,如果是奇数则向后移动。

If there is no number, the key should loop around until it's out of characters in the string to change.

如果没有数字,键应该循环直到字符串中的字符用完才能更改。

the program would then print the key. Ideally, if im pseudocoding this correctly, deciphering the string would be a reverse process only applicable with the key.

然后程序将打印密钥。理想情况下,如果我正确地对此进行了伪编码,则解密字符串将是仅适用于密钥的反向过程。

I'm guessing i'd use a combination of an array and if/else statements.

我猜我会使用数组和 if/else 语句的组合。

I'm not sure where to start.

我不知道从哪里开始。

Example & edit String: 'hello' Key: '12'

示例和编辑字符串:'hello' 键:'12'

a b c d e f g h i j k l m n o p q r s t u v w x y z

abcdefghijklmnopqrstu vwxyz

Because the corresponding key value is 1, h will travel backwards that many spaces.

因为对应的键值为 1,所以 h 将向后移动那么多空格。

h = g

小时 = 克

because e has a 2, it'll move forward that many spaces.

因为 e 有一个 2,它会向前移动很多空间。

e = g

e = g

the first l then becomes k, while the second becomes n. The Key is repeated because the string is out of numbers to compare. o turns into n because it's matched with 1.

第一个 l 然后变成 k,而第二个变成 n。由于字符串没有要比较的数字,因此键重复。o 变成 n,因为它与 1 匹配。

hello would become ggknn with the key 42.

hello 将成为 ggknn 的键为 42。

采纳答案by Martin Dinov

Here are possible steps you can take to do this. This is not an exact and working solution, but it will hopefully get you started.

您可以采取以下步骤来执行此操作。这不是一个准确且有效的解决方案,但它有望帮助您入门。

  1. Start by reading input from the console (via Scanneror a BufferedReaderfor example).
  2. Split your input on spaces perhaps, so that you have a String[]of words.
  3. Loop through the String[]of words, and loop again for which word. You can have a counter that is incremented in each iteration of an inner loop and gets reset at the end of an inner loop. You can use that counter variable to get a position into the key (key[counter%lengthOfKey]) in each iteration of the inner loop. If the (counter%lengthOfKey)%2 == 0, you have the even number case for the key, else the odd numbered case. Do whatever encryption at that point (simple substitution cipher for example).
  1. 首先从控制台读取输入(例如通过Scanner或 a BufferedReader)。
  2. 也许将您的输入拆分为空格,以便您有一个String[]单词。
  3. 循环遍历String[]单词,然后再次循环查找哪个单词。您可以拥有一个计数器,该计数器在内部循环的每次迭代中递增,并在内部循环结束时重置。您可以使用该计数器变量key[counter%lengthOfKey]在内循环的每次迭代中获取键 ( )的位置。如果(counter%lengthOfKey)%2 == 0,则密钥为偶数大小写,否则为奇数大小写。在那个时候做任何加密(例如简单的替换密码)。

回答by schmidt73

There are many methods of Encryption, but if you want to learn about Encryption you should start with the study of XOR encryption. XOR Encryption uses a key and XORs the binary code of every character with the key. If the key is longer than the encrypted code it creates a One-Time Pad that is impossible to decrypt.

加密的方法有很多,但是如果你想了解加密,你应该从研究异或加密开始。XOR Encryption 使用密钥并将每个字符的二进制代码与密钥进行异或。如果密钥比加密代码长,它会创建一个无法解密的一次性密码本。

XOR - Exclusive OR - Unlike OR both values can not be true at the same time.

XOR - 异或 - 与 OR 不同,两个值不能同时为真。

Simple Explanation:

简单说明:

  1. Pretend you want to encrypt the string "hello world" with the key 'c'.
  2. For every character in the string XOR it with the key c.
  3. Pretend the binary value of h is 1100011 and the binary value of c is 0010110 (these are made up and will not work) then you XOR every corresponding binary value.

    1100011
    XOR
    0010110
    -------
    1110101
    
  1. 假设您想用密钥“c”加密字符串“hello world”。
  2. 对于字符串中的每个字符,它都与键 c 进行异或。
  3. 假设 h 的二进制值是 1100011,c 的二进制值是 0010110(这些是编造出来的,不起作用),然后你对每个对应的二进制值进行异或。

    1100011
    XOR
    0010110
    -------
    1110101
    

1110101 is the XORed binary value.

1110101 是异或二进制值。

  1. You then cast the binary value back into character and you do this for every step of the encrypted string.
  1. 然后,您将二进制值转换回字符,并对加密字符串的每一步都执行此操作。

Problems:

问题:

Insecure for short keys. but very powerful for long keys and creates a one time pad.

短密钥不安全。但对于长键非常强大,并创建了一次性键盘。

Example code:

示例代码:

http://www.ecestudents.ul.ie/Course_Pages/Btech_ITT/Modules/ET4263/More%20Samples/CEncrypt.java.html

http://www.ecestudents.ul.ie/Course_Pages/Btech_ITT/Modules/ET4263/More%20Samples/CEncrypt.java.html

回答by Kick

Find below the class for encyption

在下面的类中找到加密

public class App 
{
    public static void main(String arg[]) 
    {

        String keys = "12";
        String codes = "hello";
        StringBuilder result = new StringBuilder();

        char[] codeList = codes.toCharArray();
        char[] keyList  = keys.toCharArray();

        int maxCount = keys.length();
        System.out.println("The length is "+maxCount);
        int i = 0;

         for (Character code : codeList) {

             int key = Character.getNumericValue(keyList[i]);

                 if(key % 2 == 0)
                    {
                      int res = code+key;
                      result.append((char)res);
                    }
                    else
                    {
                       int res = code-key;
                      result.append((char)res);
                    }
                 i++;
                 if(i==maxCount)
                 {
                    i = 0;
                 }
             }
         System.out.println("The result is "+result.toString());
        }
}