PDA

Click to See Complete Forum and Search --> : Probably A Simple Problem...


kitsune
Feb 8th, 2004, 09:43 PM
I've written the following code...The encrypt() method is supposed to read text from a file and encode it by shifting each letter three to the right. Decrypt() is supposed to decrypt in a similar way, by shifting each letter three to the left. The encrypt() method works just fine, and the decrypt() almost works...Instread of returning 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' when I enter the appropriate encoded string, it returns 'ABCDEFGHIJKLMNOPQRSTUVW>?@'

protected void encrypt(String ipfile, String opfile) throws IOException
{
StringBuffer contents = new StringBuffer();
StringBuffer newContents = new StringBuffer();
BufferedReader input = null;
Writer output = null;
char letter, newLetter;
int uni, answer;

try
{
input = new BufferedReader(new FileReader(ipfile) );
String line = null;
output = new BufferedWriter( new FileWriter(opfile) );

while ((line = input.readLine()) != null)
{
contents.append(line);

for (int i = 0; i < contents.length(); i++)
{
letter = contents.charAt(i);
uni = (int)letter;

if (uni == 32)
{
answer = 32;
}
else if (uni == 13)
{
answer = 13;
}
else
{
answer = (((uni - 65) + 3)%26) + 65;
}
newLetter = (char)answer;
newContents.append(newLetter);
}
newContents.append(System.getProperty("line.separator"));
contents.delete(0, contents.length());
}
String newContentString = newContents.toString();
output.write(newContentString);
}
catch (IOException e)
{
System.out.println("I/O Error: " + e.getMessage());
System.exit(1);
}
finally
{
if (output != null) output.close();
}
}

protected void decrypt(String ipfile, String opfile) throws IOException
{
StringBuffer contents = new StringBuffer();
StringBuffer newContents = new StringBuffer();
BufferedReader input = null;
Writer output = null;
char letter, newLetter;
int uni, answer;

try
{
input = new BufferedReader(new FileReader(ipfile) );
String line = null;
output = new BufferedWriter( new FileWriter(opfile) );

while ((line = input.readLine()) != null)
{
contents.append(line);

for (int i = 0; i < contents.length(); i++)
{
letter = contents.charAt(i);
uni = (int)letter;

if (uni == 32)
{
answer = 32;
}
else if (uni == 13)
{
answer = 13;
}
else
{
answer = (((uni - 65) - 3)%26) + 65;
}
newLetter = (char)answer;
newContents.append(newLetter);
}
newContents.append(System.getProperty("line.separator"));
contents.delete(0, contents.length());
}
String newContentString = newContents.toString();
output.write(newContentString);
}
catch (IOException e)
{
System.out.println("I/O Error: " + e.getMessage());
System.exit(1);
}
finally
{
if (output != null) output.close();
}
}

Can anybody see my mistake?

sql_lall
Feb 9th, 2004, 02:32 AM
a tricky one this, gets lots of people in competitions...

the % thing, for negative numbers, gives negative results...
i.e. -3 % 7 = -3, -24 % 5 = -4...

so, when you get this line:
answer = (((uni - 65) - 3)%26) + 65;

and you are decrypting 'A' (=65), this happens:

answer = (-3 % 26) + 65 = -3 + 65 = 62
and, if you look at ASCII 62, 63 and 64: >?@ !!!!

so, just make it (((uni - 65) + 75)%26) + 65;

or even something bigger than 75, just that 75%26 = -3%26, This will assure you it will be positive!

Oh, btw, i have seen try...catch before, but never try...catch...finally?!?! what does this do?