Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fixedxor;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- public class FixedXor {
- private String someString;
- public FixedXor(String someString) {
- this.someString = someString;
- }
- public ArrayList<Integer> stringToHex() {
- ArrayList<Integer> asciiList = new ArrayList<Integer>();
- for(int i=0; i<someString.length(); i++) {
- //asciiList.add((int)word.charAt(i));
- asciiList.add(hexTable(someString.toUpperCase().charAt(i)));
- }
- return asciiList;
- }
- private int hexTable(Character c) {
- Map<Character, Integer> table = new HashMap<Character, Integer>();
- table.put('0', 0);
- table.put('1', 1);
- table.put('2', 2);
- table.put('3', 3);
- table.put('4', 4);
- table.put('5', 5);
- table.put('6', 6);
- table.put('7', 7);
- table.put('8', 8);
- table.put('9', 9);
- table.put('A', 10);
- table.put('B', 11);
- table.put('C', 12);
- table.put('D', 13);
- table.put('E', 14);
- table.put('F', 15);
- return table.get(c);
- }
- public ArrayList<String> hexToBinary(ArrayList<Integer> hex){
- ArrayList<String> binary = new ArrayList<String>();
- for(int i=0; i<hex.size(); i++) {
- if(hex.get(i) == 0) {
- binary.add("0000");
- }else if(hex.get(i) == 1) {
- binary.add("0001");
- }else {
- binary.add(convertToBinary(hex.get(i)));
- }
- }
- return binary;
- }
- public String convertToBinary(int number) {
- String binary = "";
- while (number != 1) {
- binary += number % 2;
- number = number / 2;
- }
- binary += number % 2;
- if(binary.length() == 2) {
- //return "0"+new StringBuilder(binary).reverse().toString();
- binary = binary+"00";
- }else if(binary.length() == 3) {
- binary = binary+"0";
- }
- return new StringBuilder(binary).reverse().toString();
- }
- public String binaryCombiner(ArrayList<String> binary) {
- String combinedBinary = "";
- for (String binaries : binary) {
- combinedBinary += binaries;
- }
- return combinedBinary;
- }
- public static ArrayList<String> fourDigitBinary(String binaryXored) {
- ArrayList<String> fourValuedBinary = new ArrayList<String>();
- String fourBinary = "";
- int count = 0;
- for(int i=0; i<binaryXored.length(); i++) {
- if(count != 4) {
- fourBinary += binaryXored.charAt(i);
- count++;
- }else {
- fourValuedBinary.add(fourBinary);
- count = 0;
- fourBinary = "";
- i-=1;
- }
- }
- fourValuedBinary.add(fourBinary);
- return fourValuedBinary;
- }
- public static ArrayList<String> binaryToBytes(ArrayList<String> fourBinary){
- ArrayList<String> bytes = new ArrayList<String>();
- for (String binaries : fourBinary) {
- int count = 3;
- int byteValue = 0;
- for(int i=0; i<binaries.length(); i++) {
- if(i == 3 && binaries.charAt(i) == '1') {
- byteValue += 1;
- }
- else if(binaries.charAt(i) == '1') {
- byteValue += Math.pow(2, count);
- }
- count--;
- }
- bytes.add(String.valueOf(byteValue));
- }
- return bytes;
- }
- public static void main(String[] args) {
- String binaryXored = "";
- String HexValue = "";
- //String finalHexValue = "";
- String[] hextable = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
- FixedXor fx = new FixedXor("1c0111001f010100061a024b53535009181c");
- ArrayList<Integer> hexValues = fx.stringToHex();
- ArrayList<String> binaries = fx.hexToBinary(hexValues);
- String combinedBinary1 = fx.binaryCombiner(binaries);
- FixedXor fx1 = new FixedXor("686974207468652062756c6c277320657965");
- ArrayList<Integer> hexValue1 = fx1.stringToHex();
- ArrayList<String> binaries1 = fx1.hexToBinary(hexValue1);
- String combinedBinary2 = fx1.binaryCombiner(binaries1);
- for(int i=0; i<combinedBinary1.length(); i++) {
- if((combinedBinary1.charAt(i) == '0' && combinedBinary2.charAt(i) == '0') ||
- (combinedBinary1.charAt(i) == '1' && combinedBinary2.charAt(i) == '1')) {
- binaryXored += '0';
- }else {
- binaryXored += '1';
- }
- }
- ArrayList<String> hexDecoded = fourDigitBinary(binaryXored);
- ArrayList<String> bytes = binaryToBytes(hexDecoded);
- for (String s : bytes) {
- HexValue += hextable[Integer.valueOf(s)];
- }
- System.out.println(HexValue);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement