简体   繁体   English

区分大小写的Vigenere密码会产生错误的输出

[英]Case sensitive Vigenere cipher produces wrong output

I have put a lot of effort into making a cipher more robust so that the output is case sensitive. 我已经投入了大量精力使密码更加健壮,因此输出区分大小写。

Meaning, if a capital letter is in the message string, the output will have an encoded capital letter in the string at that location.. For example InpUT MesSagE turns into HrhTS WwlReyD . 意思是,如果大写字母在消息字符串中,则输出将在该位置的字符串中具有编码的大写字母。例如, InpUT MesSagE变为HrhTS WwlReyD The key used is test . 使用的关键是test

public String encrypt(String text, final String key) {
    int a_num = (int) 'a';
    int A_num = (int) 'A';
    String output = "";
    for (int i = 0, j = 0; i < text.length(); i++) {
        int cur = (int) text.charAt(i);
        // check for spaces
        if (text.charAt(i) == ' ') {
            output += " ";
        // check for lowercase
        } else if (cur >= 'a' && cur < 'z' + 26) {
            output += Character.toString((char) ((cur + key.charAt(j) - 2 * 'a') % 26 + 'a'));
            j = ++j % key.length();
        // check for uppercase between 'N' and 'Z'
        } else if (cur >= 'N' && cur < 'Z') {
            output += Character.toString((char) ((cur + key.charAt(j) - 2 * 'A') % 26 + 'N' + 7));
            j = ++j % key.length();
        // check for uppercase between 'A' and 'M'
        } else {
            output += Character.toString((char) ((cur + key.charAt(j) - 2 * 'A') % 26 + 'A' - 6));
            j = ++j % key.length();
        }
    }
    return output;
}

Currently, all lowercase letters seem to come out right, and some of the uppercase do. 目前,所有小写字母似乎都是正确的,而一些大写字母也是如此。 My problem is sometimes the uppercase is wrong, for instance symbols will be part of the output because of my incorrect math/logic. 我的问题有时是大写错误,例如由于我的数学/逻辑不正确,符号将成为输出的一部分。

The variables that I'm pretty sure are the issue are in these sections of the code: 我非常肯定的变量是代码的这些部分:

((cur + key.charAt(j) - 2 * 'A') % 26 + 'A' - 6));
public String encrypt(String text, final String key) {
    // we assume the key is all lower case
    // and only inputs are letters and space (could enhance to leave all else alone)
    int a_num = (int) 'a'; //unused?
    int A_num = (int) 'A';//unused?
    String output = "";

    for (int i = 0, j = 0; i < text.length(); i++) {
        int cur = (int) text.charAt(i);

        // check for spaces
        if (text.charAt(i) == ' ') {
            output += " ";
        }
        // check for lowercase
        else if (cur >= 'a' && cur <= 'z') {
            output += Character.toString((char) ((cur + key.charAt(j) - 2 * 'a') % 26 + 'a'));
            j = ++j % key.length();
        }
        // should work for uppercase between 'A' and 'Z'
        else {
            output += Character.toString((char) ((cur -'A' + key.charAt(j) - 'a') % 26 + 'A'));
            j = ++j % key.length();
        }
    }
    return output;
}

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

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