0

I am having a problem with my Encryption/Decryption project, This is what the project is asking for:

You have just been hired by the CIA as a programmer in the encryption department. Your job is to write a class called Crypto. One method, encrypt, will accept a String that represents the sentence to be encrypted. It will return a String that is the sentence with all v’s (big or small) replaced with “ag’,r”, all m’s (big or small) with “ssad”, all g’s (big or small) with “jeb..w”, and all b’s (big or small) with “dug?/”.

The class contains another method, decrypt, that accepts a String that represents the sentence to be decrypted. In this method the reverse process described above is performed. It returns a String that is the original sentence before encryption.

Use the following Tester class to insure that your methods work.

My tester class is below, I do not belive there is any problem with this:

//import java.io.*; 

import java.util.*;
//import java.io.*; 
//import java.util.*; 

public class CryptoTester {

    public static void main(String args[]) {
        Scanner kbReader = new Scanner(System.in);
        System.out.print("Enter a sentence that is to be encrypted:");
        String sntnc = kbReader.nextLine();
        System.out.println("Original Sentence = " + sntnc);

        Crypto myCryptObj = new Crypto();
        String encryptdSntnc = myCryptObj.encrypt(sntnc);
        System.out.println("Encrypted sentence = " + encryptdSntnc);

        String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc);
        System.out.println("Decrypted sentence = " + decryptdSntnc);
    }
}

My Encryption/decryption class is where I am having some significant issues, it does encrypt but not the right way, and it also does not decrypt:

class Crypto {
    public String s;

    public String acceptor(String sntnc) {
        String s;
        s = sntnc;
        return null;

    }

    public String encrypt(String sntnc) {
        sntnc = sntnc.replaceAll("V", "ag',r");
        sntnc = sntnc.replaceAll("v", "ag',r");
        sntnc = sntnc.replaceAll("M", "ssad");
        sntnc = sntnc.replaceAll("m", "ssad");
        sntnc = sntnc.replaceAll("G", "jeb..w");
        sntnc = sntnc.replaceAll("g", "jeb..w");
        sntnc = sntnc.replaceAll("B", "dug>?/");
        sntnc = sntnc.replaceAll("b", "dug>?/");
    
        return sntnc;
    }

    public String decrypt(String sntnc) {

        s = sntnc.replaceAll("ag',r", "V");
        s = sntnc.replaceAll("ag',r", "V");
        s = sntnc.replaceAll("ssad", "M");
        s = sntnc.replaceAll("ssad", "m");
        s = sntnc.replaceAll("jeb..w", "g");
        s = sntnc.replaceAll("jeb..w", "G");
        s = sntnc.replaceAll("dug>?/", "B");
        s = sntnc.replaceAll("dug>?/", "b");

        return sntnc;
    }

}

this is what it prints out in the console:

Enter a sentence that is to be encrypted: this is a very big morning
Original sentence: this is a very big morning
Encrypted sentence: this is a ajedug>?/..w',rery dug>?..w ssadorninjedug>?/..w
Decrypted sentence:this is a ajedug>?/..w',rery dug>?..w ssadorninjedug>?/..w
Community
  • 1
  • 1
Tyler
  • 3
  • 1
  • 2

1 Answers1

0

First of all, you shouldn't be using replaceAll(). That method, as its documentation indicates, takes a regex as first argument, and not a literal substring. replace() does what you want.

Second, the algorithm isn't really well described, and is not reversible (since there is no way to know, for example, if dug>?/was initially a bor a B).

What's sure is that with your interpretation of your algorithm, a v would first be replaced by ag',r, and the g contained in this replacement would then be replaced by jeb..w.

So, if you want to do the algorithm backwards, you need to first replace jeb..w by g, and then replace ag',r by v.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255