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

Thread: [HELP] Encryption - Decryption alphabet

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Unhappy [HELP] Encryption - Decryption alphabet

    I want to create a java program that can be encrypt-decrypt alphabet from characters . Which is the shifting of the alphabet is starting from the the first character to increase 4 and 5 increased in the second character, increasing 6 at the third character, and so on..
    Then when one of the characters increasement is more than the ascii code of 'z', that character will decrease from the variable i from the loop. (Sorry my english isn't good enough)
    This is the output of the program that i want:
    Encryption:
    Plain text: "abcz"
    Cipher text: "egiz"

    Decryption:
    Cipher text: "egiz"
    Plain text: "abcz"

    This is my code for now:
     
    package -;
     
    public class - {
     
        public static void main(String[] args) {
     
            String enkripsi = "abcz".toLowerCase();
            String dekripsi = "egiz".toLowerCase();
            System.out.println("============================");
            System.out.println("\tENKRIPSI");
            System.out.println("============================");
            System.out.println("Plain text: ");
            System.out.println(enkripsi);
            System.out.println("");
            System.out.println("Cipher text:");
            enkrip(enkripsi);
            System.out.println("");
            System.out.println("============================");
            System.out.println("");
            System.out.println("============================");
            System.out.println("\t DEKRIPSI");
            System.out.println("============================");
            System.out.println("Cipher text:");
            System.out.println(dekripsi);
            System.out.println("");
            System.out.println("Plain text:");
            dekrip(dekripsi);
            System.out.println("");
            System.out.println("============================");
            System.out.println("");
            System.out.println("============================");
     
        }
     
        public static void enkrip(String enkripsi) {
            int a[] = new int[enkripsi.length()];
            for (int i = 0; i < a.length; i++) {
                a[i] = enkripsi.charAt(i) + 4 + i;
                if (a[i] >= 0 && a[i] <= 96 || a[i] > 122 && a[i] <= 255 ) {
                    a[i] = enkripsi.charAt(i) - 1;
                    a[i] = enkripsi.charAt(i) - (i % 26);
                    if (a[i] < (byte) 'a') {
                        a[i] = enkripsi.charAt(i) + (i % 26);
                    } else if (a[i] > (byte) 'z') {
                        a[i] = enkripsi.charAt(i) + (i % 26);
                    }
     
                }
     
                System.out.print((char) a[i]);
            }
        }
     
        public static void dekrip(String dekripsi) {
            int b[] = new int[dekripsi.length()];
            for (int i = 0; i < b.length; i++) {
     
                b[i] = dekripsi.charAt(i) - 4 - i;
                if (b[i] >= 0 && b[i] <= 96 || b[i] > 122 && b[i] <= 255) {
                b[i]= dekripsi.charAt(i)-1;    
                }
                if (b[i] < (byte) 'z') {
     
                    b[i] = dekripsi.charAt(i) + (i%26) ;
                    if (b[i] < (byte) 'a') {
                        b[i] = (i % 26) - dekripsi.charAt(i);
                    } else if (b[i] > (byte) 'z') {
                        b[i] = (i % 26) - dekripsi.charAt(i);
                    }
     
           }
     
                System.out.print((char) b[i]);
            }
        }
    }

    What's wrong with my code? It won't work. Specially in the decryption . Please help me...


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

    Default Re: [HELP] Encryption - Decryption alphabet

    Can you copy the console from when the program executed that shows what the problem is?
    For testing and debugging use the shortest input String that shows the problem. That will make tracing the code's execution flow easier.
    Try debugging the code by adding some println statements that shows the values of variables as their values are set and changed.
    If you don't understand my answer, don't ignore it, ask a question.

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

    noobies (March 9th, 2014)

  4. #3
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: [HELP] Encryption - Decryption alphabet

    This is the example when my program executed, and it's wrong in the decryption.

    ============================
    	ENKRIPSI
    ============================
    Plain text: 
    lamb
     
    Cipher text:
    pfsi
    ============================
     
    ============================
    	 DEKRIPSI
    ============================
    Cipher text:
    pfsi
     
    Plain text:
    pgul
    ============================
     
    ============================


    ============================
    	ENKRIPSI
    ============================
    Plain text: 
    friedchicken
     
    Cipher text:
    jwolllrtoxsy
    ============================
     
    ============================
    	 DEKRIPSI
    ============================
    Cipher text:
    jwolllrtoxsy
     
    Plain text:
    jxqopqxモwムラメ
    ============================
     
    ============================

  5. #4
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: [HELP] Encryption - Decryption alphabet

    Anyone can't help me?

  6. #5
    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] Encryption - Decryption alphabet

    It's interesting that the incorrect decryption results vary from the encrypted input in a simple way. For example:

    Encrypted: pfsi
    Decrypted: pgul

    Can you see that

    p + 0 = p
    f + 1 = g
    s + 2 = u
    i + 3 = l

    when what you want is:

    p - 4 = l
    f - 5 = a
    s - 6 = m
    i - 7 = b

    Can you see the problem in the results? If you can see the problem in the results, then you should be able to inspect the code and determine the fix.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    noobies (March 9th, 2014)

  8. #6
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: [HELP] Encryption - Decryption alphabet

    Quote Originally Posted by GregBrannon View Post
    It's interesting that the incorrect decryption results vary from the encrypted input in a simple way. For example:

    Encrypted: pfsi
    Decrypted: pgul

    Can you see that

    p + 0 = p
    f + 1 = g
    s + 2 = u
    i + 3 = l

    when what you want is:

    p - 4 = l
    f - 5 = a
    s - 6 = m
    i - 7 = b

    Can you see the problem in the results? If you can see the problem in the results, then you should be able to inspect the code and determine the fix.
    Yup, i know my mistake and I fixed it. So this is my code for now:
     public static void main(String[] args) {
            String enkripsi = "lamb".toLowerCase();
            String dekripsi = "pfsi".toLowerCase();
            System.out.println("============================");
            System.out.println("\tENKRIPSI");
            System.out.println("============================");
            System.out.println("Plain text: ");
            System.out.println(enkripsi);
            System.out.println("");
            System.out.println("Cipher text:");
            enkrip(enkripsi);
            System.out.println("");
            System.out.println("============================");
            System.out.println("");
            System.out.println("============================");
            System.out.println("\t DEKRIPSI");
            System.out.println("============================");
            System.out.println("Cipher text:");
            System.out.println(dekripsi);
            System.out.println("");
            System.out.println("Plain text:");
            dekrip(dekripsi);
            System.out.println("");
            System.out.println("============================");
            System.out.println("");
            System.out.println("============================");
     
        }
     
        public static void enkrip(String enkripsi) {
            int a[] = new int[enkripsi.length()];
            for (int i = 0; i < a.length; i++) {
                a[i] = enkripsi.charAt(i) + 4 + i;
                if (a[i] > (byte) 'z') {
                    a[i] = enkripsi.charAt(i) - (i % 26);
                } else if (a[i] < (byte) 'a') {
                    a[i] = enkripsi.charAt(i) + (i % 26);
                }
                System.out.print((char) a[i]);
            }
     
        }
     
        public static void dekrip(String dekripsi) {
            int b[] = new int[dekripsi.length()];
            for (int i = 0; i < b.length; i++) {
     
                b[i] = dekripsi.charAt(i) - 4 - i;
     
                System.out.print((char) b[i]);
            }
        }
    }

    And the ouput is:
    ============================
    	ENKRIPSI
    ============================
    Plain text: 
    lamb
     
    Cipher text:
    pfsi
    ============================
     
    ============================
    	 DEKRIPSI
    ============================
    Cipher text:
    pfsi
     
    Plain text:
    lamb
    ============================
     
    ============================

    BUT when I change the plain text into "abczwi" the encryption is correct, the cipher text is: "egiwsr"
    But in the decryption the cipher text not same with the plain text. Like this:
    ============================
    	 DEKRIPSI
    ============================
    Cipher text:
    egiwsr
     
    Plain text:
    abcpki
    ============================
     
    ============================

    How is it?

  9. #7
    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] Encryption - Decryption alphabet

    You should be able to look at the ciphered text, egiwsr, the resulting decrypted text, abcpki, (which should be abczwi) and then inspect your code to determine why the resulting decryption is incorrect, just as I did for you above.

  10. #8
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: [HELP] Encryption - Decryption alphabet

    Okay, I'll try here.
    And when I'm stuck I'll ask again if you don't mind

  11. #9
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: [HELP] Encryption - Decryption alphabet

    I'm stuck and I've found this methodology to complete my codes. But, what and where I should type my code here? :/
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package cobacoba;
     
    /**
     *
     * @author Hengky
     */
    public class Cobacoba {
     
        // prints out instead
        // while this is fundmentally useless
        // it appears to be an assignment requirement
        public static void encode(String s, int shift) {
            for (char c : s.toCharArray()) {
                encode(c, shift);
            }
            System.out.println();
        }
     
        // prints out the character
        public static void encode(char c, int shift) {
            // your code here
     
        }
     
        public static void enkrip(String s) {
            encode(s, 4);
        }
     
        public static void dekrip(String s) {
            encode(s, -4);
        }
     
        public static void main(String[] args) {
            System.out.println("Encryption:");
            String s = "abcz";
            System.out.print("Plain text: \"");
            System.out.print(s);
            System.out.println("\"");
            System.out.print("Cipher text: \"");
            enkrip(s);
            System.out.println("\"");
     
            System.out.println("Decryption:");
            s = "efgd";
            System.out.print("Cipher text: \"");
            System.out.print(s);
            System.out.println("\"");
            System.out.print("Plain text: \"");
            dekrip(s);
            System.out.println("\"");
     
        }
    }

  12. #10
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: [HELP] Encryption - Decryption alphabet

    This is my code for now
    package cobacoba;
     
    /**
     *
     * @author Hengky
     */
    public class Cobacoba {
     
        public static void main(String[] args) {
            String enkrip = "abczzzzzz".toLowerCase();
            String dekrip = "egidddddd".toLowerCase();
            int inc = 4;
            for (int i = 0; i < enkrip.length(); i++) {
                char abjad = enkrip.charAt(i);
                inc %= 26;
     
                if ((abjad >= 'a') && (abjad <= 'z')) {
                    abjad += inc + i;
                    if (abjad > 'z') {
     
                        abjad = (char) ((abjad - 'z' + 'a') - 1 - i);
                }
                System.out.print(abjad);
            }
            System.out.println("");
            for (int i = 0; i < dekrip.length(); i++) {
                char huruf= dekrip.charAt(i);
                inc %=26;
                if (huruf >= 'a' && huruf <= 'z' ) {
                    huruf-=inc +i;
                    if (huruf < 'a') {
                        huruf= (char)((huruf+ 'z'-'a')+1+i );
                    }
     
                }
                System.out.print(huruf);
            }
        }
    }

    The output will be:
    The "abczzzzzzz" will be encrypted to egidddddd, then "egiddddd" will be decrypted into abczzzz.
    BUT when I input "qwerty" it will encrypt "uaky^c". When I input "uak^y" it'll decrypt "qwer^y".
    Why the non alpahabetical character still pop out? And what going on with my decryption?
    any suggest? I'm trully stuck here -_-

  13. #11
    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] Encryption - Decryption alphabet

    The encrypt/decrypt algorithm must correctly handle the ends of the alphabet. When a character less than 'a' is needed, the algorithm must wrap to 'z' and then continue backwards from there. When a letter greater than 'z' is needed, the algorithm must wrap to 'a' and continue from there. You can accomplish this using a combination of if statements and the % operator.

  14. #12
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: [HELP] Encryption - Decryption alphabet

    Quote Originally Posted by GregBrannon View Post
    The encrypt/decrypt algorithm must correctly handle the ends of the alphabet. When a character less than 'a' is needed, the algorithm must wrap to 'z' and then continue backwards from there. When a letter greater than 'z' is needed, the algorithm must wrap to 'a' and continue from there. You can accomplish this using a combination of if statements and the % operator.
    I solved this Thanks for your responses guys!

Similar Threads

  1. Re: Simple encryption/ decryption problem
    By vynramiscal21 in forum Loops & Control Statements
    Replies: 0
    Last Post: December 10th, 2013, 08:26 PM
  2. A few questions about encryption and decryption
    By mjr in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 18th, 2013, 01:29 PM
  3. Cipher Client/Server Encryption/Decryption Program - Please Help
    By drwatson in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 15th, 2013, 11:13 AM
  4. Simple encryption/ decryption problem
    By searchformeaning in forum Loops & Control Statements
    Replies: 2
    Last Post: May 7th, 2012, 01:34 AM
  5. Java encryption and decryption
    By frozen java in forum Java Theory & Questions
    Replies: 2
    Last Post: December 4th, 2011, 04:01 PM