Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Decryption problem

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Decryption problem

    Nevermind. It turns out it worked. The only thing wrong with it is my lack of patience.
    Last edited by hjen; October 20th, 2010 at 09:28 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Decryption problem

    Quote Originally Posted by hjen View Post
    So I've made a program to decrypt a textfile without user interaction. This is done by counting the letters in the input file and using the fact that the most common letter is "e" (not fool-proof of course, but usually works for large txt files). Everything seems to work except for the decryption itself. It is just a Caesar Cipher, so the code for it should be alright. However, it does not seem to return the necessary string. I have spent a lot of time trying to figure out where the mistake is but, alas, without success. By now, however, I am pretty sure the problem is to be found within the last method (i.e. the one called "caesar"). I would love for someone to have a look at it and tell me where I've gone wrong. The code:

    import java.util.*;
    import java.io.*;
     
     
    public class Caesardec{	// beginning of class
    	public static void main (String [] args) { // beginning of main
    	File infile;
    	PrintStream output = null;
     
    	if (args.length > 1){ // beginning of if
    		try{ // beginning of try
    		output = new PrintStream(new File(args [1]));
    		} // end of try
    		catch(Exception e) { // beginning of catch
    System.out.println("nah");
    		} // end of catch
     	} // end of if
     
    	infile = new File( args[0] );
    	String input = "";
    	try { // beginning of try
    	 BufferedReader br = new BufferedReader(new FileReader(infile));
             StringBuilder sb = new StringBuilder();
             String line = br.readLine();
         	   while ( line != null){ // beginning of while
            	  sb.append(line + "\n");
           		  line = br.readLine(); 
              } // end of while
    	 br.close();
          	input = sb.toString();
        	} // end of try
    	catch (Exception e) { // beginning of catch
          e.printStackTrace();
          System.err.println(infile);
          System.err.println(e);
    	} // end of catch
     
    	int key = deknogle(input);
     
    		if( args.length == 1 ){ // beginning of if
    			System.out.println( caesar( input, key ));
    		} // end of if
    		else { // beginning of else
    		output.println( caesar( input, key));
    		output.close();
    		System.out.println("Writing to output-file.");
    		}	// end of else		
     
     
    	} // end of method
     
    	public static int deknogle(String input){ // beginning of method
    			String lower = input.toLowerCase();
    		HashMap lettercount = new HashMap();
    		for (int i = 0; i < lower.length(); i++) { // beginning of for
    			if(Character.isLetter(lower.charAt(i))){ // beginning of if
                	 		 char c = lower.charAt(i);   
    				if(lettercount.get(c)!=null){ // beginning of if
    				lettercount.put(c, (Integer)(lettercount.get(c))+1);
    				} // end of if
    				else{ // beginning of else
    				lettercount.put(c, 1);
    				} // end of else
    			} // end of if
     
     		} // end of for
    		HashMap map = new LinkedHashMap();
    		List nogler = new ArrayList(lettercount.keySet());
    		List vardier = new ArrayList(lettercount.values());
    		TreeSet sorteret = new TreeSet(vardier);
    		Object[] sort = sorteret.toArray();
    		int size = sort.length;
     
    		for (int i=0; i<size; i++) { // beginning of for
    		   map.put
    		      (nogler.get(vardier.indexOf(sort[i])),
    		       sort[i]);
    		} // end of for
    		Character[] keys = new Character[map.size()]; 
    				map.keySet().toArray(keys);
    		int nogl = keys[25]-'a';
    		int tildek = nogl - 4;      //4 corresponding to the letter "e".
    		int enkey = 26-tildek;
    		tildek = enkey;	
    		return tildek;
    	}	// end of method
     
    		public static String caesar(String input, int key) { // beginning of method
    		String decrypt = "";
    		for (int i = 0; i < input.length(); i++) { // beginning of for
    			char d = 0;
    			int c = input.charAt(i);
    			if (97 <= c && c <= 122){ // beginning of if
    				d = (char) ((c+key-97)%26+97);
     
    			}  // end of if
    else{ // beginning of else
    			if (65 <= c && c <= 90){
    			d = (char) ((c+key-65)%26+65);
    			} // end of if
    			else { // beginning of else
    			d = (char) c;
     
    			} // end of else} // end of else
    			decrypt = decrypt + d;
    		} // end of for
    		return decrypt;
     
    	} // end of method
     
    } // end of class
    As I understand it, the method caesar does not return the decrypt string i create, but simply the empty string as first defined. If this is indeed the problem, do any of you guys have an idea as to how I can avoid it? Any help would be greatly appreciated.
    Well, it looks like you're adding a char to a String. Try type casting it to a String or something.

    It's because you're adding a char to a String that's causing the problem I think.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Decryption problem

    I assume you mean by defining a new string as Character.toString(d). That doesn't work either though. If you mean something different, then please elaborate. I have roughly one week of programming experience, so I apologize if I seem a little dim on the subject.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Decryption problem

    Quote Originally Posted by hjen View Post
    I assume you mean by defining a new string as Character.toString(d). That doesn't work either though. If you mean something different, then please elaborate. I have roughly one week of programming experience, so I apologize if I seem a little dim on the subject.
    Well, I've found that it can work the way you did it. However, maybe something is being defined inside one of your loops that should be defined outside, as its value is getting lost before you return the final value, hence returning "";

    Well, char d may be getting lost. Not sure.

    Anyway, I thought the syntax would be

    char d = '0';

    Otherwise you're setting it to an int. I admit I've never user BufferWriters and readers before.
    Last edited by javapenguin; October 19th, 2010 at 07:28 PM.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Decryption problem

    int c = input.charAt(i);

    Probably should be

    int c = (int) input.charAt(i);

    Otherwise you're setting an int to a char value.

    And I don't mean that it'll do that. It can't, but you're telling it to anyway.

    By any chance was the encrypted value you passed it empty?
    Last edited by javapenguin; October 19th, 2010 at 07:36 PM.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Decryption problem

    d = (char) ((c+key-97)%26+97);


    Will add c to key and then subtract 97.

    Will then divide value in parenthesis by 26 and add 97 to the remainder of that division and then make the whole thing into a char.

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Decryption problem

    If I add the line "System.out.println(decrypt);" right after the line "decrypt = decrypt + d" it actually prints out the right thing, but it does it over and over again in sort of an endless loop. Either way it's correct, except for the fact that it wont return the value. It seems like the return statement is unaware of what's going on inside the 'for' statement. And of course I can't move the return statement up, because then it wont compile because of "missing return statement".