Home > Blockchain >  Noob Programmer getting Incorrect output
Noob Programmer getting Incorrect output

Time:09-30

Why am I getting incorrect output on the decrypt lines?

My output

DEFGHIJKLMNOPRSTUVWXYZABC

DEFGHIJKLMNOPRSTUVWXYZABCABCDEFGHIJKLMOPQRSTUVWXYZ

fghijklmnopqrtuvwxyzabcde

fghijklmnopqrtuvwxyzabcdeabcdefghijklmopqrstuvwxyz

Expected Output

DEFGHIJKLMNOPRSTUVWXYZABC

ABCDEFGHIJKLMOPQRSTUVWXYZ

fghijklmnopqrtuvwxyzabcde

abcdefghijklmopqrstuvwxyz

public class CaesarCipherCaseSensitive
{
public static String encrypt(String plainText, int shiftKey){
    if(shiftKey > 25) {
        shiftKey = shiftKey&; 
    }
    else if (shiftKey<0){
        shiftKey = (shiftKey&) 26; 
    }

    String cipherText = ""; 
    int length = plainText.length();

    for(int i=0; i<length; i  ) {
        char ch = plainText.charAt(i);
        if (Character.isLetter(ch)){

            if(Character.isLowerCase(ch)) {
                char c = (char) (ch shiftKey);
                if (c > 'z') {
                    cipherText  = (char) (ch - (26-shiftKey));
                }

                else {
                    cipherText  = c; 
                }
                
            }  
            else if (Character.isUpperCase(ch)){
                char c = (char) (ch shiftKey);
                if (c > 'Z') {
                    cipherText  = (char) (ch - (26-shiftKey));
                }

                else {
                    cipherText  = c; 
                }

            }
        }

        else{
            cipherText  = ch;
        }
    }
    return cipherText;
 
    }
public static String decrypt(String cipherText, int shiftKey){
    if(shiftKey > 25) {
        shiftKey = shiftKey&; 
    }
    else if (shiftKey<0){
        shiftKey = (shiftKey&) 26; 
    }

    int length = cipherText.length();

    for(int i=0; i<length; i  ) {
        char ch = cipherText.charAt(i);
        if (Character.isLetter(ch)){

            if(Character.isLowerCase(ch)) {
                char c = (char) (ch-shiftKey);
                if (c < 'a') {
                    cipherText  = (char) (ch   (26-shiftKey));
                }

                else {
                    cipherText  = c; 
                }
                
            }  
            else if (Character.isUpperCase(ch)){
                char c = (char) (ch-shiftKey);
                if (c < 'A') {
                    cipherText  = (char) (ch   (26-shiftKey));
                }

                else {
                    cipherText  = c; 
                }

            }
        }

        else{
            cipherText  = ch;
        }
    }
    return cipherText;
    }
public static void main(String[] args)
{
String message1 = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
System.out.println(encrypt(message1, 3));
System.out.println(decrypt(encrypt(message1, 3), 3));
System.out.println(encrypt(message1.toLowerCase(),5));
System.out.println(decrypt(encrypt(message1.toLowerCase(),5),5));
}
}

CodePudding user response:

As Dawood & Rakozay said, the problem is that decrypt is taking a parameter cipherText and adding to it so you are getting the input cipher text with the deciphered text appended to that.

Like in encrypt where you declare String cipherText = "", in decrypt you should declare String plainText = "" and append to that, and return it.

This makes your decrypt method look like:

public static String decrypt(String cipherText, int shiftKey)
{
    if (shiftKey > 25) {
        shiftKey = shiftKey % 26;
    } else if (shiftKey < 0) {
        shiftKey = (shiftKey % 26)   26;
    }

    int length = cipherText.length();
    String plainText = "";             // Added this

    for (int i = 0; i < length; i  )
    {
        char ch = cipherText.charAt(i);
        if (Character.isLetter(ch)) {

            if (Character.isLowerCase(ch)) {
                char c = (char) (ch - shiftKey);
                if (c < 'a') {
                    plainText  = (char) (ch   (26 - shiftKey));
                }

                else {
                    plainText  = c;     // Always add to `plainText`
                }

            } else if (Character.isUpperCase(ch)) {
                char c = (char) (ch - shiftKey);
                if (c < 'A') {
                    plainText  = (char) (ch   (26 - shiftKey));
                }

                else {
                    plainText  = c;
                }

            }
        }

        else {
            plainText  = ch;
        }
    }
    return plainText;
}

CodePudding user response:

here is an example how it should be

public class CaesarCipherCaseSensitive
{
public static String encrypt(String plainText, int shiftKey){
    if(shiftKey > 25) {
        shiftKey = shiftKey&; 
    }
    else if (shiftKey<0){
        shiftKey = (shiftKey&) 26; 
    }

    String cipherText = ""; 
    int length = plainText.length();

    for(int i=0; i<length; i  ) {
        char ch = plainText.charAt(i);
        if (Character.isLetter(ch)){

            if(Character.isLowerCase(ch)) {
                char c = (char) (ch shiftKey);
                if (c > 'z') {
                    cipherText  = (char) (ch - (26-shiftKey));
                }

                else {
                    cipherText  = c; 
                }
                
            }  
            else if (Character.isUpperCase(ch)){
                char c = (char) (ch shiftKey);
                if (c > 'Z') {
                    cipherText  = (char) (ch - (26-shiftKey));
                }

                else {
                    cipherText  = c; 
                }

            }
        }

        else{
            cipherText  = ch;
        }
    }
    return cipherText;
 
    }
public static String decrypt(String cipherText, int shiftKey){
    if(shiftKey > 25) {
        shiftKey = shiftKey&; 
    }
    else if (shiftKey<0){
        shiftKey = (shiftKey&) 26; 
    }

    String res = ""; 
    int length = cipherText.length();

    for(int i=0; i<length; i  ) {
        char ch = cipherText.charAt(i);
        if (Character.isLetter(ch)){

            if(Character.isLowerCase(ch)) {
                char c = (char) (ch-shiftKey);
                if (c < 'a') {
                    res  = (char) (ch   (26-shiftKey));
                }

                else {
                    res  = c; 
                }
                
            }  
            else if (Character.isUpperCase(ch)){
                char c = (char) (ch-shiftKey);
                if (c < 'A') {
                    res  = (char) (ch   (26-shiftKey));
                }

                else {
                    res  = c; 
                }

            }
        }

        else{
            res  = ch;
        }
    }
    return res;
    }
public static void main(String[] args)
{
String message1 = "ABCDEFGHIJKLMOPQRSTUVWXYZ";
System.out.println(encrypt(message1, 3));
System.out.println(decrypt(encrypt(message1, 3), 3));
System.out.println(encrypt(message1.toLowerCase(),5));
System.out.println(decrypt(encrypt(message1.toLowerCase(),5),5));
}
}
  •  Tags:  
  • java
  • Related