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","reg3
45*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("sCrypt
Text:"+sCr
yptText);
System.out.println("iKeyTe
xtLength:"
+iKeyTextL
ength);
// 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 (StringIndexOutOfBoundsExc
eption e0)
{
System.out.println("Caught
StringIndexOutOfBoundsExce
ption in decrypt() (crypt).");
}
if (cTemp != ' ')
{
sbTemp.append(cTemp);
}
i++;
}
iCrypt[iCryptLength] = Integer.parseInt(sbTemp.to
String());
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 (StringIndexOutOfBoundsExc
eption e1)
{
System.out.println("Caught
StringIndexOutOfBoundsExce
ption in decrypt() (key).");
}
if (cTemp != ' ')
{
sbTemp.append(cTemp);
}
i++;
}
try
{
iTemp = Integer.parseInt(sbTemp.to
String());
}
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("Ignori
ng excess key digits.");
}
else
{
System.out.println("Ignori
ng 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 (StringIndexOutOfBoundsExc
eption e2)
{
System.out.println("Caught
StringIndexOutOfBoundsExce
ption 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 (StringIndexOutOfBoundsExc
eption e3)
{
System.out.println("Caught
StringIndexOutOfBoundsExce
ption in decrypt() (e3).");
}
iTemp = cTemp ^ iKey[i];
}
}
else
{
if (bNumericCrypt)
{
try
{
cTemp = sKeyText.charAt(i);
}
catch (StringIndexOutOfBoundsExc
eption e4)
{
System.out.println("Caught
StringIndexOutOfBoundsExce
ption in decrypt() (e4).");
}
iTemp = iCrypt[i] ^ cTemp;
}
else
{
try
{
cTemp = sCryptText.charAt(i);
cTemp0 = sKeyText.charAt(i);
}
catch (StringIndexOutOfBoundsExc
eption e5)
{
System.out.println("Caught
StringIndexOutOfBoundsExce
ption 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("Unprin
table decrypted text, writing " + iCryptLength + " characters as numeric code.");
//lblMessage.setText("Unpr
intable decrypted text, writing " + iCryptLength + " characters as numeric code.");
sbTemp = new StringBuffer();
for (i = 0; i < iCryptLength; i++)
{
try
{
iTemp = sTemp.charAt(i);
}
catch (StringIndexOutOfBoundsExc
eption e6)
{
System.out.println("Caught
StringIndexOutOfBoundsExce
ption in decrypt() (e6).");
}
if (i == iCryptLength - 1)
{
sbTemp.append(iTemp);
}
else
{
sbTemp.append(iTemp + " ");
}
}
taPlainText=sbTemp.toStrin
g();
}
else
{
System.out.println("Printa
ble decrypted text, writing " + iCryptLength + " characters.");
// lblMessage.setText("Printa
ble 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 (StringIndexOutOfBoundsExc
eption e7)
{
System.out.println("Caught
StringIndexOutOfBoundsExce
ption 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 (StringIndexOutOfBoundsExc
eption e8)
{
System.out.println("Caught
StringIndexOutOfBoundsExce
ption in decrypt() (e8).");
}
if (cTemp < 32 || cTemp == 127)
{
if (cTemp != 9 && cTemp != 10 && cTemp != 12 && cTemp != 13)
{
r = true;
System.out.println("Unprin
table on character " + (int) cTemp);
break; // Break out of for loop.
}
}
}
return r;
}