2 Replies - 5059 Views - Last Post: 01 August 2010 - 09:38 PM Rate Topic: -----

#1 Guest_Tom*


Reputation:

Shift Cipher

Posted 01 August 2010 - 08:24 PM

My assignment is to create a shift cipher that has the user input some encrypted text and it will decipher it. The problem I run into is that the text that it prints to the screen that is supposed to be decrypted is all jumbled. Also, the program is supposed to output 26 different texts and you have to look at the 26 and decide which one is correct (one will be an actual word). Whenever I run the program it only outputs 1 text decryption, and it is in lowercase( program is supposed to print all the outputs in upper case). Any help would be greatly appreciated.

{
import java.util.Scanner;
public class shiftback
{
    public static void main( String args[] )
	 {
	 	Scanner keyboard = new Scanner(System.in);
		String str, str2;
		str2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		char [] alphabet = str2.toCharArray();
		int key,n,i,k,j, key2=0;
		char ptext, ctext, etext, ch;
		System.out.print("Enter a line of text: ");
		str = keyboard.next();
		char [] total= str.toCharArray();
		n = total.length;
		k = alphabet.length;
		for (j=0; j<26; j++);
		{
			for (i=0; i<n; i++)
			{
				ch = total[i];
				ch = (char)(ch-65);
				ch = (char)(ch + (26 - key2));
				ch = (char)(ch % 26);
				ch = (char)(ch + 97);
				System.out.print(ch);
			}
		System.out.println();
		}
	}
}
}


This post has been edited by pbl: 01 August 2010 - 08:48 PM
Reason for edit:: Code tags added please :code:


Is This A Good Question/Topic? 0

Replies To: Shift Cipher

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Shift Cipher

Posted 01 August 2010 - 08:46 PM

what an horrible hardcoding of value 97 and 65
Use 'a', 'A', 'z' and 'Z'
What do you want to prove ? That you know your ASCII code by heart ?
I have more than 35 years of coding experience and I do not know them :)

This post has been edited by pbl: 01 August 2010 - 08:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Shift Cipher

Posted 01 August 2010 - 09:38 PM

I made the same mistake when I was learning (not that I am done :wink:) That was the greatest thing ever when I found that out. lol, good times

EDIT: the reason it is all the same is because you never change the key. I suggest using the for(j...) loop to help you out with that

This post has been edited by Luckless: 01 August 2010 - 09:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1