Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用密码数组加密字符串_Java_Encryption - Fatal编程技术网

Java 使用密码数组加密字符串

Java 使用密码数组加密字符串,java,encryption,Java,Encryption,我的任务是编写一个程序,使用随机生成的普通字母密码将纯文本转换为密文。 密码数组对字母表中的每个字母都有不同的特定字母。e、 g.字母“a”对应于“x”,“b”对应于“j”,依此类推。 纯文本和密文都存储为数组。 我能够生成cypher数组,但似乎无法使用纯文本和密文数组创建加密数组。 当我运行这段代码时,它会生成作为加密结果的纯文本代码 提前感谢你们的帮助 以下是我迄今为止的代码: String plaintextString = new String( plaintext ) ;

我的任务是编写一个程序,使用随机生成的普通字母密码将纯文本转换为密文。 密码数组对字母表中的每个字母都有不同的特定字母。e、 g.字母“a”对应于“x”,“b”对应于“j”,依此类推。 纯文本和密文都存储为数组。 我能够生成cypher数组,但似乎无法使用纯文本和密文数组创建加密数组。 当我运行这段代码时,它会生成作为加密结果的纯文本代码

提前感谢你们的帮助

以下是我迄今为止的代码:

    String plaintextString = new String( plaintext ) ;
    plaintextString = plaintextString.toLowerCase();
    plaintext = plaintextString.toCharArray() ;

    char i =0 ;
    for (int index=0; index<plaintext.length; index++)
    {
        i = plaintext[index] ;
        if ( i == ' ')                      //The space character 
        {                                   //doesn't get encrypted
            plaintext[index] = ' ' ;
        }
        for (int index2=0; index2<alphabet.length; index2++)
        {
            if ( i == alphabet[index2])    //Alphabet array already generated
            {
                encrypt[index]= cipher[index2] ;                                    
            }
        }


    }
String纯文本字符串=新字符串(纯文本);
plaintextString=plaintextString.toLowerCase();
明文=明文字符串。toCharArray();
字符i=0;
对于(int index=0;index不确定如何生成(或获取)“cipher”数组,但它应该简单地将每个ASCII字符映射到不同的ASCII字符。根据您问题中的代码,这应该包括将空格字符(实际上,任何其他非字母字符)映射到自身

如果不是这样,那么我建议您首先从您拥有的任何“映射对象”构造这样的“密码”数组。一旦确定了这一点,您的方法应该非常简单:

  • 输入:字符数组“明文”和“密码”

  • 输出:字符数组“密文”

以及守则:

for (int index=0; index<plaintext.length; index++)
    ciphertext[index] = cipher[plaintext[index]];

for(int index=0;index?你能发布你所有的代码吗,或者你能保证bug在这段代码中吗?bug肯定在这段代码中。出于某种原因,程序不会在char[]encrypt数组中存储任何我想要的值。