简体   繁体   English

凯撒密码Java

[英]Caesar Cipher Java

I'm trying to make a caeser cypher using a StringBuilder and I have 2 main problems. 我正在尝试使用StringBuilder创建caeser密码,但我有两个主要问题。 One is that my encrypt and decrypt don't cancel each other out, and my other one is that when I use the .isLetter() function it actually skips a lot of my characters, and not just ignore the white spaces. 一种是我的加密和解密不会互相抵消,另一种是我使用.isLetter()函数时,它实际上跳过了很多字符,而不仅仅是忽略了空格。 Can anyone help me? 谁能帮我?

import java.io.*;
import java.util.Scanner;

import javafx.scene.transform.ScaleBuilder;


public class FileUtilities
{
    static String tempE ; 
    static String tempD ;

    public static void main(String [] args)
    {

        try
        {

            File iFile = new File("BattlePlans.txt");
            File oFile=  new File("decodedBattlePlans.txt");

            Scanner input = new Scanner(iFile);
            PrintWriter fout = new PrintWriter(oFile);

            while(input.hasNextLine())
            {
                tempE = input.nextLine();
                //System.out.println(args[0]);
                //StringBuilder str = new StringBuilder(args[1]);

                //My encrypt funtion give me an indexoutofbounds error for some           reason and I don't know why do you see what I did wrong

                if(args[0].equals("encrypt"))
                {
                    fout.println(encrypt(tempE, Integer.parseInt(args[1])));

                }else if(args[0].equalsIgnoreCase("decrypt"))
                {
                    fout.println(decrypt(tempE, Integer.parseInt(args[1])));

                }


            }
            input.close();
            fout.close();
        }
        catch(FileNotFoundException e)
        {

        }
        // this is commented out becuase I couldn't figure out
        //how to make the InvalidCommand method or InvalidShift methods
        //and it was keeping it from running
        //do I even have to make my own, or is there something built in I can use?

        /*catch(InvalidCommand i)
        {
            System.out.println("Invalid command");
        }
        catch(InvalidShift s)
        {
            System.out.println("Invalid shift");
        }*/


    }

    public static String encrypt(String encrypt, int shift)
    {

        //first SB takes in the original text and second SB holds the changed txt
        StringBuilder str = new StringBuilder(encrypt);
        StringBuilder str2 = new StringBuilder();

        for(int i = 0; i < str.length(); i++ )
        {

            // I want to use a isLetter() if statement here to that it won't take out my space characters but when I did it made it print nothing so I deleted it
            char s = encrypt.charAt(i);

            //checks for capital and lowercase letter to see what the shift range is
            if(Character.isLetter(i))
            {
                if(s >= 'A' && s <= 'Z')
                {
                    //I went to the ILC to ask how to handle the shift and I could just add it to the end
                    //I was told that I couldn't and had to use another int and subtract the bottom of the range from the character at the index i
                    //the subtract the shift. It shifts the text to the left actually. is that okay?
                    int x = s - 'A' - shift;
                    x = x % 26;             
                    str2.append((char)(x + 'A'));

                    // add to str with .append

                }else if(s >= 'a' && s <= 'z')
                {
                    int z = s - 'a' - shift;
                    z = z % 26;
                    str2.append((char)(z + 'a'));
                }

            }
        }

        return str2.toString();
    }
    public static String decrypt(String decrypt, int shift)
    {
        //first SB takes in the original text and second SB holds the changed txt
        StringBuilder str = new StringBuilder(decrypt);
        StringBuilder str2 = new StringBuilder();
        for(int i = 0; i < decrypt.length(); i++)
        {
            char s = decrypt.charAt(i);

            //same if statement for encrypt would go here to so that it would keep the whitespaces
            if(Character.isLetter(i))
            {
                if(s >= 'A' && s <= 'Z')
                {
                    //I went to the ILC to ask how to handle the shift and I could just add it to the end
                    //I was told that I couldn't and had to use another int and subtract the bottom of the range from the character at the index i
                    //the subtract the shift. It shifts the text to the left actually. is that okay?
                    int x = s + 'A' + shift;
                    if(x < 0)
                        x = x + 26;
                    str2.append((char)(x + 'A'));

                    // add to str with .append

                }else if(s >= 'a' && s <= 'z')
                {
                    int z = s + 'a' + shift;

                    if(z < 0)
                        z = z + 26;
                    z = z % 26;
                    str2.append((char)(z + 'a'));

                }

            }
        }

        //decrypt is encrypt backwards
        return str2.toString();
    }
}

Few hints: 一些提示:

  1. Test Character.isLetter(s) , not Character.isLetter(i) . 测试Character.isLetter(s) ,而不是Character.isLetter(i) Currently, you are testing the index of the symbol, not the symbol itself. 当前,您正在测试符号的索引,而不是符号本身。

  2. Make sure that what you are taking mod 26 is positive , add +26 to x before computing x = x % 26 . 确保您采用的是mod 26 ,在计算x = x % 26之前向x添加+26

  3. Why do you have two separate encrypt and decrypt methods? 为什么会有两种单独的encryptdecrypt方法? Eliminate one of them. 消除其中之一。

  4. isLetter is unnecessary, you check >= 'A' and <= 'Z' anyway. isLetter是不必要的, isLetter检查>= 'A'<= 'Z'

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

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