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

Thread: [Help] A simple encode and decode program

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

    Question [Help] A simple encode and decode program

    Hello everyone, this is my first post, and I have a problem I just can't solve and I don't know where I got wrong so I am here to ask you Masters to help me a newbie The problem is the result of encoded message can't not restore to the original message by the decoder. Here are my three class's code

    SecureMsgMain:
    package securemsg.core;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;
     
    import securemsg.database.*;
     
    public class SecureMsgMain {
     
    	public static String a = Encodevariables.a;
    	public static String b = Encodevariables.b;
    	public static String c = Encodevariables.c;
    	public static String d = Encodevariables.d;
    	public static String e = Encodevariables.e;
    	public static String f = Encodevariables.f;
    	public static String g = Encodevariables.g;
    	public static String h = Encodevariables.h;
    	public static String i = Encodevariables.i;
    	public static String j = Encodevariables.j;
    	public static String k = Encodevariables.k;
    	public static String l = Encodevariables.l;
    	public static String m = Encodevariables.m;
    	public static String n = Encodevariables.n;
    	public static String o = Encodevariables.o;
    	public static String p = Encodevariables.p;
    	public static String q = Encodevariables.q;
    	public static String r = Encodevariables.r;
    	public static String s = Encodevariables.s;
    	public static String t = Encodevariables.t;
    	public static String u = Encodevariables.u;
    	public static String v = Encodevariables.v;
    	public static String w = Encodevariables.w;
    	public static String x = Encodevariables.x;
    	public static String y = Encodevariables.y;
    	public static String z = Encodevariables.z;
     
    	public static String dea = Decodevariables.dea;
    	public static String deb = Decodevariables.deb;
    	public static String dec = Decodevariables.dec;
    	public static String ded = Decodevariables.ded;
    	public static String dee = Decodevariables.dee;
    	public static String def = Decodevariables.def;
    	public static String deg = Decodevariables.deg;
    	public static String deh = Decodevariables.deh;
    	public static String dei = Decodevariables.dei;
    	public static String dej = Decodevariables.dej;
    	public static String dek = Decodevariables.dek;
    	public static String del = Decodevariables.del;
    	public static String dem = Decodevariables.dem;
    	public static String den = Decodevariables.den;
    	public static String deo = Decodevariables.deo;
    	public static String dep = Decodevariables.dep;
    	public static String deq = Decodevariables.deq;
    	public static String der = Decodevariables.der;
    	public static String des = Decodevariables.des;
    	public static String det = Decodevariables.det;
    	public static String deu = Decodevariables.deu;
    	public static String dev = Decodevariables.dev;
    	public static String dew = Decodevariables.dew;
    	public static String dex = Decodevariables.dex;
    	public static String dey = Decodevariables.dey;
    	public static String dez = Decodevariables.dez;
     
    	String enmsg = null;
    	String demsg = null;
     
    	public static void main(String[] args) throws IOException {
    		run();
    	}
     
    	public static void run() throws IOException {
    		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    		String option;
    		print("================================");
    		print("encode [Add protection to input]");
    		print("decode [Read the protected info]");
    		print("================================");
    		option = in.readLine().toLowerCase().trim();
     
    		while (option != null) {
    			BufferedReader code = new BufferedReader(new InputStreamReader(System.in));
    			if (option.equalsIgnoreCase("encode")) {
    				String enmsg = code.readLine().toLowerCase();
    				enmsg = enmsg.replace("a", a).replace("b", b)
    						.replace("c", c).replace("d", d)
    						.replace("e", e).replace("f", f)
    						.replace("g", g).replace("h", h)
    						.replace("i", i).replace("j", j)
    						.replace("k", k).replace("l", l)
    						.replace("m", m).replace("n", n)
    						.replace("o", o).replace("p", p)
    						.replace("q", q).replace("r", r)
    						.replace("s", s).replace("t", t)
    						.replace("u", u).replace("v", v)
    						.replace("w", w).replace("x", x)
    						.replace("y", y).replace("z", z);
    				if (enmsg != null) {
    					print("Message encoded to " + enmsg);
    					enmsg = null;
    					run();
    				} else {
     
    				}
    			}
     
    			if (option.equalsIgnoreCase("decode")) {
    				String demsg = code.readLine().toLowerCase().replace(dea, "a")
    						.replace(deb, "b").replace(dec, "c")
    						.replace(ded, "d").replace(dee, "e")
    						.replace(def, "f").replace(deg, "g")
    						.replace(deh, "h").replace(dei, "i")
    						.replace(dej, "j").replace(dek, "k")
    						.replace(del, "l").replace(dem, "m")
    						.replace(den, "n").replace(deo, "o")
    						.replace(dep, "p").replace(deq, "q")
    						.replace(der, "r").replace(des, "s")
    						.replace(det, "t").replace(deu, "u")
    						.replace(dev, "v").replace(dew, "w")
    						.replace(dex, "x").replace(dey, "y")
    						.replace(dez, "z");
    				if (demsg != null) {
    					print("Message decoded to " + demsg);
    					demsg = null;
    					run();
     
    				} else {
     
    				}
    			} else {
    				print("Unknown input");
    				System.out.println();
    				run();
    			}
    		}
    	}
     
    	public static void print(String input) {
    		System.out.println("[SecureMsg] " + input);
    	}
     
    }

    Encodevariables:
    package securemsg.database;
     
    public class Encodevariables {
     
    	public static String a = "q";
    	public static String b = "w";
    	public static String c = "e";
    	public static String d = "r";
    	public static String e = "t";
    	public static String f = "y";
    	public static String g = "u";
    	public static String h = "i";
    	public static String i = "o";
    	public static String j = "p";
    	public static String k = "a";
    	public static String l = "s";
    	public static String m = "d";
    	public static String n = "f";
    	public static String o = "g";
    	public static String p = "h";
    	public static String q = "j";
    	public static String r = "k";
    	public static String s = "l";
    	public static String t = "z";
    	public static String u = "x";
    	public static String v = "c";
    	public static String w = "v";
    	public static String x = "b";
    	public static String y = "n";
    	public static String z = "m";
     
     
     
     
    }

    Decodevariables:
    package securemsg.database;
     
    public class Decodevariables {
     
    	public static String deq = "a";
    	public static String dew = "b";
    	public static String dee = "c";
    	public static String der = "d";
    	public static String det = "e";
    	public static String dey = "f";
    	public static String deu = "g";
    	public static String dei = "h";
    	public static String deo = "i";
    	public static String dep = "j";
    	public static String dea = "k";
    	public static String des = "l";
    	public static String ded = "m";
    	public static String def = "n";
    	public static String deg = "o";
    	public static String deh = "p";
    	public static String dej = "q";
    	public static String dek = "r";
    	public static String del = "s";
    	public static String dez = "t";
    	public static String dex = "u";
    	public static String dec = "v";
    	public static String dev = "w";
    	public static String deb = "x";
    	public static String den = "y";
    	public static String dem = "z";
     
     
     
     
    }

    Here are some example output/input from the console:

    [SecureMsg] ================================
    [SecureMsg] encode [Add protection to input]
    [SecureMsg] decode [Read the protected info]
    [SecureMsg] ================================
    encode
    hi
    [SecureMsg] Message encoded to gg
    [SecureMsg] ================================
    [SecureMsg] encode [Add protection to input]
    [SecureMsg] decode [Read the protected info]
    [SecureMsg] ================================
    decode
    gg
    [SecureMsg] Message decoded to xx

    So if you know where I got wrong please help me Thank you very much!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: [Help] A simple encode and decode program

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    This is probably the most unique and complicated approach I've ever seen to a simple substitution cipher. A more common and simpler approach is to use an array that maps the cipher text to clear text and vice-versa. For example, since 'a' = 'q', to cipher, an 'a' would be replaced by 'q', the first element of the cipher array. To decipher, since 'q' is the first element of the cipher array, it matches to 'a'. This simpler substitution approach can be done with addition and subtraction.

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

    Default Re: [Help] A simple encode and decode program

    Can you give me a example of converting "a" to "q" and "q" to "a" ? Thank you.

    --- Update ---

    I am kinda new to Java, so can you give me a example of code convert "a" to "q" and vice-versa? Thank you very much! Much appreciated

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

    Default Re: [Help] A simple encode and decode program

    One "trick" for using char values to index into a array is that the code can do math with char values.
    For example if you want to use the letters: 'a' to 'z' to index into an array of 26 elements, subtract the value: 'a' from char variable: 'a' - 'a' = 0 and 'b' - 'a' = 1 etc
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Encode PNG
    By Cornix in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 21st, 2013, 09:21 AM
  2. Replies: 7
    Last Post: June 6th, 2012, 03:16 PM
  3. [SOLVED] Help reading a file to decode or encode
    By mickey2012 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 29th, 2012, 10:46 PM
  4. encode a png image in ascii format..
    By entwicklerin in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: January 16th, 2012, 10:17 AM
  5. Encode and decode - Caesar cipher
    By siabanie in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2011, 06:25 PM