Encryption problem using key

Reply

Join Date: Dec 2007
Posts: 10
Reputation: ivabigun is an unknown quantity at this point 
Solved Threads: 0
ivabigun ivabigun is offline Offline
Newbie Poster

Encryption problem using key

 
0
  #1
Dec 6th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Encryption problem using key

 
0
  #2
Dec 6th, 2007
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.
  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.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: ivabigun is an unknown quantity at this point 
Solved Threads: 0
ivabigun ivabigun is offline Offline
Newbie Poster

Re: Encryption problem using key

 
0
  #3
Dec 6th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Encryption problem using key

 
0
  #4
Dec 6th, 2007
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.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: ivabigun is an unknown quantity at this point 
Solved Threads: 0
ivabigun ivabigun is offline Offline
Newbie Poster

Re: Encryption problem using key

 
0
  #5
Dec 6th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,455
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 511
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Encryption problem using key

 
0
  #6
Dec 6th, 2007
Because that is what you have for an increment statement i+=key.length in your loop.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: ivabigun is an unknown quantity at this point 
Solved Threads: 0
ivabigun ivabigun is offline Offline
Newbie Poster

Re: Encryption problem using key

 
0
  #7
Dec 6th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Encryption problem using key

 
0
  #8
Dec 6th, 2007
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.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: ivabigun is an unknown quantity at this point 
Solved Threads: 0
ivabigun ivabigun is offline Offline
Newbie Poster

Re: Encryption problem using key

 
0
  #9
Dec 6th, 2007
Originally Posted by ShawnCplus View Post
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
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC