# Simple Encryption Algorithms

Page 1 of 1

## 9 Replies - 1419 Views - Last Post: 29 June 2016 - 03:36 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=394873&amp;s=6c9299651f9b4f4e74563637e04fd0b9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jacks94

Reputation: 0
• Posts: 5
• Joined: 29-April 15

# Simple Encryption Algorithms

Posted 28 June 2016 - 04:05 PM

So I have an encryption assignment that uses the basics of Java in order to create it. The tasks for the assignment is to:
Ask the user for a shift key
Ask them for a 5 character code(any key on the keyboard)
Then shift the code by the shift key that was given. Similar to the caesar cipher
I am asking for help on how to do this. Could I create a method in order to do this or can I just concatenate everything?
I want help with trying to figure out how to shift 1,2,3,4,5 to 5,5,7,7,9 if the shift key is something like 2.

```import java.util.Scanner;

public class Ethics
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int sKey = in.hasNextInt();
Scanner keyboard = new Scanner(System.in);
String code = keyboard.hasNext();

while (keyboard.hasNext)
{

System.out.println("The original code is: " + code);
System.out.println("The new code is: " + sCode );

System.out.println("Would you like to continue?(Y/y/N/n)");

}

```

Is This A Good Question/Topic? 0

## Replies To: Simple Encryption Algorithms

### #2 g00se

• D.I.C Lover

Reputation: 3648
• Posts: 16,770
• Joined: 20-September 08

## Re: Simple Encryption Algorithms

Posted 28 June 2016 - 04:29 PM

http://technojeeves....-cipher-in-java

Try and work the above into your code in your own way

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12680
• Posts: 45,861
• Joined: 27-December 08

## Re: Simple Encryption Algorithms

Posted 28 June 2016 - 06:43 PM

Quote

with trying to figure out how to shift 1,2,3,4,5 to 5,5,7,7,9 if the shift key is something like 2.

It's not immediately clear how you would attain that output. You would need to define your algorithm more clearly and explain the logic. Once you have defined the logic, the implementation is the easy part.

I suspect, though, that you are after something more like the Vigenere Cipher.

I should note as well that your code does not compile. At line 14, for instance: while (keyboard.hasNext). The hasNext() method is actually a method, not an instance variable, for the Scanner class. So this should be: while (keyboard.hasNext()).

Additionally, you should adopt a clear indentation convention. Having code that is clean and readable goes a long ways in helping you (and us) to follow the logic and debug your code.

### #4 g00se

• D.I.C Lover

Reputation: 3648
• Posts: 16,770
• Joined: 20-September 08

## Re: Simple Encryption Algorithms

Posted 29 June 2016 - 01:01 AM

Quote

It's not immediately clear how you would attain that output.

That's true - didn't notice that Certainly not with a normal application of Caesar

### #5 jacks94

Reputation: 0
• Posts: 5
• Joined: 29-April 15

## Re: Simple Encryption Algorithms

Posted 29 June 2016 - 10:32 AM

Ok so I'm using the Cipher that you gave me. But I also need to include basically everything from the keyboard as well. The output should read as follows.
The original code:
The new code(backwards):
Would you like to continue Y/N?
So if the user has a shift key of two it would shift the corresponding input two and once it is created if any of the new coded characters are represented by an even ASCII code equivalent, then it would be shifted again by 1 if not then it would be shifted by 2.
Below is my code so far and it does not compile. I just have no idea on how to have the method include everything from the keyboard as well as accepting user input and moving them accordingly.
```import java.util.Scanner;

public class Ethics
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int sKey = in.nextInt();
Scanner keyboard = new Scanner(System.in);
char code = keyboard.nextLine();
}
public char rotate(char code, int sKey) {
// Keep shift within bounds of alphabet
shiftDistance %= 127;

// Only rotate Latin alphabet and only letters
if ((code >= 'a') && (code <= 'z')) {
code += sKey;

if (code > 'z') {
// correct overflow
code = (char) ((code - 'z' + 'a') - 1);
} else if (code < 'a') {
// correct underflow
code = (char) (('z' + code) - 'a' + 1);
}
} else if ((code >= 'A') && (code <= 'Z')) {
c += shiftDistance;

if (code > 'Z') {
code = (char) ((code - 'Z' + 'A') - 1);
} else if (code < 'A') {
code = (char) (('Z' + code) - 'A' + 1);
}
}

return code;
}
}

System.out.println("The original code is: " + code);
//System.out.println("The new code is: " + sCode );

System.out.println("Would you like to continue?(Y/y/N/n)");
}

}

```

### #6 NormR

• D.I.C Lover

Reputation: 797
• Posts: 6,062
• Joined: 25-December 13

## Re: Simple Encryption Algorithms

Posted 29 June 2016 - 11:09 AM

Quote

it does not compile.

If there are error messages, copy the full text and paste it here so we can see what the problems are.

### #7 jacks94

Reputation: 0
• Posts: 5
• Joined: 29-April 15

## Re: Simple Encryption Algorithms

Posted 29 June 2016 - 11:14 AM

NormR, on 29 June 2016 - 01:09 PM, said:

Quote

it does not compile.

If there are error messages, copy the full text and paste it here so we can see what the problems are.

Ethics.java:43: error: class, interface, or enum expected
System.out.println("The original code is: " + code);
^
Ethics.java:48: error: class, interface, or enum expected
System.out.println("Would you like to continue?(Y/y/N/n)");
^
Ethics.java:49: error: class, interface, or enum expected
}
^
3 errors

### #8 NormR

• D.I.C Lover

Reputation: 797
• Posts: 6,062
• Joined: 25-December 13

## Re: Simple Encryption Algorithms

Posted 29 June 2016 - 11:17 AM

Quote

Ethics.java:43: error: class, interface, or enum expected
System.out.println("The original code is: " + code);
^

That message is usually caused by having statements outside of a method where the compiler is only expecting to find the definition for a class, interface or enum. Make sure the statements at line 43 and following are inside of the {}s that define a method.

This post has been edited by NormR: 29 June 2016 - 11:19 AM

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12680
• Posts: 45,861
• Joined: 27-December 08

## Re: Simple Encryption Algorithms

Posted 29 June 2016 - 11:26 AM

Again- proper indentation of your code would go a long ways in helping you read and debug it. More on code readability.

Also- do you understand the code from the tutorial? The tutorials are there to teach! Better not to include code you don't understand (or can't reproduce without said tutorial).

### #10 g00se

• D.I.C Lover

Reputation: 3648
• Posts: 16,770
• Joined: 20-September 08

## Re: Simple Encryption Algorithms

Posted 29 June 2016 - 03:36 PM

Quote

if any of the new coded characters are represented by an even ASCII code equivalent, then it would be shifted again by 1 if not then it would be shifted by 2.

I'm tired now but surely that adds nothing to the encryption because that step is reverse-engineerable with ease?