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

Thread: Help With Java Encryption Program

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

    Default Help With Java Encryption Program

    Right, i am in dire need of help, I have been given a java assignment to do where I need to write code which first reads in a line of text from the keyboard, displays that line, encrypts it, displays the encrypted text, decrypts the encrypted text and finally displays the decrypted text (which should look like the original).

    Its to be made as an expansion of the following code:
    This is the first Class (Caeser)

    /** Class for doing encryption and decryption using the Caesar Cipher. */
    public class Caeser {
    public static final int ALPHASIZE = 26; // English alphabet(uppercase)
    public static final char[] alpha =
    {'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'};
     
    protected char[] encrypt = new char[ALPHASIZE]; // Encryption array
    protected char[] decrypt = new char[ALPHASIZE]; // Decryption array
    /** Constructor that initializes the encryption and decryption arrays */
    public Caeser() {
    for (int i=0; i<ALPHASIZE; i++)
    encrypt[i] = alpha[(i + 3) % ALPHASIZE]; // rotate by 3 places
    for (int i=0; i<ALPHASIZE; i++)
    decrypt[encrypt[i] - 'A'] = alpha[i]; // reverse of encrypt
    }
    /** Encryption method */
    public String encrypt(String secret) {
    char[] mess = secret.toCharArray(); // the message array
    for (int i=0; i<mess.length; i++) // encryption loop
    if (Character.isUpperCase(mess[i])) // we have a letter to change
    mess[i] = encrypt[mess[i] - 'A']; // encrypt letter
    return new String(mess);
    }
    /** Decryption method */
    public String decrypt(String secret) {
    char[] mess = secret.toCharArray(); // the message array
    for (int i=0; i<mess.length; i++) // decryption loop
    if (Character.isUpperCase(mess[i])) // we have a letter to change
    mess[i] = decrypt[mess[i] - 'A']; // decrypt letter
    return new String(mess);
    }
    }
    /** Simple class for testing the Caesar cipher */

    This is the second class - CaeserTryOut

    public class CaeserTryOut {
    public static void main(String[] args) {
    Caeser cipher = new Caeser(); // Create a Caesar cipher object
    String secret = "THE EAGLE IS IN PLAY; MEET AT JOE'S.";
    secret = cipher.encrypt(secret);
    System.out.println(secret); // the cipher text
    secret = cipher.decrypt(secret);
    System.out.println(secret); // should be original again
    }
    }

    But I am so rusty with java as i have not used it in a while that I have no idea where to go from this...All i know is that I need to generate a random replacement for each character :S

    "The encryption should involve generating a random replacement for each character (both upper and lower case), while ensuring that the same character appearing more than once in the original is replaced by the same character in the coded string. You must also ensure that no two characters in the original are coded as the same character in the coded string, in order to enable successful decryption. Non-alphabetic characters in the original should remain as they are in the coded string. The code should include instance methods to encrypt and decrypt, both taking a string passed as parameter.
    Both upper and lower-case characters should be encrypted and characters in the decrypted message should match the case of those in the original message."

    Even is someone can just point me in the right direction, Thanks!


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Help With Java Encryption Program

    Quote Originally Posted by JonnySKK
    I need to generate a random replacement for each character
    I would use a "random shuffle algorithm" to create a character array that has a "random" replacement for each character in the original [A-Z] array.

    Here's a testbed:

    import java.util.*;
     
    public class Z {
        public static void main(String [] args) {
     
            char [] plain = new char[26];
            char thisChar = 'A';
            for (int i = 0; i < 26; i++) {
                plain[i] = thisChar++;
            }
            System.out.printf("Plain      :");
            printArray(plain);
     
            char [] enc = Arrays.copyOf(plain, plain.length);
     
            randomShuffle(enc);
            System.out.printf("Encode Key :");
            printArray(enc);
     
        } // End main()
     
        static void printArray(char [] x) {
            System.out.print("[");
     
            for (int i = 0; i < x.length; i++) {
                System.out.printf("%c", (char)x[i]);
            }
            System.out.println("]");
        }
     
     
        static void randomShuffle(char [] x) {
          //
          // Suggestion: Use "The modern algorithm" pseudo-code from
          //the wikipedia article "Fisher-Yates shuffle"
          //
        }
    }

    Not familiar with 'random shuffle'?

    See "The Modern Algorithm" pseudo-code at Fisher-Yates shuffle - wikipedia

    It translates to about five or six lines of Java. It's really simple, very efficient (requires a grand total of 25 values from a "random" number generator), and it is mathematically robust, as indicated in the wikipedia article and in Volume 2 (Seminumerical Algorithms) of Knuth's The Art of Computer Programming.

    A run or two or three:
    Plain      :[ABCDEFGHIJKLMNOPQRSTUVWXYZ]
    Encode Key :[SFVXGTAPEDNUCBLIJOKWHYRQZM]


    And

    Plain      :[ABCDEFGHIJKLMNOPQRSTUVWXYZ]
    Encode Key :[CEANDLIWRZFGKMQPTBJUXHOSVY]


    And

    Plain      :[ABCDEFGHIJKLMNOPQRSTUVWXYZ]
    Encode Key :[OPAYZGJTSRFUWQLHIKCMDNBEVX]

    I could go on all day...


    Cheers!

    Z
    Last edited by Zaphod_b; October 9th, 2012 at 06:47 PM.

Similar Threads

  1. Encryption password by java MD5 and UTF-8
    By jahirul019 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 6th, 2012, 05:35 PM
  2. Replies: 0
    Last Post: September 6th, 2012, 10:57 AM
  3. Java encryption and decryption
    By frozen java in forum Java Theory & Questions
    Replies: 2
    Last Post: December 4th, 2011, 04:01 PM
  4. Java Encryption
    By xxcorrosionxx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2011, 04:12 AM
  5. Basic Java Encryption
    By BronxBomber in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2010, 10:50 PM