Results 1 to 2 of 2

Thread: Probably A Simple Problem...

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    Illinois
    Posts
    9

    Probably A Simple Problem...

    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>?@'

    Code:
    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?

  2. #2
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    hehehe...

    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?
    sql_lall

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width