Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.36/5 (4 votes)
See more:
I have Autokey cipher code which is not working correctly - it is giving cipher text as its plain text.

Can someone help?

Java
import java.util.Scanner;

public class k {

    public static void main(String[] args){

        String cip=k.encrypt();
        String pln=k.decrypt(cip);

    }

    private static String encrypt() {
        // TODO Auto-generated method stub

            Scanner input = new Scanner(System.in);
            System.out.println("Enter the plaintext");
            String plainText = input.nextLine();
            int len = plainText.length(), asciiValue, newValue, letterValue, x = 0, counter = 0, nexti = 0;
            String ciphertext = new String();
            char current;
            plainText = plainText.toUpperCase(); //it makes it easier to have it all in one case
            String keyword = "secret";
            keyword = keyword.toUpperCase();

            for (int i = 0; i < keyword.length(); i++)
            {
                current = plainText.charAt(i);
                if (Character.isSpace(current))
                {
                    ciphertext += ' ';
                    i++;
                    current = plainText.charAt(i);
                }

                asciiValue = ((int)current);
                //if it's an uppercase letter, encode it
                if (asciiValue >= 65 && asciiValue <= 90)
                {
                    letterValue = asciiValue - 65;
                    newValue = letterValue + (((int)(keyword.charAt(counter))) - 65);
                    newValue %= 26;
                    ciphertext += (char)(newValue + 65);//add it to the ciphertext
                    counter++;
                }
                nexti = i;
            }

            x = 0;
            for (int i = nexti + 1; i < len; i++)
            {
                char temp = ' ';
                current = plainText.charAt(i);
                if (Character.isSpace(current))
                {
                    ciphertext += ' ';
                    i++;
                    current = plainText.charAt(i);
                }

                asciiValue = ((int)current);
                //if it's an uppercase letter, encode it
                if (asciiValue >= 65 && asciiValue <= 90)
                {
                    letterValue = asciiValue - 65;
                    temp = ciphertext.charAt(x);
                    while (temp == ' ')
                    {
                        x++;
                        temp = ciphertext.charAt(x);
                    }

                    newValue = letterValue + (((int)(temp)) - 65);//add the shift

                    newValue %= 26;
                    ciphertext += (char)(newValue + 65);//add it to the ciphertext
                    x++;
                }

                temp = ' ';
            }
            System.out.println("Cipher text = "+ciphertext);
            return ciphertext;

        }


    private static String decrypt(String ciphertext) {

        int x = 0, len = ciphertext.length(), asciiValue, newValue, letterValue, y = 0;
        String plaintext = new String();
        char current, temp = ' ';
        ciphertext = ciphertext.toLowerCase();//it makes it easier to have it all in one case
        String keyy = "secret";
        int keywordLength = keyy.length();

        for (int i = 0; i < keywordLength; i++)
        {
            if (x < ciphertext.length())
            {
                plaintext += ciphertext.charAt(x);
                x++;

                if (Character.isSpace(ciphertext.charAt(x - 1)))
                {
                    plaintext += ciphertext.charAt(x);
                    x++;
                }
                y = x;
            }
        }

        x = 0;
        for (int i = 0; i < (len - y); i++)
        {

            current = ciphertext.charAt(i + y);
            if (Character.isSpace(current))
                plaintext += ' ';


            else
            {
                asciiValue = ((int)current);
                if (asciiValue >= 97 && asciiValue <= 122)
                {
                    letterValue = asciiValue - 97;

                    temp = ciphertext.charAt(x);
                    while (temp == ' ')
                    {
                        x++;
                        temp = ciphertext.charAt(x);
                    }

                    newValue = letterValue - (((int)(temp)) - 97); //take off the shift
                    newValue %= 26;
                    //if we've gone below 0, we add 26, which has the effect of wrapping around to the end of the alphabet
                    if (newValue < 0)
                        newValue += 26;

                    plaintext += (char)(newValue + 97);//add it to the plaintext
                    x++;
                }
            }
            temp = ' ';
        }
        System.out.println("Plaintext text = "+plaintext);
        return plaintext;
    }


    }
Posted
Updated 7-Mar-12 6:43am
v4
Comments
Chris Maunder 7-Mar-12 10:01am    
What have you tried?
Richard MacCutchan 7-Mar-12 12:02pm    
First you need to explain what is not working, I don't think anyone can figure out what's wrong without some more information.
CurrentlyBE 7-Mar-12 12:24pm    
I thought you will run it and then look at it..
Anyways problem is it is giving correct cipher text but not decrypted plain text.
May be problem is with decryption method but what is the problem am not getting it.
CurrentlyBE 7-Mar-12 12:25pm    
it is giving cipher text as its plain text...

1 solution

Your code does NOT give out the cipher text. It struggles after some characters and gives nonsense.

Java
// in function decrypt() around line 100
for (int i = 0; i < keywordLength; i++)
        {
            if (x < ciphertext.length())
            {
                plaintext += ciphertext.charAt(x);
                x++;
 
                if (Character.isSpace(ciphertext.charAt(x - 1)))
                {
                    plaintext += ciphertext.charAt(x);
                    x++;
                }
                y = x;
            }
        }


I guess that works as long as the keyword is longer than the cipher text.


Also please think about the concept of OOP. It seems that you've not really understood what OOP means. I struggled upon the private static String encrypt() - which means it does not get any arguments but delivers a value back. Pretty strange for such a function.

Entering text and result output should be done in your main, leaving just the encryption and decryption to your methods. keep them functional! don't give them several tasks to do. 1 task for 1 function.

Also please don't make the methods static - init an object (might be named DeEncrpyter or even "K" <- KAPITAL K that is!! ) and work with it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900