GS-1719's Doomworld Forums Blog

GS-1719's Doomworld Forums Blog

Systematic approach to code June 25, 2003, 10:34 am
Here is a simple crude and inefficient java program to solve these codes:
// GS-1719

import java.awt.*;
import java.awt.event.*;

/**
* The decode() function is basically translated directly from Phobos Lab.
*/
class decode extends Frame
{
private Label origLabel,
keyLabel,
resLabel;

private TextField origInput,
keyInput,
resOutput;

private Button decodeButton, sysButton;

private static boolean reverse = true;

public decode()
{
origLabel = new Label("Original code:"); // height stuff
keyLabel = new Label("Key");
resLabel = new Label("Result");

origInput = new TextField(20);
keyInput = new TextField(20);
resOutput = new TextField(20);

setLayout(new GridLayout(4, 2));

decodeButton = new Button(" Decode ");
sysButton = new Button(" Decode systematically ");
decodeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
validateKey();
decodeString();
}
});
sysButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
sysDec();
}
});
setSize(400, 150);

origInput.setText("ELUZYIXKJEEKSCVGHGZK");
keyInput.setText("WYMGQIEC");

add(origLabel); add(origInput); add(keyLabel); add(keyInput); add(resLabel);
add(resOutput); add(decodeButton); add(sysButton); setVisible(true);
}

public void decodeString()
{
int GuessLen = 0, CodeLen = 0, CodeChResultCh = 0, CodeCh = 0, GuessCh = 0, ResultCh = 0;
StringBuffer result = new StringBuffer("");

GuessLen = (origInput.getText()).length();
CodeLen = (keyInput.getText()).length();
resOutput.setText("");

for (int i = 0; i < CodeLen; i++)
{
CodeCh = Character.getNumericValue(Character.toUpperCase((origInput.getText()).charAt(i)));
GuessCh = Character.getNumericValue(Character.toUpperCase((keyInput.getText()).charAt(i % GuessLen)));

ResultCh = 65 + ((26 + CodeCh - GuessCh) % 26);
result.append(Character.toUpperCase((char)ResultCh));
}

resOutput.setText(result.toString());
}

private static String possibleStr[] = {"DEIMOS", "ATTACK", "UNION", "AEROSPACE", "CORPORATION",
"DOOM", "III", "PHOBOS", "LAB", "LOST", "CONTACT", "MARINE",
"SPACE"};

// will attempt to produce strings of the same length, otherwise performance
// will be on the order O(n!) which is very bad.
public void sysDec()
{
String currentGuess;
for(int i = 0; i < possibleStr.length; i++)
{
currentGuess = new String(possibleStr);

if(currentGuess.length() < origInput.getText().length())
{
sysDec(currentGuess);
}
else
{
test(currentGuess);
}
}
}

public void sysDec(String current)
{
for(int i = 0; i < possibleStr.length; i++)
{
if(current.length() < origInput.getText().length() &&
current.length() + possibleStr.length() < origInput.getText().length())
{
sysDec(new String(current + possibleStr));
}
else if(current.length() + possibleStr.length() == origInput.getText().length())
{
test(new String(current + possibleStr));
}
}
}

private void test(String str)
{
if(reverse)
{
str = itReverse(str);
}

keyInput.setText(str);
decodeString();
System.out.println("Keyword: " + str + "\tOutput: " + resOutput.getText());
}

public static void main(String args[])
{
decode d = new decode();
}

public static String itReverse(String s)
{
StringBuffer in = new StringBuffer(s);
int lr = 0, rl = in.length() - 1;
while(lr <= rl)
{
char temp = in.charAt(rl);
in.setCharAt(rl,in.charAt(lr));
in.setCharAt(lr, temp);
lr++; rl--;
}
return new String(in);
}

private void validateKey()
{
int i = 0, l = 0;;

if((i = keyInput.getText().length()) < (l = origInput.getText().length()))
{
String str = keyInput.getText();
StringBuffer strB = new StringBuffer(keyInput.getText());
int j = i;
while(i < l)
{
strB.append(str.charAt(i % j));
i++;
}
keyInput.setText(strB.toString());
}
}
}


Note: For the systematic approach, the strings to be searched are stored in the array possibleStr, and the output is in the console. A reverse variable determines whether the string will be turned backwards.
0 Comments

Next