Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package railway;
- import java.util.regex.Pattern;
- import com.sun.org.apache.xml.internal.security.encryption.EncryptedKey;
- import sun.applet.resources.MsgAppletViewer;
- //import com.sun.java_cup.internal.runtime.Scanner;
- import java.util.*;
- public class Railwayproject {
- String msg = new String();
- //initalzing and making key and choice variables global
- int key = 0;
- int choice = 0;
- static Railwayproject r = new Railwayproject();
- //initializing array list varraribles
- //initializing top
- ArrayList<Character> top = new ArrayList<>();
- //initializing bot
- ArrayList<Character> bot = new ArrayList<>();
- //initializing encrypt
- ArrayList<Character> encrypt = new ArrayList<>();
- ArrayList<Character> encrypted = new ArrayList<>();
- ArrayList<Character> decrypted = new ArrayList<>();
- public static void main(String[] args) {
- //creating the title
- System.out.printf("%50s", "Welcome to Rail Fence Cipher!");
- //showing the users the options
- System.out.println("\n\nPlease select one of the following options:");
- System.out.println("");
- //1 is for encryption option
- System.out.println("1 - Encrypt a message");
- //2 is for decryption option
- System.out.println("2 - Decrypt a message");
- //3 is for brute force option
- System.out.println("3 - Brute force");
- //starts
- while (1 == 1) {
- if (r.messageControl(1))break;
- }
- }
- public boolean messageControl(int state) {
- //initializing scanner
- Scanner scan = new Scanner(System.in);
- //initializing boolean
- boolean bol = false;
- //initializing pattern
- Pattern p = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%-]");
- if (state == 1) {
- if (choice == 1 ||choice == 2) {
- bol = false;
- }
- do{
- System.out.println("\nEnter your choice: ");
- try {
- choice = scan.nextInt();
- bol = (choice >= 1 && choice <= 3);
- //decrypt
- if(choice == 2 ) {
- while (1 == 1) {
- if (r.messageControl(2))break;
- }
- while (1 == 1) {
- if (r.messageControl(3))break;
- }
- r.formatEncrypted(msg);
- decrypted = r.decrypt(encrypted);
- }
- //encrypt
- if(choice == 1) {
- while (1 == 1) {
- if (r.messageControl(2))break;
- }
- while (1 == 1) {
- if (r.messageControl(3))break;
- }
- //calling method zig zag
- r.twoArray(top, bot);
- //outputting results
- System.out.println(top);
- System.out.println( bot);
- r.prepareEncrypt(top, bot, encrypt);
- ArrayList<Character> encrypted = r.encrypt(encrypt);
- }
- if(choice == 3 ) {
- //brute force stuff here
- return true;
- }
- if (!bol) {
- System.out.println("Invalid choice! Please enter a number between 1-3.");
- return false;
- }
- }
- catch (Exception e){
- System.out.println("Invalid key! Please enter an integer");
- return false;
- }
- return true;
- }while (bol == false);
- }
- if(state == 2) {
- do{
- System.out.println("Enter your message: ");
- try {
- msg = scan.nextLine();
- if (msg.matches(".*\\d.*") || p.matcher(msg).find()) {
- System.out.println("no nums or special chars");
- return false;
- }
- }
- catch (Exception e){
- System.out.println("Invalid please enter a message");
- return false;
- }
- return true;
- }while (bol == false);
- }
- if(state == 3) {
- do{
- System.out.println("\nEnter the key number : ");
- try {
- key = scan.nextInt();
- bol = (key >= 1 && key <= 26);
- if (!bol) {
- System.out.println("Invalid key! Please enter a number between 1-26.");
- return false;
- }
- }
- catch (InputMismatchException ex){
- System.out.println("Invalid key! Please enter an integer");
- return false;
- }
- return true;
- }while (bol == false);
- }
- return false;
- }
- public void twoArray(ArrayList<Character> top, ArrayList<Character> bot) {
- for (int i = 0; i <= msg.length() - 1; i++)
- {
- if (i == 0) {
- top.add(msg.charAt(i));
- }
- else if (Character.isWhitespace(msg.charAt(i))) {
- if ((i & 1) == 0) {
- top.add(' ');
- top.add(' ');
- }
- else {
- bot.add(' ');
- bot.add(' ');
- }
- }
- else if ((i & 1) == 0) {
- top.add(' ');
- top.add(msg.charAt(i));
- }
- else {
- bot.add(' ');
- bot.add(msg.charAt(i));
- }
- }
- if (bot.size() > top.size())top.add(' ');
- else bot.add(' ');
- }
- public void prepareEncrypt(ArrayList<Character> top, ArrayList<Character> bot, ArrayList<Character> encrypt){
- for (int i = 0; i <= top.size() - 1; i++) {
- if (Character.isWhitespace(top.get(i))){
- if (Character.isWhitespace(bot.get(i))){
- encrypt.add(' ');
- }
- }
- else {
- encrypt.add(top.get(i));
- }
- }
- for (int i = 0; i <= bot.size() - 1; i++) {
- if (Character.isWhitespace(bot.get(i))){
- if (Character.isWhitespace(top.get(i))){
- encrypt.add(' ');
- }
- }
- else {
- encrypt.add(bot.get(i));
- }
- }
- }
- public ArrayList<Character> encrypt(ArrayList<Character> encrypt) {
- ArrayList<Character> encrypted = new ArrayList<>();
- for (int i=0; i<encrypt.size(); i++)
- {
- if (Character.isWhitespace(encrypt.get(i))){
- encrypted.add(' ');
- }
- else if (Character.isUpperCase(encrypt.get(i)))
- {
- char ch = (char)(((int)encrypt.get(i) +
- key - 65) % 26 + 65);
- encrypted.add(ch);
- }
- else
- {
- char ch = (char)(((int)encrypt.get(i) +
- key - 97) % 26 + 97);
- encrypted.add(ch);
- }
- }
- System.out.println("Your encrypted message is:");
- System.out.println(encrypted);
- return encrypted;
- }
- public void formatEncrypted(String msg) {
- for (int i = 0; i <= msg.length() - 1; i++) {
- if (Character.isWhitespace(msg.charAt(i))) {
- encrypted.add(' ');
- }
- else encrypted.add(msg.charAt(i));
- }
- }
- public ArrayList<Character> decrypt(ArrayList<Character> encrypted) {
- ArrayList<Character> decrypted = new ArrayList<>();
- if (top.size() > bot.size())bot.add(' ');
- else top.add(' ');
- for (int i=0; i<encrypted.size(); i++)
- {
- if (Character.isWhitespace(encrypted.get(i))){
- decrypted.add(' ');
- }
- else if (Character.isUpperCase(encrypted.get(i)))
- {
- char ch = (char)(((int)encrypted.get(i) +
- (-key) - 65) % 26 + 65);
- decrypted.add(ch);
- }
- else
- {
- char ch = (char)(((int)encrypted.get(i) +
- (-key) - 97) % 26 + 97);
- decrypted.add(ch);
- }
- }
- System.out.println("Your decrpyed message is:");
- System.out.println(decrypted);
- return decrypted;
- }
- public void formatMessage(ArrayList<Character> decrypted) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement