Simple problem with encryption

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Simple problem with encryption

 
0
  #1
Mar 21st, 2005
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 53
Reputation: aj.wh.ca is an unknown quantity at this point 
Solved Threads: 1
aj.wh.ca aj.wh.ca is offline Offline
Junior Poster in Training

Re: Simple problem with encryption

 
0
  #2
Mar 21st, 2005
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Simple problem with encryption

 
0
  #3
Mar 21st, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC