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 16 of 16

Thread: Encrypt Decrypt

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Encrypt Decrypt

    The method Encrypt should for example
    input:
    ab
    cd

    output:acbd

    the method decrypt should take same input and output:abcd

    what could be wrong:

    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	int y = dimension;
     
    	char[][] box = new char [x][y];// This is the box: a 2 dimensional array
     
    	int counter = 0;
     
    	for(int i = 0; i<x; i++){
     
    		for(int j = 0; j<y; j++){
     
    		if(counter<s.length()){	
     
               box[i][j]= s.charAt(counter);
     
              counter++;         
    		}		
    	}
    		}
    	java.util.Arrays.deepToString(box);
     
    	 StringBuilder sb = new StringBuilder();
     
    	for(int j =0; j<y; j++){
     
    			for(int i = 0; i<x; i++){
     
    				sb.append(box[i][j]);
    			}			
    }
     
    return (sb.toString());
     
    	}
     
     
     
    	   public static String decrypt(String cypherText) {
     
     
    	String s = cypherText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	int y = dimension;
     
    	char[][] box = new char [x][y];// This is the box: a 2 dimensional array
     
    	int counter = 0;
     
    	for(int i = 0; i<x; i++){
     
    		for(int j = 0; j<y; j++){
     
    		if(counter<s.length()){	
     
               box[i][j]= s.charAt(counter);
     
              counter++;         
    		}		
    	}
    		}
    	java.util.Arrays.deepToString(box);
     
    	 StringBuilder sb = new StringBuilder();
     
    	for(int i = 0; i<x; i++){
     
    			for(int j = 0; j<y; j++){
     
    				sb.append(box[i][j]);
    			}			
    }
     
    return (sb.toString());
     
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypt Decrypt

    input:
    ab
    cd
    The input does not make sense. It looks like the contents of a 2 dim array. Shouldn't the input be a String?
    What are the steps the decrypt method should do with the input String it receives?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypt Decrypt

    Sorry, the input is abcd

    But I have to put it into a caesar box before encrypt/decrypt

    decrypt should put it into a caesar box like

    a c
    b d

    Fixed:

    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	int y = dimension;
     
    	char[][] box = new char [x][y];// This is the box: a 2 dimensional array
     
    	int counter = 0;
     
    	for(int i = 0; i<x; i++){
     
    		for(int j = 0; j<y; j++){
     
    		if(counter<s.length()){	
     
               box[i][j]= s.charAt(counter);
     
              counter++;         
    		}		
    	}
    		}
    	java.util.Arrays.deepToString(box);
     
    	 StringBuilder sb = new StringBuilder();
     
    	for(int j =0; j<y; j++){
     
    			for(int i = 0; i<x; i++){
     
    				sb.append(box[i][j]);
    			}			
    }
     
    return (sb.toString());
     
    	}
     
     
     
    	   public static String decrypt(String cypherText) {
     
     
    	String s = cypherText.toLowerCase();// Taking out upper cases
     
    	s = s.replaceAll("\\s","");//Taking out blank spaces
     
    	float squareRoot = (float) Math.sqrt(s.length());
     
    	int dimension = (int) Math.ceil(squareRoot);
     
    	int x = dimension;
     
    	int y = dimension;
     
    	char[][] box = new char [x][y];// This is the box: a 2 dimensional array
     
    	int counter = 0;
     
    	for(int j = 0; j<y; j++){
     
    		for(int i = 0; i<x; i++){
     
    		if(counter<s.length()){	
     
               box[i][j]= s.charAt(counter);
     
              counter++;         
    		}		
    	}
    		}
    	java.util.Arrays.deepToString(box);
     
    	 StringBuilder sb = new StringBuilder();
     
    	for(int i = 0; i<x; i++){
     
    			for(int j = 0; j<y; j++){
     
    				sb.append(box[i][j]);
    			}			
    }
     
    return (sb.toString());
     
    	}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypt Decrypt

    Fixed:
    Does that mean the code is now working?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypt Decrypt

    What could be wrong now?

    --- Update ---

    No, sorry

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypt Decrypt

    What are the steps the decrypt() method must do with the String it gets as an arg?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypt Decrypt

    It should put the string character by character in a ceaser box from top to bottom left to right, so if the input is abcd the caesar box should be:

    a c
    b d

    And then it should decrypt the caesar box and return the string: acbd (reading from left to right, top to bottom)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypt Decrypt

    (reading from left to right, top to bottom)
    Are you sure that is the way? Shouldn't it be top to bottom, left to right?
    first a
    then down to b
    then right to c
    then down to d
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypt Decrypt

    Yes, I am. What you said would be encrypt. This examples are given in the assignment:

    Sample Input/Output
    Case A
    Input:
    java CaesarBox -encrypt abcd
    Logic:
    a b
    c d
    Output:
    acbd
    Case B
    Input:
    java CaesarBox -encrypt abcdefghij
    Logic:
    a b c d
    e f g h
    i j

    Output:
    aeibfjcgdh
    Case C
    Input:
    java CaesarBox -decrypt acbd
    a b
    c d
    Output:
    abcd

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypt Decrypt

    What does the decrypt method do with the String passed to it? Given the String: "acbd", what steps should it do to decrypt the String?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypt Decrypt

    I thought I had answered it. Sorry I wasn't clear. Maybe I didn't get it as well.
    Here are the instructions:

    Write a Java program to encrypt a string of text using a Caesar Square: the string should be written along the rows of a square that is as small as possible, and then the encrypted string is read down the columns like so:

    "A long time ago, in a galaxy far, far away" is first processed to remove case and space, to get alongtimeago,inagalaxyfar,faraway (33 characters long) and is then laid out on a square grid:

    a l o n g t
    i m e a g o
    , i n a g a
    l a x y f a
    r , f a r a
    w a y
    The grid is 6 x 6 as 36 is the closest square number that is no less than the number of characters in the message.

    Reading form top-to-bottom we get ai,lrwlmia,aoenxfynaayagggfrtoaaa - which is of course garbage ☺
    You also have to decrypt such strings which may contain white space that needs to be removed ...

    Learning Outcomes
    Simple string manipulation
    Creating/Populating/traversing an array
    FAQ
    Will I have to put spaces and capitalisations back in? No, you have to do neither of those things. It would be a much harder Task and beyond the scope of this course to do that.
    How will my program be tested? We run the programs as though you would on the command-line, just like this:java CaesarBox -encrypt abcd or java CaesarBox ALongTimeAgo
    How can I put spaces in the command-line input? If you want a String argument (which would be args[1] if you use the standard name for the arguments to main) then you can do this:
    java CaesarBox -encrypt "One Flew Over A Cuckoo's Nest"
    with the double quotes, and then the whole phrase "One Flew Over A Cuckoo's Nest" is made into a single String.
    Do I have to leave the skeleton code as it is? Well you have to add to the skeleton code: the main method is given for you and you don't need to alter it, but you do need to fill in the other two methods. If you don't know what a method is yet then don't worry, because we'll cover those in Week 5. As a rough guide if you want to get started anyway, a method is like a tool in a program that you use by passing it information like a String, and then using the result that is returned.
    Is encrypting the same as decrypting? No, it isn't, but they are very similar. You will have to write different methods for each function, as indicated in the skeleton code.
    I saw in the examples that some of the input strings don't fit neatly into a square: there are spaces left over. What should I do? Do I need to include those spaces in the output? No, you shouldn't include spaces in your output.
    What size square should I use? You have to work out the size of square that is as small as possible but which fits the entire string.
    When decrypting, where do I put in the gaps? The output should have no gaps but during the decription process you might need to put gaps in your grid as "placeholders".
    Sample Input/Output
    Case A
    Input:
    java CaesarBox -encrypt abcd
    Logic:
    a b
    c d
    Output:
    acbd
    Case B
    Input:
    java CaesarBox -encrypt abcdefghij
    Logic:
    a b c d
    e f g h
    i j

    Output:
    aeibfjcgdh
    Case C
    Input:
    java CaesarBox -decrypt acbd
    a b
    c d
    Output:
    abcd

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypt Decrypt

    Too much. I'm asking what the decrypt method should do with the String: "acbd"? I was looking for some simple steps like the following:
    1) create an array
    2) get a new String from the array

    How does it take the letters in the String and create the array?
    How does it take the letters from the array and create the String?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypt Decrypt

    1 - take out upper cases and spaces from string input
    2 - string should be written along the rows of a caesar square
    3 - case decryp it should be written top to bottom left to right
    4-case decrypt should read a string from the square caeser left to right top to bottom

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypt Decrypt

    Given a String "acbd" where do the letters go in the array?
    When the array is finished, how are the letters taken out of the array to make the String?

    written top to bottom left to right
    would give the array:
    ab
    cd
    left to right top to bottom
    would give: "abcd"

    Does the code do that? Print out the contents of the array to see if it is correctly created.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Encrypt Decrypt

    The first two questions have no answers because they are exactly the assignment. I suppose I have to use loops.

    The code does that for some inputs but not for others.

    I tried to print box[i][j] but it says i and j are undefined.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Encrypt Decrypt

    You need to post more code than one statement for that error message. It needs to show where i and j are defined.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Encrypt a text message
    By dianac in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 12th, 2013, 09:32 PM
  2. Replies: 2
    Last Post: June 4th, 2012, 11:40 PM
  3. [SOLVED] Unable to encrypt using AES 256bits
    By fuweng in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 5th, 2011, 07:44 AM
  4. How do i encrypt my password at command line
    By AHOT in forum Java Theory & Questions
    Replies: 5
    Last Post: October 13th, 2010, 03:13 PM
  5. Need Help for encrypt/decrypt.
    By superdhebz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 17th, 2010, 12:17 PM