I am working on a project where you must first encrypt a sentence then decrypt it. the code that is used is:

import java.io.*;
import java.util.*;
public class Decryption{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);


4 Years
Discussion Span
Last Post by ZaneDarklace

Yes there is. I would appreciate some help in figuring out how to code this right. Please and Thank You


Judging by the code you posted, Crypto is the name of a class that has instance methods called encrypt and decrypt. You haven't provided any other information about your project, so I can't comment on where that class is or how it works, other than to note that it's not part of the standard Java API


Thank You JamesCherrill. I just realized that i needed to create another class. My project is in the BluePelicanJava book.


Doesn't the book give you any lead on this?
If not, the easiest thing to start with is the Caeser Cypher (Google it). To do it properly, use classes from the javax.crypto package (see the API doc for details)


OK, I finally managed to download a copy of the book. It explains exactly what the encode and decode methods should do. Read it properly before asking for more help.


The book he's working from explains exactly what method to use - it just replaces a few specific letters with some garbage strings. It's not anything to do with encryption really, it's just a beginner's exercise in using String methods.


One of the easiest cyphers is a simple xor substitution of the source text with letters from the passphrase. You iterate through the source and the passphrase (password) and xor each letter of the source with the current letter of the passphrase. When you get to the end of the passphrase, you wrap around back to the beginning of it.

This is NOT a secure algorithm, but it should be adequate for your class project. Just make sure that your professor understands that you know it is not secure, but just an example of a simple algorithm.

So, the signatures of your encrypt() and decrypt() methods on the Crypto class would look like this (I leave the implementation to you):

string encrypt(string source, string password);
string decrypt(string encrypted, string password);

Note that an xor algorithm is symmetrical. IE, to decrypt what you encrypted, you just do it the same way, applying the xor operation on the encrypted text in just the same way you encrypted the source in the first place. Keeps stuff simple! :-)

Edited by rubberman


Thank you for all the help everyone. My teacher told me that the coding help that i got is really good.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.