Skip to content

Instantly share code, notes, and snippets.

@aravindc26
Created August 18, 2012 18:07
Show Gist options
  • Save aravindc26/3388783 to your computer and use it in GitHub Desktop.
Save aravindc26/3388783 to your computer and use it in GitHub Desktop.
import java.util.*;
class Pair {
private final int left;
private final int right;
public Pair(int left, int right) {
this.left = left;
this.right = right;
}
public int getLeft() { return left; }
public int getRight() { return right; }
}
public class PlainTextAttack {
public String getwordSets(String plainText,String cipherText)
{
ArrayList listOfPairsPos = new ArrayList();
ArrayList listOfPairCrypt = new ArrayList();
//finding position of equal pairs
for(int i = 0; i < plainText.length(); i++){
for(int j = i + 1; j < plainText.length(); j++) {
if(plainText.charAt(i) == plainText.charAt(j)){
listOfPairsPos.add(new Pair(i,j));
break;
}
}
}
int res = 0;
int j;
for(int i = 0; i < cipherText.length(); i++) {
for(j = i; j < plainText.length(); j++) {
for(int k = j + 1; k < plainText.length(); k++) {
if(cipherText.charAt(j) == plainText.charAt(k)){
listOfPairCrypt.add(new Pair(j,k));
break;
}
}
}
if(listOfPairsPos.equals(listOfPairCrypt)) {
res = j;
break;
}
}
String resString = cipherText.substring(res, plainText.length());
if(resString != null){
return resString;
}
return null;
}
public static void main(String[] args){
//TestCase 1
try{
String plainText="AMMUNITION";
String cipherText="ASQWDFDFCOOWPKVKQPFGDFGDGCXVRFTWEA";
PlainTextAttack obj = new PlainTextAttack();
String output= obj.getwordSets(plainText,cipherText);
System.out.println("TestCase 1");
if(output!=null)
System.out.println(output);
else
System.out.println("NULL");
}
catch(Exception e){
e.printStackTrace();
}
//TestCase 2
try{
String plainText="AMMUNITION";
String cipherText="QWERTYUIOPLKJHGFDSAZXCV";
PlainTextAttack obj = new PlainTextAttack();
String output= obj.getwordSets(plainText,cipherText);
System.out.println("TestCase 2");
if(output!=null)
System.out.println(output);
else
System.out.println("NULL");
}
catch(Exception e){
e.printStackTrace();
}
//TestCase 3
try{
String plainText="AMMUNITION";
String cipherText="SDLKFJSDLKFJLJRLWRWEJRWLEJRWLERJs";
PlainTextAttack obj = new PlainTextAttack();
String output= obj.getwordSets(plainText,cipherText);
System.out.println("TestCase 3");
if(output!=null)
System.out.println(output);
else
System.out.println("NULL");
}
catch(Exception e){
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment