[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

Parse special characters: Servlet/xml

Asked by istiaquem in Java Servlets, Extensible Stylesheet Language Transformation (XSLT), Parsers

Tags: java/ xml

Hi experts,
I have this requirement where  I need to do implement XOR logic with the following.

Time = decrypt("ÕÒÝÚÚÐØßÜ","åæçèéêëìíîï");

it works fine with alpha numeric like -- decrypt("qr35%&5675","reg345*453"); But not working with special characters like ("ÕÒÝÚÚÐØßÜ","åæçèéêëìíîï");

Here is my decrypt code

private String decrypt(String taCryptText, String taKeyText)
      {
            int i = 0;
            int iCryptLength = 0;
            int iCryptTextLength = 0;
            int iKeyLength = 0;
            int iKeyTextLength = 0;
            int iDelta = 0;
            int iTemp = 0;
            char cTemp = 0;
            char cTemp0 = 0;
            String sTemp = null;
            String sCryptText = null;
            String sKeyText = null;
            String sPlainText = null;
            String taPlainText = null;
            StringBuffer sbTemp = null;
            int iCrypt[] = null;
            int iCryptBuffer[] = null;
            int iKey[] = null;
            int iKeyBuffer[] = null;
            boolean bNumericKey = false;
            boolean bNumericCrypt = false;

            sCryptText = taCryptText;
            sKeyText = taKeyText;

            iCryptTextLength = sCryptText.length();
            iKeyTextLength = sKeyText.length();

            System.out.println("Key is :"+sKeyText);

            System.out.println("sCryptText:"+sCryptText);
            System.out.println("iKeyTextLength:"+iKeyTextLength);

            // Find out if the crypttext is numeric:
            if (numericP(iCryptTextLength, sCryptText))
            {
                  bNumericCrypt = true;
                  System.out.println("The crypt text is numeric.");
                  // Fill the crypt array:
                  iCrypt = new int[iCryptTextLength];                      
                  // More than enough for the crypt integers.
                  i = 0;
                  while (i < iCryptTextLength)
                  {
                        cTemp = 0;
                        sbTemp = new StringBuffer();
                        while (cTemp != ' ' && i < iCryptTextLength)
                        {
                              try
                              {
                                    cTemp = sCryptText.charAt(i);
                              }
                              catch (StringIndexOutOfBoundsException e0)
                              {
                                    System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (crypt).");
                              }
                              if (cTemp != ' ')
                              {
                                    sbTemp.append(cTemp);
                              }
                              i++;
                        }
                        iCrypt[iCryptLength] = Integer.parseInt(sbTemp.toString());
                        iCryptLength++;
                  }
            }
            else
            {
                  iCryptLength = iCryptTextLength;
                  System.out.println("The crypt text is not numeric, iCryptLength = " + iCryptLength + ".");
            }

            // Find out if the key is numeric:
            if (numericP(iKeyTextLength, sKeyText))
            {
                  bNumericKey = true;
                  System.out.println("The key is numeric.");
                  // Fill the key array:
                  iKey = new int[iKeyTextLength];
                  i = 0;
                  while (i < iKeyTextLength)
                  {
                        cTemp = 0;
                        sbTemp = new StringBuffer();
                        while (cTemp != ' ' && i < iKeyTextLength)
                        {
                              try
                              {
                                    cTemp = sKeyText.charAt(i);
                              }
                              catch (StringIndexOutOfBoundsException e1)
                              {
                                    System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (key).");
                              }
                              if (cTemp != ' ')
                              {
                                    sbTemp.append(cTemp);
                              }
                              i++;
                        }
                        try
                        {
                              iTemp = Integer.parseInt(sbTemp.toString());
                        }
                        catch (NumberFormatException e1a)
                        {
                              System.out.println("Caught NumberFormatException in decrypt() (key).");
                              break;
                        }
                        iKey[iKeyLength] = iTemp;
                        iKeyLength++;
                  }
            }
            else
            {
                  iKeyLength = iKeyTextLength;
                  System.out.println("The key is not numeric, iKeyLength = " + iKeyLength + ".");
            }

            if (iCryptLength < iKeyLength)
            {
                  if (bNumericCrypt)
                  {
                        System.out.println("Ignoring excess key digits.");
                  }
                  else
                  {
                        System.out.println("Ignoring excess key characters.");
                  }
            }
            else
            {
                  if (iKeyLength < iCryptLength)
                  {
                        System.out.println("Key is shorter than crypttext: repeating the key.");
                        //      lblMessage.setText("Key is shorter than crypttext: repeating the key.");
                        if (bNumericKey)
                        {
                              // Repeat the key as necessary to make it long enough.
                              iKeyBuffer = new int[iKeyLength];                                  // Need to stash the key characters someplace
                              for (i = 0; i < iKeyLength; i++)                                   // while we get more memory for the array.
                              {
                                    iKeyBuffer[i] = iKey[i];
                              }
                              iKey = new int[iCryptLength];                                      // Expand the key array.
                              for (i = 0; i < iKeyLength; i++)
                              {
                                    iKey[i] = iKeyBuffer[i];
                              }
                              for (i = iKeyLength; i < iCryptLength; i++)
                              {
                                    iKey[i] = iKey[i - iKeyLength];
                              }
                        }
                        else
                        {
                              sbTemp = new StringBuffer(sKeyText);
                              for (i = iKeyLength; i < iCryptLength; i++)
                              {
                                    try
                                    {
                                          cTemp = sbTemp.charAt(i - iKeyLength);
                                    }
                                    catch (StringIndexOutOfBoundsException e2)
                                    {
                                          System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (e2).");
                                    }
                                    sbTemp.append(cTemp);
                              }
                              sKeyText = sbTemp.toString();
                              iKeyLength = sKeyText.length();
                        }
                  }
                  else
                  {
                        // They are equal in length, do nothing.
                  }
            }
            // Begin the decryption.
            sbTemp = new StringBuffer();
            for (i = 0; i < iCryptLength; i++)
            {
                  if (bNumericKey)
                  {
                        if (bNumericCrypt)
                        {
                              iTemp = iCrypt[i] ^ iKey[i];
                        }
                        else
                        {
                              try
                              {
                                    cTemp = sCryptText.charAt(i);
                              }
                              catch (StringIndexOutOfBoundsException e3)
                              {
                                    System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (e3).");
                              }
                              iTemp = cTemp ^ iKey[i];
                        }
                  }
                  else
                  {
                        if (bNumericCrypt)
                        {
                              try
                              {
                                    cTemp = sKeyText.charAt(i);
                              }
                              catch (StringIndexOutOfBoundsException e4)
                              {
                                    System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (e4).");
                              }
                              iTemp = iCrypt[i] ^ cTemp;
                        }
                        else
                        {
                              try
                              {
                                    cTemp = sCryptText.charAt(i);
                                    cTemp0 = sKeyText.charAt(i);
                              }
                              catch (StringIndexOutOfBoundsException e5)
                              {
                                    System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (e5) , i = " + i + ".");
                              }
                              iTemp = cTemp ^ cTemp0;
                        }
                  }
                  sbTemp.append((char) iTemp);
            }
            sTemp = sbTemp.toString();
            System.out.println("sTemp " + sTemp );

            if (unprintableP(iCryptLength, sTemp))
            {
                  System.out.println("Unprintable decrypted text, writing " + iCryptLength + " characters as numeric code.");
                  //lblMessage.setText("Unprintable decrypted text, writing " + iCryptLength + " characters as numeric code.");
                  sbTemp = new StringBuffer();
                  for (i = 0; i < iCryptLength; i++)
                  {
                        try
                        {
                              iTemp = sTemp.charAt(i);
                        }
                        catch (StringIndexOutOfBoundsException e6)
                        {
                              System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (e6).");
                        }

                        if (i == iCryptLength - 1)
                        {
                              sbTemp.append(iTemp);
                        }
                        else
                        {
                              sbTemp.append(iTemp + " ");
                        }
                  }
                  taPlainText=sbTemp.toString();
            }
            else
            {
                  System.out.println("Printable decrypted text, writing " + iCryptLength + " characters.");
                  //      lblMessage.setText("Printable decrypted text, writing " + iCryptLength + " characters.");
                  taPlainText=sTemp;
            }
            return taPlainText;
      }                                      
      boolean numericP(int iTextLength, String sText)
      {
            boolean r = true;
            int i = 0;
            char cTemp = 0;
            int iRunLength = 0;
            int iMaxRunLength = 0;

            for (i = 0; i < iTextLength; i++)
            {
                  try
                  {
                        cTemp = sText.charAt(i);
                  }
                  catch (StringIndexOutOfBoundsException e7)
                  {
                        System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (e7).");
                  }
                  if (cTemp != ' ')
                  {
                        if (cTemp < 48 || cTemp > 57)
                        {
                              r = false;
                              break;                                                          // Break out of for loop.
                        }
                        else
                        {
                              if (cTemp > 47 && cTemp < 58)
                              {
                                    // We have a decimal digit.
                                    iRunLength++;
                              }
                        }
                  }
                  else
                  {
                        // We have a space:                                                // Detect delimiting spaces
                        if (iRunLength > iMaxRunLength)
                        {
                              iMaxRunLength = iRunLength;
                        }
                        iRunLength = 0;
                  }
            }                                                                     // End of for.

            if (iRunLength > iMaxRunLength)                                       // Catch the case where the space
            {                                                                     // comes at the end of the string.
                  iMaxRunLength = iRunLength;
            }

            if (iMaxRunLength > 3)                                                // It's not text that happens to to be
            {                                                                     // a string (or strings) of digits.
                  r = false;
            }

            return r;
      }

      boolean unprintableP(int iLength, String sText)
      {
            boolean r = false;
            int i = 0;
            char cTemp = 0;

            for (i = 0; i < iLength; i++)
            {
                  try
                  {
                        cTemp = sText.charAt(i);
                  }
                  catch (StringIndexOutOfBoundsException e8)
                  {
                        System.out.println("Caught StringIndexOutOfBoundsException in decrypt() (e8).");
                  }

                  if (cTemp < 32 || cTemp == 127)
                  {
                        if (cTemp != 9 && cTemp != 10 && cTemp != 12 && cTemp != 13)
                        {
                              r = true;
                              System.out.println("Unprintable on character " + (int) cTemp);
                              break;                                                          // Break out of for loop.
                        }
                  }
            }

            return r;
      }
[+][-]11/03/08 02:32 PM, ID: 22871954Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/10/08 12:42 PM, ID: 23142873Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_2_20070628