954,141 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Encryption problem using key

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

ivabigun
Newbie Poster
10 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Pseudocode

string key = "blah"
string input = "my string"
string cipherstring = ""
for(i=0;i<input.length;i+=key.length)
{
  for(k=0;k<key.length;k++)
 {
    cipherstring += key[k] //concatenate
 }
}


I believe that should work.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

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

ivabigun
Newbie Poster
10 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

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

ivabigun
Newbie Poster
10 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

Because that is what you have for an increment statement i+=key.length in your loop.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

ivabigun
Newbie Poster
10 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 
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

ivabigun
Newbie Poster
10 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You