Skip to content

Instantly share code, notes, and snippets.

@benjholla
Created September 11, 2014 19:43
Show Gist options
  • Save benjholla/f049c464d09550bd6b35 to your computer and use it in GitHub Desktop.
Save benjholla/f049c464d09550bd6b35 to your computer and use it in GitHub Desktop.
Dataflow laundering with exceptions
/**
* A toy example of laundering data through the use of the program stack and exception control flow paths
* The input data drives how two methods recursively call each other. If the next input value is a 0 then
* the _0 method is called, likewise a 1 calls the _1 method. When there is no more data an Exception is thrown
* with the following stack trace. The Exception is caught and the stack trace is used to recover the data.
*
* java.lang.StringIndexOutOfBoundsException: String index out of range: 0
* at java.lang.String.charAt(String.java:658)
* at ExceptionalLaunder._0(ExceptionalLaunder.java:39)
* at ExceptionalLaunder._1(ExceptionalLaunder.java:48)
* at ExceptionalLaunder._0(ExceptionalLaunder.java:42)
* at ExceptionalLaunder._1(ExceptionalLaunder.java:48)
* at ExceptionalLaunder.launder(ExceptionalLaunder.java:22)
* at ExceptionalLaunder.main(ExceptionalLaunder.java:12)
*
*@author Ben Holland
*/
public class ExceptionalLaunder {
public static void main(String[] args) {
String x = "1010";
String y = launder(x);
System.out.println(y + " is a laundered version of " + x);
}
public static String launder(String data) {
String result = "";
try {
if (data.charAt(0) == '0'){
_0(data.substring(1));
} else{
_1(data.substring(1));
}
} catch (Exception e) {
e.printStackTrace();
StackTraceElement[] stack = e.getStackTrace();
StringBuilder temp = new StringBuilder("");
for(StackTraceElement element : stack){
if(element.getMethodName().substring(0,1).equals("_")){
temp.append(element.getMethodName().replace("_", ""));
}
}
result = temp.reverse().toString();
}
return result;
}
private static void _0(String data) {
if (data.charAt(0) == '0'){
_0(data.substring(1));
} else {
_1(data.substring(1));
}
}
private static void _1(String data) {
if (data.charAt(0) == '0'){
_0(data.substring(1));
} else {
_1(data.substring(1));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment