9 Replies - 1419 Views - Last Post: 29 June 2016 - 03:36 PM Rate Topic: -----

#1 jacks94   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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) 
   {  
System.out.println("Type your shift key:");
Scanner in = new Scanner(System.in);
int sKey = in.hasNextInt();
System.out.println("Please enter your 5 character code:");
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   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3648
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12680
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3648
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 jacks94   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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) 
   {  
System.out.println("Type your shift key:");
Scanner in = new Scanner(System.in);
int sKey = in.nextInt();
System.out.println("Please enter your 5 character code:");
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)");
   }

}


Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 jacks94   User is offline

  • New D.I.C Head

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

Re: Simple Encryption Algorithms

Posted 29 June 2016 - 11:14 AM

View PostNormR, 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
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12680
  • View blog
  • 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).
Was This Post Helpful? 0
  • +
  • -

#10 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3648
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1