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

Thread: Encryption

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Encryption

    Ok, I am having an issue with the encryption program I am trying to write.
    Basically, I need to have the user enter a string and encrypt it.
    I have a variable for the string, an array for the alphabet, and an array for the alphabet reversed.
    What I am trying to do is take the string the user entered, compare it to the alphabet array and get a subscript for which letter alphabet array matches the letter in the string. Then, I need to take that subscript of the alphabet array, and compare it to the reversed alphabet array, and assign new letters in the reversed alphabet array to the string, thus "encrypting it"
    Essentially, user enters ABC and it becomes ZYX.
    Problem is, I have been racking my brain and trying different things, and I cannot fathom a way to do this.
    Can anyone help or offer a suggestion?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Encryption

    Can you show us what you've tried?

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Encryption

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import java.util.Random;
     
    public class Encrypt extends JFrame
    {
        private String dCountry;    //string to hold the destination country
        private String encryptText; //string to hold text for encryption
        private String encryptedText;
        private char temp;
        private char[] Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
        private char[] Encryption = {'Z', 'Y', 'X' ,'W' , 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 
            'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A'};
        private JPanel panel;
        private JLabel messageLabel, countryLabel;
        private JTextField input, cInput;
        private JButton encryption;
        private int c = 0;
     
        public void Interface()
        {
            setTitle("Secret");
     
            setSize(300, 200);
     
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            buildInterface();
     
            add(panel);
     
            setVisible(true);
        }
     
        private void buildInterface()
        {
            messageLabel = new JLabel("Enter Code to be encrypted");
            countryLabel = new JLabel("Enter Destination Country");
     
            input = new JTextField(25);
            cInput = new JTextField(5);
     
            encryption = new JButton("Encrypt");
     
            panel = new JPanel();
     
            panel.add(messageLabel);
            panel.add(input);
            panel.add(countryLabel);
            panel.add(cInput);
            panel.add(encryption);
            encryption.addActionListener(new buttonlistener());
     
        }
     
        private class buttonlistener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {         
                if (e.getSource() == encryption)
                {
                    encryptText = input.getText();
                    encryptText = encryptText.toUpperCase();
                    c = encryptText.length();
                    for(int i = 0; i < 26; i++)
                    {
                        if (encryptText.charAt(0) == Alphabet[i])
                        {
                            temp = Encryption[i];
                            encryptText.replace(encryptText.charAt(0), temp);
                        }
                    }
                    System.out.println(temp);
                    System.out.println(encryptText);
     
                }
                if (e.getSource() == encryption)
                {
                    dCountry = cInput.getText();
                    dCountry = dCountry.toUpperCase();
                    if(dCountry.equals("FR"))
                    {
                        dCountry = "France";
                    }
                    else if(dCountry.equals("GB"))
                    {
                        dCountry = "Great Briton";
                    }
                    else if(dCountry.equals("CA"))
                    {
                        dCountry = "Canada";
                    }
                    else if(dCountry.equals("JA"))
                    {
                        dCountry = "Japan";
                    }
                    else if(dCountry.equals("RU"))
                    {
                        dCountry = "Russia";
                    }
                    else if(dCountry.equals("GE"))
                    {
                        dCountry = "Germany";
                    }
                    else if(dCountry.equals("AU"))
                    {
                        dCountry = "Australia";
                    }
                    else if(dCountry.equals("MX"))
                    {
                        dCountry = "Mexico";
                    }
                }
                if(e.getSource() == encryption)
                {
                    JOptionPane.showMessageDialog(null,"To: " + dCountry + "\n" + encryptText);
                }
            }
        }
     
    }

    This is what I have so far. I know my variable temp is getting the proper encryption letter after testing its output, but I cannot seem to replace that one with the one in the string to encrypt the message.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Encryption

    OK, one problem I see is that you may be trying to solve too many problems at once by trying to encrypt with a GUI. I suggest that you isolate the issue that you're currently trying to solve in a small program, and then when you get it to work, put it into the larger program.

    Suggestions:
    • Make your char arrays constants since you'll never change them. That means that they're declared static final and their names are all upper case.
    • You will likely need nested for loops. The outer loop will loop through all the chars in the original text, and the inner loop will loop through the ALPHABET array.
    • Make sure that your variable and method naming is compliant with Java naming conventions. All variable and method names should begin with a lower case letter. All class, interface, enum names begin with upper case. Constants are all upper case.


    For example your small test class could have a skeleton that looks like this:

    class SmallEncrypt {
       // makes these guys constants (static final) and make names all-caps
       private static final char[] ALPHABET = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
             'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
             'X', 'Y', 'Z' };
       private static final char[] REVERSE_ALPHABET = { 'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R',
             'Q', 'P', 'O', 'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D',
             'C', 'B', 'A' };
     
       public static void main(String[] args) {
          String originalText = "Hello World";
          String encryptText = "";
     
          for (int i = 0; i < originalText.length(); i++) {
             // get the char at position i
             // change to upper case
             // use inner for loop to find position in ALPHABET array
             // ....
             // encryptText += .... // you have to figure out what goes here
          }
     
          System.out.println("original text: " + originalText);
          System.out.println("encrypted tex: " + encryptText);
       }
    }

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    Kseidel (November 22nd, 2012)

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Encryption

    Ok, I get what your saying there, thank you, that logic is extremely helpful. How might I get it to skip white spaces though, in case I have for instance, a phrase that needs to be encrypted? An if statement using the isWhiteSpace method?

    --- Update ---

    Never-mind, I just added a white-space char to both arrays, and it seemed to work.

  7. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Encryption

    Wonderful, and glad you've got things working!

Similar Threads

  1. Caesar Encryption
    By andrew_m428 in forum The Cafe
    Replies: 1
    Last Post: October 4th, 2012, 10:59 PM
  2. Replies: 0
    Last Post: September 6th, 2012, 10:57 AM
  3. [SOLVED] Need help with IDEA Encryption
    By finalforce in forum Algorithms & Recursion
    Replies: 1
    Last Post: June 4th, 2012, 03:41 AM
  4. Hello.. need help in encryption process
    By apisz8701 in forum Member Introductions
    Replies: 0
    Last Post: February 13th, 2011, 05:03 AM
  5. Java Encryption
    By xxcorrosionxx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2011, 04:12 AM