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);

}
}

Recommended Answers

All 17 Replies

Hello ZaneDarklace, welcome to DaniWeb.

Is there a question associated with this code?

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

The code you posted seems OK. What exactly are you having difficulty with?

I am having trouble Encrypting and Decrypting the sentence

I keep getting an error stating that Crypto cannot be resolved to a type.

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.

I don't have the Blue Pelican book, but I assume it does explain what you are supposed to do?

Ok. Thank You for your help.

on the method what would you recommend i use?

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.

I am still a beginner to all of this. My apologies.

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! :-)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.