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

Thread: encrypting and decrypting a string

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default encrypting and decrypting a string

    -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 ...


    I am struggling to understand how the shifting of elements works. I know decrypting is reading from left to right and encrypting is reading from top to bottom but I can't think of a way to code it. Also I considered that the question has to do with two dimensional arrays but then I didn't know how to determine the amount of rows and columns I needed and hence didn't know what to make each dimension.

    So far I have just changed the cypherText(string) into an array called decryptArray, and each element of this array contains a letter within the string. I was up to shifting the elements and I thought of using a FOR loop to do that but there was not much point until I understand how the shifting works. To be honest, I am not even sure if I am on the right track.

    public class CaesarBox 
    {
     
    	public static void main(String[] args) 
    	{
     
    	    if(args[0].equals("-decrypt"))
    	    {
    	    	System.out.println(decrypt(args[1]));
    	    }
     
    	}
     
    	public static char decrypt (String cypherText) 
    	{
    		char example = 'a'+'b'; //ignore this
    		int length = cypherText.length(); //determining the length of the string
    		char [] decryptArray  = new char [length]; //created an array using the length of what was entered 
    		for(int i=0; i < length; i++)
    		{
    			decryptArray[i]=cypherText.charAt(i); 
                            //making each element of what as typed in to equal an element in the array
    			System.out.print(decryptArray[i]);
    		}
     
     
    		return example; //just put this in as I am not up to this part and so I can compile the program
    	}
    }
    Last edited by mist4lyf; April 8th, 2013 at 04:13 AM. Reason: simplified my previous posts


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: encrypting and decrypting a string

    how the shifting of elements works
    Can you give an example of "shifting"?

    how to determine the amount of rows and columns I needed
    Is that the square root of the minimum square number? The Math class has some methods that could help find that.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: encrypting and decrypting a string

    I know how to determine the amounts of rows and columns now.

    What I mean by shifting the elements, is so that if the string was decrypted"abcd"
    a b
    c d
    We read the elements left to right, row by row. If I wanted to encrypt the string to make it "acbd", reading it from top to bottom, column by column I would have to shift, for example myArray[1][0]= c and putting b into that element and myArray[0][1] will contain c. Doing this now makes the array print out acbd.

    I hope this makes sense.. My logic seems confusing!

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problems with my FOR loop

    Hi guys,

    I have for example a 2-D array with a=[0,0] b=[0,1], c=[1,0] and d=[1,1]
    a b
    c d

    So for my array I am reading in the letters from left to right, row by row and now I wanted to do the opposite so I would read a,c,b,d

    I know a remains as [0,0] and d remains as [1,1].
    The problem I am having is, is switching the b and c so that b=[1,0] and c=[0,1].
    When I compiled it, it became accd instead of acbd.
    I realised that when c is being changed to [0,1] it changes the contents of that element and when I want to change b to c the problem arises, as [0,1] no longer contains b, but c now. I can't think of a way so that it keeps the original contents so I can switch b to [1,0].


     
    for(int i= 0; i < encryptArray.length; i++)
    {
          for(int j=0; j < encryptArray.length; j++)
          {
                encryptArray[i][j]=encryptArray[j][i];
                System.out.print(encryptArray[i][j]);
          }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: encrypting and decrypting a string

    I don't see how reading the array by rows or reading it by columns is doing any shifting of its contents. The positions of the letters in the array doesn't change.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problems with my FOR loop

    I think you need to copy the array to a temporary array, then build your new, transposed array from the copy.

    HTH

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problems with my FOR loop

    Okay, Ill look into it and see how I go.

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: encrypting and decrypting a string

    a is [0][0], b is [0][1], c is [1][0], d is [1][1]

    and I wanted to make it so it becomes:

    a is [0][0], c is [0][1], b is [1][0], d is [1][1]

    I guess by changing the position of the letters and not shifting.. hmm
    Last edited by mist4lyf; April 9th, 2013 at 06:34 AM. Reason: deleting post contents due to duplication

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: encrypting and decrypting a string

    Why change the array? Why not change how the elements are read from the array?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problems with my FOR loop

    Is this the same problem: http://www.javaprogrammingforums.com...tml#post107540

    The Threads have been merged
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problems with my FOR loop

    ..

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: encrypting and decrypting a string

    Is there some confusion on what the program is supposed to do?
    For the encrypt part: What does the program read in, what does it do with what was read and what does it write out?
    For the decrypt part, does the program take the output from the encrypt step and convert it to what the encrypt part read in?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: encrypting and decrypting a string

    Okay so the user would type -encrypt and -decrypt and that would be determined as args[0] and whatever the user types as args[1] is sent to that particular method.
    Examples I need to test are -encrypt abcd and -decrypt acbd

    public static void main(String[] args) 
    {
            if(args[0].equals("-encrypt")) 
            {
            	// System.out.println(encrypt(args[1]));
                encrypt(args[1]); //calls the encrypt method and sends the contents of args[1] to it
            }
     
            else if(args[0].equals("-decrypt")) //if string within args[0] is equal to -decrypt, then the statements inside are executed
            {
                //System.out.println(decrypt(args[1]));
                decrypt(args[1]); //calls the decrypt method and sends the contents of args[1] to it
            }
             */
    }


    --- Update ---

    I populated my array so that the first character of the string "a" would be the [0][0] of the array and etc.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: encrypting and decrypting a string

    What problems are you having?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: encrypting and decrypting a string

    The point of each method is changing the inputted string so that the string is either encrypted or decrypted. If I were inside the encrypt method and I read in abcd, how would I make the output be acbd? I was trying to change the contents of the array elements to do that but then you suggested to just change how the array elements are read and therefore getting an output. I dont understand how to do that.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: encrypting and decrypting a string

    Work on one method at a time.
    In the encrypt method, work on putting the input into the array. When that step works, then move to the next step.

    Use this statement for printing out the contents of the array for debugging:
    System.out.println("an ID "+ java.util.Arrays.deepToString(theArrayName));
    Change "an ID " to describe what is being printed.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    My Mood
    Angry
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: encrypting and decrypting a string

    Yes I have put the input into a 2-D array but to go about printing the elements of the array different to how it was entered into the array, would I be using some sort of loop?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: encrypting and decrypting a string

    I have put the input into a 2-D array
    If you have the code to store the String's characters into an array row by row,
    now work on the code to take out the characters from the array column by column.
    Use a StringBuilder to save the characters and when done, create a String from its contents.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    mist4lyf (April 13th, 2013)

Similar Threads

  1. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  2. Replies: 0
    Last Post: March 28th, 2013, 06:27 AM
  3. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  4. Problem decrypting with AES
    By wmd in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 17th, 2012, 09:41 AM
  5. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM