Caesar Cipher:密文输出不正确

时间:2017-05-11 06:50:32

标签: java caesar-cipher

在我的代码中,我从.txt文件中输入纯文本输入,该文件只包含大写和小写字母,而不包含其他字符。以下是我的源代码。

源代码:

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Random;

public class Cipher {

    private String file_name;
    private int shift_value;

    public Cipher(){
        file_name = "/Users/SubrataMohanty/IdeaProjects/CaesarCipher/src/cipher_text.txt";
    }
    public String getFileName(){
        return (file_name);
    }

    /*Encode method*/
    public char [] encodePlainText(char input_text_array[]) {
        char [] cipher_text_array = new char[input_text_array.length];
        int ascii_value, resultant, temp_value;

        Random random = new Random();

        shift_value = random.nextInt(100) + 1;

        for(int i = 0 ; i < input_text_array.length ; i++){
            ascii_value = (int)input_text_array[i];

            if (ascii_value <= 90) {
                temp_value = ascii_value - 65;
                resultant = (temp_value + shift_value) % 26 + 65;

                cipher_text_array[i] = (char)resultant;
            }
            else if (ascii_value >= 97 && ascii_value <= 122){
                temp_value = ascii_value - 97;
                resultant = (temp_value + shift_value) % 26 + 97;

                cipher_text_array[i] = (char)resultant;
            }
        }

        System.out.println(shift_value);
        return (cipher_text_array);
    }

    /*Decode method*/
    public char [] decodeCipherText(char cipher_text_array[]) {
        char [] plain_text_array = new char[cipher_text_array.length];
        int ascii_value, resultant, temp_value;

        for(int i = 0 ; i < cipher_text_array.length ; i++){
            ascii_value = (int)cipher_text_array[i];

            if(ascii_value <= 90){
                temp_value = ascii_value - 65;
                resultant = (temp_value - shift_value) % 26 + 65;

                cipher_text_array[i] = (char)plain_text_array[i];
            }
            else if (ascii_value >= 97 && ascii_value <= 122){
                temp_value = ascii_value - 97;
                resultant = (temp_value - shift_value) % 26 + 97;

                cipher_text_array[i] = (char)resultant;
            }

        }

        System.out.println(shift_value);
        return (plain_text_array);
    }
    public static void main(String[] args) throws IOException{

        BufferedReader br = null;
        FileReader fr = null;

        Cipher cipher_1 = new Cipher();

        br = new BufferedReader(new FileReader(cipher_1.getFileName()));

        String current_line;
        String input_text = null;

        while ((current_line = br.readLine()) != null) input_text = current_line;

        char [] input_char_array = input_text.toCharArray();
        char [] cipher_char_array = cipher_1.encodePlainText(input_char_array);

        char [] plain_text_array = cipher_1.decodeCipherText(cipher_char_array);

        System.out.println("Plain text input : ");
        System.out.println(input_char_array);

        System.out.println("Resultant cipher text");
        System.out.println(cipher_char_array);

        System.out.println("Decoded text");
        System.out.println(plain_text_array);

        br.close();
        }
    }

输出:

Cipher
Connected to the target VM, address: '127.0.0.1:49797', transport: 'socket'
65
65
Plain text input : 
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Resultant cipher text
aHIJKLMNOPQRSTUVWXYZ[\]^_`
Decoded text

Disconnected from the target VM, address: '127.0.0.1:49797', transport: 'socket'

Process finished with exit code 0

当我使用非常简单的旋转算法时,我似乎不明白encodePlainText()方法如何输出上面得到的密文。

0 个答案:

没有答案