Last active
December 19, 2015 22:08
-
-
Save m0rjc/73289d8084b2fcf1bbfa to your computer and use it in GitHub Desktop.
Code used trying to work out a cipher for Cyber Security Challenge
This file contains 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
package m0rjc.test; | |
import java.io.UnsupportedEncodingException; | |
import java.net.URLDecoder; | |
/** | |
* Decode the hex string from a Cyber Security Challenge question. | |
* This code is enough to get the job done, rather than something neat. | |
* | |
* @author Richard Corfield <[email protected]> | |
*/ | |
public class Decoding | |
{ | |
/** | |
* Base64 decode result from http://www.base64decode.org/ | |
*/ | |
private static final String HEX = "53 23 e2 13 d2 77 a6 14 03 23 52 64 84 94 83 53 b4 84 73 03 23 52 a6 36 a6 e2 17 17 66 26 a6 77 e6 b6 d2 37 e6 26 84 53 52 66 97 66 94 03 23 52 37 47 e6 97 66 86 e6 17 57 57 64 84 53 52 15 37 47 97 17 e6 27 66 d4 84 53 52 64 33 52 84 03 23 52 a4 a5 15 74 d2 25 75 a4 95 d2 95 35 e4"; | |
public static void main(final String[] args) throws UnsupportedEncodingException | |
{ | |
// Initially I'd swapped nibbles, decoded then noted it had to be reversed. | |
// This way is shorter. | |
String input = swapNibblesAndDecodeHex(); | |
System.out.println("Reverse, -> ASCII: " + input); | |
// First pass tried all values for n to see if any looked interesting, | |
// though given I needed to make %5H valid it needed to be a small back step. | |
String rot21 = rotn(input.toString(), 21); | |
System.out.println("ROT-21: " + rot21); | |
String urlDecoded = URLDecoder.decode(rot21); | |
System.out.println("URL Decoded: " + urlDecoded); | |
} | |
/** | |
* Given the constant HEX, swap the nibbles and convert to characters. | |
* @return ASCII text from the HEX data. | |
*/ | |
private static String swapNibblesAndDecodeHex() | |
{ | |
StringBuilder sb1 = new StringBuilder(); | |
for(String hex : reverse(HEX).split(" ")) | |
{ | |
sb1.append((char)Integer.parseInt(hex, 16)); | |
} | |
return sb1.toString(); | |
} | |
/** | |
* Reverse a string. | |
* @param forward | |
* @return drawrof | |
*/ | |
private static String reverse(final String forward) | |
{ | |
StringBuilder reversed = new StringBuilder(forward.length()); | |
for (char ch : forward.toCharArray()) | |
{ | |
reversed.insert(0, ch); | |
} | |
return reversed.toString(); | |
} | |
/** | |
* Apply the "ROT-n" Ceaser Cipher to the given string. Only applies to alphabetical characters. | |
* @param input input text | |
* @param n amount of places to rotate | |
* @return decoded text. | |
*/ | |
private static String rotn(final String input, final int n) | |
{ | |
StringBuilder sb = new StringBuilder(input.length()); | |
for (char ch : input.toCharArray()) | |
{ | |
if (Character.isLetter(ch)) | |
{ | |
if (Character.isUpperCase(ch)) | |
{ | |
ch = (char) (((ch - 'A' + n) % 26) + 'A'); | |
} | |
else | |
{ | |
ch = (char) (((ch - 'a' + n) % 26) + 'a'); | |
} | |
} | |
sb.append(ch); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment