Jump to content

Basic Encryption. Need help with Chars!

- - - - -

  • Please log in to reply
2 replies to this topic

#1
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
I need to create a program that does some basic encryption.

Just read the first few, short paragraphs for the details: http://www.csee.usf....aesarCipher.pdf

I can't get the part where I need to make sure that ONLY letters are changed. Symbols, punctuation and basically anything that is not a letter needs to be kept the same.

Look at my "else if" statement in the code below:
import java.io.*;


public class main {


    public static void main(String args[]) {

    	//get message and store it into "message"

    	String message = getMessage("text.txt");

        String encrypt = "";

        int key = 1;

    	

    	//System.out.println("Test to see if file is being read)" + message); //test to see if file is being read properly

        //Yes, file is read.

        

	    	//We will loop thru each char and add the key to it. 

	    	for(int x = 0; x < message.length(); x++){

	    	//Create a temporary char so we can add the key to it. 

	    	int temp;

	    	

	    	//if there is a space, then add a space to the the encrypted message

	    	if(message.charAt(x) == (' ')){

	    		encrypt += " ";

	    	}

	    	

	    	//if there is a line break, add a line break

	    	else if(message.charAt(x) == (char) 10){

	    		encrypt += "\n";

	    	}

	    	//if the character is anything BUT a letter ex: !@#$%, don't encrypt it. 

//	    	else if(!(message.charAt(x).isLetter(a-z))){

//	    		

//	    	}

	    	else{ //add key to char at X and store new char in encrypted message

	    		 temp = (int) (message.charAt(x) +  key);

	    		 encrypt += (char) temp;

	    	}    	

	    	

	    	}

    	

    	System.out.println("Encrypted Message: " + encrypt);

    	System.out.println("Original Message(test): " + message);


    	

    }

    

    public static String getMessage(String location) {

        String data = "";

        try {

            FileReader fr = new FileReader(location);  // Opens the file

            BufferedReader br = new BufferedReader(fr);  //Sets up a buffered stream so that large files can be read

            

            String line = null;

            while((line = br.readLine())!= null && !line.equalsIgnoreCase("stop") ){  // Reads each line while 'line' is not null

                data += line;

                data += (char) 10;  // Starts a new line, look at ASCII chart to see what 10 is

            }

            

            br.close();  // closes the file so other programs can read it

        } catch (Exception e) {

            e.printStackTrace();  // if the files does not exist or if the file is locked then it will let you know by giving an error

        }

        

        return data;

    }

}


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Loop trough the characters of your String using toCharArray();

for (char c : string.toCharArray()){            

}


And with chars you can do the following:

if(c>='a' && c<='z')

OR

if (String.valueOf(c).matches("[a-z]"))



if(!(message.charAt(x).isLetter(a-z)))

Which you had was wrong. First of all isLetter is a static method of Character, so it should be used like:

if( Character.isLetter(message.charAt(x)) )

BUT, if your JVM thinks that the default locale is, for example, french, it will also say é ç è are valid letters.
So in this case, it's not the best option to use that method to check for letters.


You shouldn't need these by the way:

if(message.charAt(x) == (' ')){

   encrypt += " ";

} else if(message.charAt(x) == (char) 10){

   encrypt += "\n";

}



#3
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Thanks man. Really appreciate it. +rep.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users