简体   繁体   English

替代加密

[英]Substitution encryption

I'm trying to make a substitution encryption program in java, but I'm having trouble creating the code and I'm wondering if you have any tips. 我正在尝试用Java编写替代加密程序,但是在创建代码时遇到了麻烦,我想知道您是否有任何提示。 My idea was to make the alphabet into numbers and then compare it to the plainText and then make a new string to send back. 我的想法是将字母转换为数字,然后将其与plainText进行比较,然后制作一个新字符串以发送回去。

This is my code: 这是我的代码:

package ComputerSecurity;

public class Substitution {

    String str = "abcdefghijklmnopqrstuvwxyz";

    public String encrypt(int key, String plainText){
        StringBuilder temp = new StringBuilder();
        for(int i=0; i< plainText.length(); i++){
            int position = plainText.charAt(i);

            temp.append(str.charAt(plainText.indexOf(position)+key));
        }
        return temp.toString();
    }
}

key=3 but it's sent in by the main method plainText is "hello world". key=3但是它是通过主要方法发送的plainText是“ hello world”。

I think you try to implement a Caesar Cipher. 我认为您尝试实施凯撒密码。

plainText.charAt(i)

returns the actual current character, not a position. 返回实际的当前字符,而不是位置。 You can use the character to find the position in the alphabet: 您可以使用该字符在字母表中找到位置:

char c = plainText.charAt(i)
int aPos = str.indexOf(c);

Then you can move this position and get the encrypted letter, but you need to wrap around. 然后,您可以移动该位置并获得加密的字母,但需要重新包装。 This is usually done with a modulo operation: 这通常通过模运算来完成:

int encPos = aPos;
if (key >= 0) {
    encPos = (aPos + key) % str.length();
} else {
    // the key may be bigger than the alphabet length
    encPos = (str.length() + ((key + aPos) % str.length())) % str.length();
}
char ec = str.indexOf(encPos);

For every char where aPos is -1, you can skip the encryption. 对于aPos为-1的每个字符,您可以跳过加密。 If you do this, then you can also encrypt spaces and other non-alphabet characters. 如果执行此操作,则还可以加密空格和其他非字母字符。

The problem with your idea to return a "number string" is that you don't know where one character number ends and where the next one begins. 返回“数字字符串”的想法的问题是,您不知道一个数字在哪里结束,下一个数字在哪里开始。 So, if you want that, you will have to add delimiters and this bloats your ciphertext unnecessarily. 因此,如果需要,您将必须添加定界符,这会不必要地使密文膨胀。

There is 3 problems: 有3个问题:

  • You use str . 您使用str It will limit the alphabet you can use. 它将限制您可以使用的字母。 In your case uppercase H and W and the space between the 2 word will give the same output since they're not in str . 在您的情况下,大写的H和W以及两个单词之间的空格将提供相同的输出,因为它们不在str
  • Also, if you try to encrypt xyz with a key > 3 you'll end up trying to get a char in a position that doesn't exist in str . 另外,如果尝试使用大于3的密钥对xyz进行加密,则最终将试图使char处于str中不存在的位置。
  • Finally, if you don't use a separator in our ouput it will be realy difficult to differentiate some case like 'ak' and 'ka' since both will output '111' with a key of 0. 最后,如果您在输出中不使用分隔符,将很难区分诸如“ ak”和“ ka”之类的情况,因为两者都将输出键值为0的“ 111”。

My suggestion would be tu use the int value of the char . 我的建议是使用charint值。 You can get it with plainText.charAt(i) . 您可以使用plainText.charAt(i)来获取它。
For "abc" it will return 97, 98 and 99 since the code for a is 97. If you substract 96, you'll get 1 for a , 2 for b ... “ABC”时它会返回97,98和99,因为在代码a是97。如果你96。减去,您将获得1 a ,2 b ...
You'll just have to add your key to it. 您只需要在其中添加密钥即可。

package ComputerSecurity;
public class Substitution {

    public String encrypt(int key, String plainText){
        StringBuilder temp = new StringBuilder();
        for(int i=0; i< plainText.length(); i++){
            int val = plainText.charAt(i) - 96;
            temp.append((val+key) + ";");
        }
        return temp.toString();
    }

encrypt(3, "Hello World") will output -21,8,15,15,18,-61,26,18,21,15,7, . -21,8,15,15,18,-61,26,18,21,15,7, encrypt(3, "Hello World")将输出-21,8,15,15,18,-61,26,18,21,15,7,
If you want to get 11,8,15,15,18,-61,26,18,21,15,7, put the plainText toLowerCase() . 如果要获取11,8,15,15,18,-61,26,18,21,15,7,请将plainText放入LowerCase toLowerCase()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM