# Substitution Cipher

Page 1 of 1

## 10 Replies - 20795 Views - Last Post: 25 June 2013 - 09:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=323478&amp;s=2dabcb440fb5bc0c9cdd2a6b6ff06a8c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jcamana10

Reputation: 0
• Posts: 5
• Joined: 09-June 13

# Substitution Cipher

Posted 23 June 2013 - 12:41 PM

I have a project regarding substitution ciphers. Here are the requirements.

Provide a complete program that performs a simple substitution cipher. The program should take plain text and a shift value and produce the encrypted text. Then it should take encrypted text and a shift value and produce the plain text once again. A different encrypted text and shift can be entered so make sure to get input.

Example:
EASTER shifted by 3 to left would become HDVWHU
HDVWHU shifted by 3 to right would become EASTER

This is how the early Caesar Cipher worked.

Provide a main method. It should:

Get input for a string and a shift value
Convert to upper case
Only perform the following items on alphabetic characters between A and Z
Utilize a for loop which uses postfix incrementing operator
Convert character to its ASCII equivalent (type cast)
Shift buy shift value entered above
If you reach end of alphabet, wrap around
Example: A shifted to the left 2 would become Y
Convert back to its character equivalent (type cast)
Output the new character
Get input for a string and a shift value
Perform same steps above to convert the encrypted text back to plain text
Be sure to get input again as a different encrypted text may be entered
Utilize postfix increment/decrement operations and compound assignment operators for all math. Example: x++ or x+=2.

Sample Session:

easter
3
BXPQBO
bxpqbo
-3
EASTER
Press any key to continue . . .

Sample Session 2: : (Notice spaces removed from input)

This Is A Test
3
QEFPFPXQBPQ
qefpfpxqbpq
-3
THISISATEST
Press any key to continue . . .

Sample Session 3: (Notice the different shift and decrypt value)

hello
3
EBIIL
ebiil
5
ZWDDG
Press any key to continue . . .

Can someone assist with my code. I have a bunch of errors and Im lost. Here's my code

```import java.lang.*;

import java.util.*;

public class JRCamanaProg3
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String encryptText; //text to encrypt
int shiftValue; //number of spaces to shift
String decryptText; //text to decrypt

//User enters text to encrypt
encryptText = stdIn.nextString();

//User enters shift value
shiftValue = stdIn.nextInt();

//System returns encrypted string
String encryptedText = encrypt(encryptText , shiftValue); // text that has been encrypted
System.out.println(encryptedText);

//User enters text to decrypt
decryptText = stdIn.nextString();

//User enters shift value
shiftValue = stdIn.nextInt();

//System returns decrypted string
String decryptedText = decrypt(decryptText , shiftValue); // text that has been decrypted
System.out.println(decryptedText);
}

//Shift and manipulate characters
public static String shift(String enteredText, int shiftValue)
{

String convertedText = "";
for(int i = 0; i< enteredText.length(); i++){
char lowerCase = enteredText.charAt(i);

//Convert to upper case letters
char lowerCase = Character.toUpperCase(lowerCase);
int charNumber = upperLetter;

//Shift letters and wrap text
int rotateLetters = (charNumber + shiftValue) % 26;
char shiftLetters = (char) rotateShift;

//Populate new string of characters
convertedText += shiftLetters;
}
return convertedText;
}

//Encryption
public static String encrypt(String enteredText, int shiftValue);
{
String encryptedString = rotate(enteredText , shiftValue);
return encryptedString;
}

//Decryption
public static String decrypt(String enteredText, int shiftValue);
{
int negativeShiftValue = (-1) * (shiftValue);
String decryptedString = rotate(enteredText , negativeShiftValue);
return decryptedString;
}
}// end
```

This post has been edited by GregBrannon: 23 June 2013 - 12:46 PM

Is This A Good Question/Topic? 0

## Replies To: Substitution Cipher

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12315
• Posts: 45,414
• Joined: 27-December 08

## Re: Substitution Cipher

Posted 23 June 2013 - 01:15 PM

You need to post your errors exactly as they're appearing. Otherwise, we won't be able to help.

### #3 jcamana10

Reputation: 0
• Posts: 5
• Joined: 09-June 13

## Re: Substitution Cipher

Posted 23 June 2013 - 01:36 PM

Sorry, Im new to this... I have multiple errors

Here are the errors:

The import java.lang is never used

The method nextString() is undefined for the type Scanner- Lines 16 and 28

Duplicate local variable lowerCase- Line 49

upperLetter cannot be resolved to a variable- Line 50

rotateShift cannot be resolved to a variable- Line 54

This method requires a body instead of a semicolon- Lines 63 and 70

Multiple markers at line 65 - enteredText cannot be resolved to a variable
- shiftValue cannot be resolved to a variable

Void methods cannot return a value- Line 66 and 74

shiftValue cannot be resolved to a variable- Line 72

enteredText cannot be resolved to a variable- Line 73

### #4 schutzzz

• D.I.C Regular

Reputation: 143
• Posts: 342
• Joined: 22-April 13

## Re: Substitution Cipher

Posted 23 June 2013 - 01:46 PM

Quote

The import java.lang is never used

Not an error, just a warning saying that you didn't use that import.

Quote

The method nextString() is undefined for the type Scanner- Lines 16 and 28

