Last active
January 2, 2020 17:29
-
-
Save AnujJha-stack/826db520a5c17bdb1cf04821f611068e to your computer and use it in GitHub Desktop.
Information Security.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // coded by Anuj Jha | |
| // Caesar Cipher encryption-decryption | |
| //caution : avoid special character. | |
| import java.util.Scanner; | |
| public class CaesarCipher { | |
| static int[] encryption(int[] charArray, int key){ | |
| for(int i = 0; i < charArray.length;i++){ | |
| if(charArray[i] <=90) //for UPPERCASE | |
| charArray[i] = ((charArray[i] + key -65) % 26) + 65; | |
| else //for lowercase | |
| charArray[i] = ((charArray[i] + key -97) % 26) + 97; | |
| } | |
| return charArray; | |
| } | |
| static int[] decryption(int[] charArray, int key){ | |
| for(int i = 0; i < charArray.length;i++){ | |
| if(charArray[i] <=90) | |
| charArray[i] = ((charArray[i] - key -65 + 26) % 26) + 65; | |
| else | |
| charArray[i] = ((charArray[i] - key -97 + 26) % 26) + 97; | |
| } | |
| return charArray; | |
| } | |
| /** | |
| * Function that generates array of integer type for each char with their equivalent ASCII value. | |
| *to generates array of char type use the syntax below:- | |
| * char[] charArray = new char[val.length()]; | |
| **/ | |
| static int[] charArrayGenerator(String val){ | |
| int[] charArray = new int[val.length()]; | |
| for(int i = 0; i < val.length(); i++) | |
| charArray[i] = val.charAt(i); | |
| return charArray; | |
| } | |
| //print Array by converting ASCII to respective character value. | |
| static void printArray(int[] temp){ | |
| for (int value : temp) System.out.print((char)value +" "); | |
| } | |
| public static void main(String[] args){ | |
| Scanner input = new Scanner(System.in); | |
| while(true){ | |
| System.out.print("\nENTER YOUR TEXT : "); | |
| String val = input.next(); | |
| System.out.print("ENTER YOUR KEY : "); | |
| int key = input.nextInt(); | |
| int[] charArray = charArrayGenerator(val); | |
| System.out.print("\n 1. PLAIN TEXT --> CIPHERED TEXT" + | |
| "\n 2. CIPHERED TEXT --> ORIGINAL TEXT \n Other to EXIT\n"); | |
| System.out.print("\n Enter choice : "); | |
| int choice = input.nextInt(); | |
| switch (choice){ | |
| case 1: | |
| System.out.print("\nPLAIN TEXT : "); | |
| printArray(charArray); | |
| int[] cipheredTextArray = encryption(charArray,key); | |
| System.out.print("\nCIPHERED TEXT : "); | |
| printArray(cipheredTextArray); | |
| break; | |
| case 2: | |
| System.out.print("\nCIPHERED TEXT : "); | |
| printArray(charArray); | |
| int[] plainTextArray = decryption(charArray,key); | |
| System.out.print("\nPLAIN TEXT : "); | |
| printArray(plainTextArray); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class monoalphabetics { | |
| private static void printArray(char[] arr){ | |
| for(int i = 0; i < arr.length; i++){ | |
| System.out.print(""+arr[i]); | |
| } | |
| } | |
| public static void main(String[] args){ | |
| Scanner input = new Scanner(System.in); | |
| char[] PlAr = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' '}; | |
| char[] ChAr = {'b','d','f','h','j','l','n','p','r','t','v','x','z','a','c','e','g','i','k','m','o','q','s','u','w','y',' '}; | |
| System.out.print("Enter Plain Text: "); | |
| String PT = input.nextLine(); | |
| char[] CT1 = new char[PT.length()]; | |
| for(int i = 0; i < PT.length(); i++){ | |
| for(int j = 0; j < ChAr.length; j++){ | |
| if (PT.charAt(i) == PlAr[j]) { | |
| CT1[i] = ChAr[j]; | |
| } | |
| } | |
| } | |
| System.out.print("\nCipher Text : "); | |
| printArray(CT1); | |
| System.out.print("\nEnter Cipher text :"); | |
| String s1 = input.nextLine(); | |
| char[] res = new char[s1.length()]; | |
| for(int i = 0; i < s1.length(); i++){ | |
| for(int j = 0; j < ChAr.length; j++){ | |
| if (s1.charAt(i) == ChAr[j]) { | |
| res[i] = PlAr[j]; | |
| } | |
| } | |
| } | |
| System.out.print("\nOriginol Text : "); | |
| printArray(res); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class monoalphabeticsAttack { | |
| private static void printArray(char[] arr){ | |
| for(int i = 0; i < arr.length; i++){ | |
| System.out.print(""+arr[i]); | |
| } | |
| } | |
| private static int charCount(char[] arr, char ch){ | |
| int count = 0; | |
| for(int i = 0; i < arr.length; i++){ | |
| if(ch == arr[i]){ | |
| count = count + 1; | |
| } | |
| } | |
| return count; | |
| } | |
| public static void main(String[] args){ | |
| Scanner input = new Scanner(System.in); | |
| char[] plainTextArr = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ',',','.','-'}; | |
| char[] cipherTextArr = {'b','d','f','h','j','l','n','p','r','t','v','x','z','a','c','e','g','i','k','m','o','q','s','u','w','y',' ',',','.','-'}; | |
| System.out.print("Enter Plain Text: "); | |
| String PT = input.nextLine(); | |
| char[] CT = new char[PT.length()]; | |
| for(int i = 0; i < PT.length(); i++){ | |
| for(int j = 0; j < cipherTextArr.length; j++){ | |
| if (PT.charAt(i) == plainTextArr[j]) { | |
| CT[i] = cipherTextArr[j]; | |
| } | |
| } | |
| } | |
| System.out.print("\nCipher Text : "); | |
| printArray(CT); | |
| System.out.print("\n"); | |
| //Counter | |
| for(int i = 0; i < CT.length; i++){ | |
| System.out.print("\n"+CT[i]+": "+charCount(CT,CT[i])); | |
| } | |
| } | |
| } |
Author
AnujJha-stack
commented
Dec 18, 2019

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment