| | |
Simple problem with encryption
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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)
import java.util.*; public class CustomCypher { public String encryptText(String key, String text) { long finalKey = 0; for (int i=0; i<key.length(); i++) { long tempKey = key.charAt(i); tempKey *= 128; finalKey += tempKey; } Random generator = new Random(finalKey); String returnString = ""; for (int i=0; i<text.length(); i++) { int temp = (int)text.charAt(i); temp += generator.nextInt(95); if (temp > 126) { temp -= 95; } returnString += (char)temp; } return returnString; } public String decryptText(String key, String text) { long finalKey = 0; for (int i=0; i<key.length(); i++) { long tempKey = key.charAt(i); tempKey *= 128; finalKey += tempKey; } Random generator = new Random(finalKey); String returnString = ""; for (int i=0; i<text.length(); i++) { int temp = (int)text.charAt(i); temp -= generator.nextInt(95); if (temp < 36) { temp+= 95; } else if (temp > 126) { temp -= 95; } returnString += (char)temp; } return returnString; } }
•
•
Join Date: Mar 2005
Posts: 53
Reputation:
Solved Threads: 1
•
•
•
•
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)
import java.util.*; public class CustomCypher { public String encryptText(String key, String text) { long finalKey = 0; for (int i=0; i<key.length(); i++) { long tempKey = key.charAt(i); tempKey *= 128; finalKey += tempKey; } Random generator = new Random(finalKey); String returnString = ""; for (int i=0; i<text.length(); i++) { int temp = (int)text.charAt(i); temp += generator.nextInt(95); if (temp > 126) { temp -= 95; } returnString += (char)temp; } return returnString; } public String decryptText(String key, String text) { long finalKey = 0; for (int i=0; i<key.length(); i++) { long tempKey = key.charAt(i); tempKey *= 128; finalKey += tempKey; } Random generator = new Random(finalKey); String returnString = ""; for (int i=0; i<text.length(); i++) { int temp = (int)text.charAt(i); temp -= generator.nextInt(95); if (temp < 36) { temp+= 95; } else if (temp > 126) { temp -= 95; } returnString += (char)temp; } return returnString; } }
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.
![]() |
Similar Threads
- Simple text encryption (C#)
- Very simple XOR Encryption (C++)
- Code Snippet: Simple Data Encryption Standard (SDES) Algorithm for Encryption and Decryption. (C++)
- Wireless connection problem with encryption. (Networking Hardware Configuration)
- Help me with this Simple Problem plss (C)
- BUTTON DOES NOT WORK??? simple problem (C)
Other Threads in the Java Forum
- Previous Thread: Shopping cart class
- Next Thread: Best Java Project
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp draw eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






