Skip to content

Instantly share code, notes, and snippets.

@shivdaryanani
Created November 16, 2018 15:54
Show Gist options
  • Save shivdaryanani/d162fda98dfb61c83459f0343703ea34 to your computer and use it in GitHub Desktop.
Save shivdaryanani/d162fda98dfb61c83459f0343703ea34 to your computer and use it in GitHub Desktop.
MAIN CLASS
import java.util.Scanner;
public class main
{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
String choice = "";
while (!choice.equals("end")){
System.out.println("Would you like to Encrypt or Decrypt?");
choice = scan.nextLine();
if(choice.equals("encrypt")){
System.out.println("ENTER SOMETHING TO ENCRYPT:");
String secret = scan.nextLine();
Encryptor.encrypt(secret);
}
else if (choice.equals("decrypt")){
System.out.println("ENTER SOMETHING TO DECRYPT:");
String desecret = scan.nextLine();
Decryptor.decrypt(desecret);
}
}
}
}
ENCRYPTION CLASS
public class Encryptor {
public static void encrypt(String s){
String newStr = ""; //Empty string for later use
for(int i=0; i<s.length(); i++){ //makes code run for each char
char cha = s.charAt(i); //Sets each individual char to var
if (cha == ' '){ //for spaces
String newOne = Character.toString((char)cha);//char-string
newStr = newStr+newOne; //this adds my new created above string to the other one
}
else {
int OrgCha = (int)cha;
int OrgChara = OrgCha += 4; // shifting the letter 4
if (OrgChara > 'z'){ //For letters w x y z
OrgChara -= 26;
}
char OrgChar = (char)OrgChara; //int of new letter to character
String newOne = Character.toString((char)OrgChar);
newStr = newStr+newOne; //adds two strings
}
}
System.out.println(newStr); //print string
}
}
DECRYPTION CLASS
public class Decryptor
{
public static void decrypt(String s){
String newStr = "";
for(int i=0; i<s.length(); i++){
char cha = s.charAt(i);
if (cha == ' '){
String newOne = Character.toString((char)cha);
newStr = newStr+newOne;
}
else {int OrgCha = (int)cha;
int OrgChara = OrgCha -= 4;
if (OrgChara < 'a'){
OrgChara += 26;
}
char OrgChar = (char)OrgChara;
String newOne = Character.toString((char)OrgChar);
newStr = newStr+newOne;
}
}
System.out.println(newStr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment