My teacher said that we needed to perform 3 mathematical actions on the chars so it works when I encrypt the textboxes but then it doesn't work when I decrypt them. I think it might be the order of my math operations in the decryption code?

For example:
I put into Encrypt: hello
Decrypts to : +*--.

Try to decrypt that back and it comes out to fcllo

public void actionPerformed(ActionEvent ae){

		if(ae.getActionCommand().equals("Encrypt")){

			if(!jtf.getText().equals("")){
				String text=jtf.getText();
				String ciph="";
				for(int i=0; i<text.length(); i++) {
					char c=text.charAt(i);
					int j=(int) c;
					j=((j/3)-5) + 14;
					ciph+=(char) j;
				}

				jtf2.setText(ciph);
				//jtf2.setText(Calculation.this.encrypt(jtf.getText()));
			}
		}

		if(ae.getActionCommand().equals("Decrypt")){

			if(!jtf2.getText().equals("")){
				//undo using math operations
				String text=jtf2.getText();
				String undone="";
				for(int i=0; i<text.length(); i++) {
					char c=text.charAt(i);
					int j=(int) c;
					j=(j+5-14)*3;
					undone+=(char) j;
				}
				jtf.setText(undone);
			}
		}

		if(ae.getActionCommand().equals("Reset")){

			jtf.setText("");
			jtf2.setText("");
		}
	}

Thanks guys!

often when you try to mod a negative number, the results can be unexpected.

try this

// to encrypt
int numberOfCharacters = 256;//the number of chars in your alphabet
int key; //the number of chars you want to shift by
String clearText = "hello";
String encrypedText = "";
key = key % numberOfCharacters;
for (int i = 0;i<clearText.length();i++){
   encryptedText = encrypedText+(char)((int)clearText.charAt(i)+key %
      numberOfCharacters);
}
// to decrypt
clearText = "";
for (int i = 0;i<encryptedText.length();i++){
   clearText = clearText+(char)((int)encryptedText.charAt(i)-key+numberOfCharacters %
      numberOfCharacters);
}
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.