Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test.padencryption;
- import java.util.regex.*;
- import java.util.*;
- public class StringEncrypt
- {
- String s1, s2, result;
- static boolean isValid(String test)
- {
- if(test.matches("([0-9A-F])+")) //Regex expression for hexadecimal number
- return true;
- else
- return false;
- }
- public String encrypt(String data, String pad)
- {
- int x1 = Integer.parseInt(data, 16); //Parse string into hex integer.
- int x2 = Integer.parseInt(pad, 16);
- int x3 = x1 ^ x2; //XORing
- String res = String.format("%06x", x3); //Format string. Pad with 0s if required, min width 6.
- res = res.replace('a', 'A');
- res = res.replace('b', 'B');
- res = res.replace('c', 'C');
- res = res.replace('d', 'D');
- res = res.replace('e', 'E');
- res = res.replace('f', 'F');
- return res;
- }
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- StringEncrypt obj = new StringEncrypt();
- System.out.println("Enter the first string : ");
- obj.s1 = input.next();
- System.out.println("Enter the second string: ");
- obj.s2 = input.next();
- //Check for validity. Strings must be equal length.
- if((isValid(obj.s1)) && (isValid(obj.s2)) && (obj.s1.length() == obj.s2.length()))
- {
- obj.result = obj.encrypt(obj.s1, obj.s2);
- System.out.println("The result is = " + obj.result);
- }
- else
- System.out.println("Invalid entries");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement