a string through a for-loop, im lost......?

aah well, im stuck..again... encrypt a string through a loop?

Page 1 of 1

7 Replies - 1092 Views - Last Post: 09 October 2008 - 10:35 AM Rate Topic: -----

#1 OrangeSection  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

a string through a for-loop, im lost......?

Post icon  Posted 07 October 2008 - 09:59 AM

My first real post.... :D

Lets say i got a String of mixed characters, digits/letters/decimalpoints etc. and its encrypted.

I have to run them through a for-loop to DEcrypt them, one character at a time.

The first crypt-key is already supplied to ENcrypt the first String "A@F", its "113", and im supposed
to get the second key by ENcrypting the first String.

There is actually two Strings that are supposed to be handled through for-loops i guess, i just went fed-up
with the first one, (after 7 hours) i just wanted to move on coding, so i went on and did a temporary solution.

And now, another 5 hours later, im posting here...i need help.

I think i could handle the rest, somewhat, if i only got some pointers on how to loop the Strings....

im quite new at this and i just cant find any way to go about it.... :crazy:

ah well..*sighs*
here is the code, so far.....

 
public class Crypt
{
	public static final String MYSTERIOUS_MESSAGE = "Vcpeexb01Ud1ypc1}hrzpeb?";
	
	public static void main(String[] args)
	{ 
		
		String encryptString1 = "A@F";
		int firstKey = 113;
		Crypto crypto = new Crypto();
		crypto.enCrypt(encryptString1, firstKey);
		String decryptString1 = MYSTERIOUS_MESSAGE;
		int lengthChar1 = MYSTERIOUS_MESSAGE.length();
		crypto.deCrypt(decryptString1, lengthChar1);
	}
}
class Crypto 
{
	private int actualKey;
	
	public Crypto()
	{
		actualKey = 0;
		
	}
	public int enCrypt (String encryptString1,int firstKey)
	{
		this.actualKey = firstKey;
		char char0 = encryptString1.charAt(0);					//temp solution
		char char1 = encryptString1.charAt(1);					//temp
		char char2 = encryptString1.charAt(2);					//temp
		char encryptedChar0 = (char)(char0 ^ actualKey);   //temp
		char encryptedChar1 = (char)(char1 ^ actualKey);   //temp
		char encryptedChar2 = (char)(char2 ^ actualKey);   //temp
		String decryptKeyNext = "" + encryptedChar0 + encryptedChar1 + encryptedChar2; 
		actualKey = Integer.parseInt(decryptKeyNext);
		
		return actualKey;
	}
		public void deCrypt(String decryptString1,int lengthChar1)
		{
			for ()
			{
				for ()				 																																					
				
			}
		}
}


This post has been edited by OrangeSection: 07 October 2008 - 12:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: a string through a for-loop, im lost......?

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: a string through a for-loop, im lost......?

Posted 07 October 2008 - 05:16 PM

w00t, another Swede!!

When you say loop the strings, I guess you mean loop through the characters of the string which you actually already do in your encrypt method so it's not much more difficult o do it in the decrypt-method.

Just find out the length of the string (cryptotext) and loop

for (int i = 0; i < lengthChar1; i++) {
char tmpchar = (char) decryptString1.charAt(i);
[Do something with tmpchar here]
}
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: a string through a for-loop, im lost......?

Posted 07 October 2008 - 05:25 PM

View PostGloin, on 7 Oct, 2008 - 05:16 PM, said:

w00t, another Swede!!

So this is were you are coming from ?
So now that we all know about it you can put it in your profile. It helps for time zone calculations during messages exchanges.
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: a string through a for-loop, im lost......?

Posted 07 October 2008 - 05:34 PM

Try, so we know where to send all our generous contributions (money that is) and I'll consider it.. ;)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: a string through a for-loop, im lost......?

Posted 07 October 2008 - 05:39 PM

View PostGloin, on 7 Oct, 2008 - 05:34 PM, said:

Try, so we know where to send all our generous contributions (money that is) and I'll consider it.. ;)

Are you still with Crowns or you switched to Euro ?
:)
Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: a string through a for-loop, im lost......?

Posted 07 October 2008 - 05:45 PM

Sadly, we still have Crowns. It's stupid, stupid I tell you!
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: a string through a for-loop, im lost......?

Posted 07 October 2008 - 05:47 PM

View PostGloin, on 7 Oct, 2008 - 05:45 PM, said:

Sadly, we still have Crowns. It's stupid, stupid I tell you!

There always a positive point: that keeps the Currency Exchange bureaus in railways and ferry boat stations opened
Was This Post Helpful? 0
  • +
  • -

#8 OrangeSection  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 07-October 08

Re: a string through a for-loop, im lost......?

Posted 09 October 2008 - 10:35 AM

View PostGloin, on 7 Oct, 2008 - 05:16 PM, said:

w00t, another Swede!!

When you say loop the strings, I guess you mean loop through the characters of the string which you actually already do in your encrypt method so it's not much more difficult o do it in the decrypt-method.

Just find out the length of the string (cryptotext) and loop

for (int i = 0; i < lengthChar1; i++) {
char tmpchar = (char) decryptString1.charAt(i);
[Do something with tmpchar here]
}


we grow on trees u know....

Thanks for your help , i figured it out for myself though, cut away alot of code, and started again.

I came up with something similar to yours, but thanks anyway.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1