Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MiniEncryption {
- /**
- * @param args the command line arguments
- */
- private static Scanner input = new Scanner(System.in);
- public static void main(String[] args) {
- String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.!";
- String userInput = "";
- int i = 0;
- while (userInput.length() < characters.length()) {
- System.out.format("Map for %s ", characters.charAt(i));
- char charainput = input.nextLine().toUpperCase().charAt(0);
- if (!checkDuplicate(userInput, charainput) && checkValues(characters, charainput)) {
- userInput += charainput;
- i++;
- } else {
- System.out.println("wrong value please try again");
- }
- }
- while (true) {
- System.out.println("Mapping complete enter encrypted word");
- String word = input.nextLine();
- String encrypted = encryptWord(characters, userInput, word);
- String decrypted = decryptword(characters, userInput, word);
- System.out.println("the word encrypted is " + encrypted);
- System.out.println("the word decrypted is " + decrypted);
- System.out.println("Do you wish to try again? (y/n)");
- char answer = input.nextLine().toUpperCase().charAt(0);
- if (answer == 'N') {
- System.out.println("thank you");
- break;
- }
- }
- }
- public static boolean checkDuplicate(String Usercharacters, char input) {
- for (int i = 0; i < Usercharacters.length(); i++) {
- if (Usercharacters.contains(Character.toString(input))) {
- return true;
- }
- }
- return false;
- }
- public static boolean checkValues(String Mapcharacters, char input) {
- for (int i = 0; i < Mapcharacters.length(); i++) {
- if (!Mapcharacters.contains(Character.toString(input))) {
- return true;
- }
- }
- return false;
- }
- public static String encryptWord(String characters, String usercharacters, String word) {
- String encryptword = "";
- for (int i = 0; i < word.length(); i++) {
- int index = characters.indexOf(word.charAt(i));
- encryptword += usercharacters.charAt(index);
- }
- return encryptword;
- }
- public static String decryptword(String characters, String usercharacters, String word) {
- String decryptword = "";
- for (int i = 0; i < word.length(); i++) {
- int index = usercharacters.indexOf(word.charAt(i));
- decryptword += characters.charAt(index);
- }
- return decryptword;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement