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

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.

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

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.

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

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

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

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.

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

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.