nextString() isn't a method for Scanner. Try nextLine()

Quote

Duplicate local variable lowerCase- Line 49

You can't declare the same variable twice.

Quote

This method requires a body instead of a semicolon- Lines 63 and 70

Semicolon needs to be removed.

That's a few fixed. The errors are pretty self explainatory. As you haven't created the variables for the other variables that you called.

This post has been edited by schutzzz: 23 June 2013 - 01:51 PM

### #5 jcamana10

Reputation: 0
• Posts: 5
• Joined: 09-June 13

## Re: Substitution Cipher

Posted 23 June 2013 - 03:01 PM

Thanks. I think I was able to compile it and run the program but the results im getting is nowhere close to being correct. What am i doing wrong? here's my updated code:
```import java.util.*;

public class JRCamanaProg3
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String encryptText; //text to encrypt
int shiftValue; //number of spaces to shift
String decryptText; //text to decrypt

//User enters text to encrypt
encryptText = stdIn.nextLine();

//User enters shift value
shiftValue = stdIn.nextInt();

//System returns encrypted string
String encryptedText = encrypt(encryptText , shiftValue); // text that has been encrypted
System.out.println(encryptedText);

//User enters text to decrypt
decryptText = stdIn.nextLine();

//User enters shift value
shiftValue = stdIn.nextInt();

//System returns decrypted string
String decryptedText = decrypt(decryptText , shiftValue); // text that has been decrypted
System.out.println(decryptedText);
}

//Shift and manipulate characters
public static String shift(String enteredText, int shiftValue)
{

String convertedText = "";
for(int i = 0; i< enteredText.length(); i++){
char lowerCase = enteredText.charAt(i);

//Convert to upper case letters
lowerCase = Character.toUpperCase(lowerCase);
int upperLetter = 0;
int charNumber = upperLetter;

//Shift letters and wrap text
int rotateLetters = (charNumber + shiftValue) % 26;
char rotateShift = 0;
char shiftLetters = (char) rotateShift;

//Populate new string of characters
convertedText += shiftLetters;
}
return convertedText;
}

//Encryption
public static String encrypt(String enteredText, int shiftValue)
{
String encryptedString = rotate(enteredText , shiftValue);
return encryptedString;
}

private static String rotate(String enteredText, int shiftValue)
{
String encryptedString = rotate(enteredText , shiftValue);
return encryptedString;
}

//Decryption
public static String decrypt(String enteredText, int shiftValue)
{
int negativeShiftValue = (-1) * (shiftValue);
String decryptedString = rotate(enteredText , negativeShiftValue);
return decryptedString;
}

}// end

```

This post has been edited by macosxnerd101: 23 June 2013 - 04:26 PM
Reason for edit:: Please use code tags

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12315
• Posts: 45,414
• Joined: 27-December 08

## Re: Substitution Cipher

Posted 23 June 2013 - 04:27 PM

A couple things. First, you need to use code tags: .

Second, in order for us to help you, it is important that you define exactly how your program "isn't working" in comparison to the expected results. The more specific you can be, the better help we can provide. Telling us that "it isn't working" isn't good enough.

### #7 jcamana10

Reputation: 0
• Posts: 5
• Joined: 09-June 13

## Re: Substitution Cipher

Posted 23 June 2013 - 04:36 PM

I apologize.

The issue that I am having is when I run my program, this is the result that I get:

EASTER
3
at JRCamanaProg3.rotate(JRCamanaProg3.java:78)
at JRCamanaProg3.rotate(JRCamanaProg3.java:78)
at JRCamanaProg3.rotate(JRCamanaProg3.java:78)...

It is not encrypting the text that I want to encrypt.

### #8 TechnoBear

Reputation: 186
• Posts: 818
• Joined: 02-November 11

## Re: Substitution Cipher

Posted 24 June 2013 - 11:22 AM

I literally read through the tutorial on cipher codes the other day.

http://www.dreaminco...-ceasar-cipher/

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12315
• Posts: 45,414
• Joined: 27-December 08

## Re: Substitution Cipher

Posted 24 June 2013 - 11:23 AM

Look at your rotate() method. Why are you rotating endlessly in rotate()?
```private static String rotate(String enteredText, int shiftValue)
70	    {
71	        String encryptedString = rotate(enteredText , shiftValue);
72	        return encryptedString;
73	    }

```

### #10 jon.kiparsky

• Beginner

Reputation: 11069
• Posts: 18,907
• Joined: 19-March 11

## Re: Substitution Cipher

Posted 24 June 2013 - 11:28 AM

You don't actually have to import java.lang. You get that for free. (it's where Strings and Integers and System come from - it's imported by default)

Quote

Look at your rotate() method. Why are you rotating endlessly in rotate()?

Another way to look at this: under what circumstances could you ever stop rotate()ing?

### #11 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Substitution Cipher

Posted 25 June 2013 - 09:04 PM

The easiest way is to make a double alphabet

String alpha = "abcdefghijklmnopqrstuvwzyzabcdefghijklmnopqrstuvwzyz";

to cypher with a shift of N

```char cypher(char theCharacter, int shift) {
for(int i = 0; i < 26; ++i) {
if(alpha.charAt(i) == theCharacter)
return alpha.charAtI(i + shift);
}
return theCharacter;   .. character that I don't encode
{

```