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.

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;
	}
}

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.

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;
	}
}

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.