943,513 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1205
  • Java RSS
Dec 6th, 2007
0

Encryption problem using key

Expand Post »
Hi people,

I am wantng to read a key in as an argument that will duplicate itself to the exact character length as the plaintext , read from a text file.

E.g. key = deceptive

Then i need it to repeat until the end of the plaintext file length as below:


Key: deceptivedeceptivedeceptive (incremented to exact length as plaintext)
Plaintext: wearediscoveredsaveyourself (read from a file)
Ciphertext: ZICVTWQNGRZGVTWAVZHCQYGLMGJ (output text)

At the moment i have converted my key and plaintext into 2 integer arrays, ready to do my algorithm in order to get my ciphertext.

I am assuming i will need to add some sort of a counter, but Any ideas of how to do this?
Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivabigun is offline Offline
10 posts
since Dec 2007
Dec 6th, 2007
0

Re: Encryption problem using key

Well it looks like the Vigenere cipher, am I correct?
You would need two loops. The outer one monitoring the overall length of the string. The inner loop monitoring the length of the key.
Java Syntax (Toggle Plain Text)
  1. Pseudocode
  2.  
  3. string key = "blah"
  4. string input = "my string"
  5. string cipherstring = ""
  6. for(i=0;i<input.length;i+=key.length)
  7. {
  8. for(k=0;k<key.length;k++)
  9. {
  10. cipherstring += key[k] //concatenate
  11. }
  12. }

I believe that should work.
Last edited by ShawnCplus; Dec 6th, 2007 at 3:12 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Dec 6th, 2007
0

Re: Encryption problem using key

thank you, but i dont think it works.

Here is a snippet of my code:
for ( int i = 0; i < file.length;i+=key.length)
{

for (int k = 0; k <key.length;k++)
{

file[i] += file[i] + key[k];
System.out.println(i);
System.out.println(k);

Here are the results:

i:0
k:0
i:0
k:1
i:0
k:2
i:0
k:3
i:0
k:4
i:0
k:5
i:0
k:6
i:0
k:7
i:0
k:8
i:0
k:9
i:10
k:0
i:10
k:1
i:10
k:2
i:10
k:3
i:10
k:4
i:10
k:5
and so on.

My key length (k) was 10 in this case.
Can you tell me please why (i) suddenly goes from 0 to 10 after the key has been re-used?

i need to someone get (i) to incease up to the last character in my file.

Thanks for any help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivabigun is offline Offline
10 posts
since Dec 2007
Dec 6th, 2007
0

Re: Encryption problem using key

Because each increment the length of the key is added to i. This makes it so the key string doesn't become longer than the string to be encrypted.

The loop will keep going until it has a key length equal to the string length (in theory)

And the += operator was just an example of concatenation. If you are using char arrays then that wont concatenate, it'll give you weird characters since it's adding the ASCII values together.
Last edited by ShawnCplus; Dec 6th, 2007 at 5:40 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Dec 6th, 2007
0

Re: Encryption problem using key

I am wanting to add the arrays togther.But why does (i) suddenly jump to 10 from 0 after the 1st key mapping?shouldnt it stay constant?

Thanks
Last edited by ivabigun; Dec 6th, 2007 at 6:10 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivabigun is offline Offline
10 posts
since Dec 2007
Dec 6th, 2007
0

Re: Encryption problem using key

Because that is what you have for an increment statement i+=key.length in your loop.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Dec 6th, 2007
0

Re: Encryption problem using key

thanks guys but its still not sinking in tonight .

So to map each key character (e.g.10 characters) to my file characters (e.g. 200 characters in total) what do i need to do to change my code?

Many thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivabigun is offline Offline
10 posts
since Dec 2007
Dec 6th, 2007
0

Re: Encryption problem using key

OK, you originally wanted your key length to match your string length. To do that you need to keep repeating the key until it matches the length of the target string. This is why the outer loops has added to it the length of the key each time the loop iterates.
Last edited by ShawnCplus; Dec 6th, 2007 at 6:26 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Dec 6th, 2007
0

Re: Encryption problem using key

Click to Expand / Collapse  Quote originally posted by ShawnCplus ...
OK, you originally wanted your key length to match your string length. To do that you need to keep repeating the key until it matches the length of the target string. This is why the outer loops has added to it the length of the key each time the loop iterates.
Thanks for the quick reply. I understand it now.
Many thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivabigun is offline Offline
10 posts
since Dec 2007

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: Gregorian Calender help
Next Thread in Java Forum Timeline: Help with Idea?!?





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


Follow us on Twitter


© 2011 DaniWeb® LLC