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
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
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
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
Because that is what you have for an increment statement i+=key.length in your loop.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
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
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268