943,940 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4823
  • Java RSS
Mar 21st, 2005
0

Simple problem with encryption

Expand Post »
I got this method that encrypts and decrypts text. All of it works fine, except when it comes to spaces. It doesn't convert the spaces correct. If you take a look at it, you might understand.

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class CustomCypher
  4. {
  5. public String encryptText(String key, String text)
  6. {
  7. long finalKey = 0;
  8. for (int i=0; i<key.length(); i++)
  9. {
  10. long tempKey = key.charAt(i);
  11. tempKey *= 128;
  12. finalKey += tempKey;
  13. }
  14.  
  15. Random generator = new Random(finalKey);
  16. String returnString = "";
  17. for (int i=0; i<text.length(); i++)
  18. {
  19. int temp = (int)text.charAt(i);
  20. temp += generator.nextInt(95);
  21. if (temp > 126)
  22. {
  23. temp -= 95;
  24. }
  25. returnString += (char)temp;
  26. }
  27.  
  28. return returnString;
  29. }
  30. public String decryptText(String key, String text)
  31. {
  32. long finalKey = 0;
  33. for (int i=0; i<key.length(); i++)
  34. {
  35. long tempKey = key.charAt(i);
  36. tempKey *= 128;
  37. finalKey += tempKey;
  38. }
  39.  
  40. Random generator = new Random(finalKey);
  41. String returnString = "";
  42. for (int i=0; i<text.length(); i++)
  43. {
  44. int temp = (int)text.charAt(i);
  45. temp -= generator.nextInt(95);
  46. if (temp < 36)
  47. {
  48. temp+= 95;
  49. }
  50. else if (temp > 126)
  51. {
  52. temp -= 95;
  53. }
  54. returnString += (char)temp;
  55. }
  56.  
  57. return returnString;
  58. }
  59. }
Similar Threads
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Mar 21st, 2005
0

Re: Simple problem with encryption

Quote originally posted by server_crash ...
I got this method that encrypts and decrypts text. All of it works fine, except when it comes to spaces. It doesn't convert the spaces correct. If you take a look at it, you might understand.

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class CustomCypher
  4. {
  5. public String encryptText(String key, String text)
  6. {
  7. long finalKey = 0;
  8. for (int i=0; i<key.length(); i++)
  9. {
  10. long tempKey = key.charAt(i);
  11. tempKey *= 128;
  12. finalKey += tempKey;
  13. }
  14.  
  15. Random generator = new Random(finalKey);
  16. String returnString = "";
  17. for (int i=0; i<text.length(); i++)
  18. {
  19. int temp = (int)text.charAt(i);
  20. temp += generator.nextInt(95);
  21. if (temp > 126)
  22. {
  23. temp -= 95;
  24. }
  25. returnString += (char)temp;
  26. }
  27.  
  28. return returnString;
  29. }
  30. public String decryptText(String key, String text)
  31. {
  32. long finalKey = 0;
  33. for (int i=0; i<key.length(); i++)
  34. {
  35. long tempKey = key.charAt(i);
  36. tempKey *= 128;
  37. finalKey += tempKey;
  38. }
  39.  
  40. Random generator = new Random(finalKey);
  41. String returnString = "";
  42. for (int i=0; i<text.length(); i++)
  43. {
  44. int temp = (int)text.charAt(i);
  45. temp -= generator.nextInt(95);
  46. if (temp < 36)
  47. {
  48. temp+= 95;
  49. }
  50. else if (temp > 126)
  51. {
  52. temp -= 95;
  53. }
  54. returnString += (char)temp;
  55. }
  56.  
  57. return returnString;
  58. }
  59. }
In the decrypt function change
if (temp < 36)
{
temp+= 95;
}
else if (temp > 126)
{
temp -= 95;
}

to
if (temp < 36)
{
temp+= 95;
}
if (temp > 126)
{
temp -= 95;
}

I think this solves the problem. All though I do not know the crypting algo and I dont understand the exact logic. But brute debugging suggests this.
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
aj.wh.ca is offline Offline
53 posts
since Mar 2005
Mar 21st, 2005
0

Re: Simple problem with encryption

Im very impressed. I didn't think that would work, but it did. All of those numbers and if statements keep the range of characters inside the printable ranges, instead of using all 255 characters. If I didn't do it that way, it would be EXTREMELY hard to decrypt.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Shopping cart class
Next Thread in Java Forum Timeline: Question about indexing objects





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC