So I was talking with a friend online the other day the the subject of transposition ciphers came up and I ended up whipping this together:

It's not very pretty, but it works. I still learning the finer points of c++.Code:

#ifdef HAVE_CONFIG_H

#include <config.h>

#endif

#include <iostream>

#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])

{

char plaintext1[255];

char plaintext2[255];

char ciphertext[255];

int caesarkey = 0;

int stringlength = 0;

int i = 0;

cout<<"Next we are going to try a flip cipher, this is a little more complicated, so bear with me."<<endl;

cout<<endl<<"First off, enter a new plaintext: ";

cin.getline(plaintext2, 255, '\n');

while(plaintext2[i] != '\0'){i++;stringlength++;}

char flipcipher[stringlength+1];

int permuter[stringlength];

cout<<"Now we know that our ciphertext is "<<stringlength<<" Characters long. We are now going to move the characters around a little, for that we need to enter a key for them."<<endl;

cout<<"Please enter the numbers 0 - "<<stringlength-1<<" In a random order! Don't repeat yourself!"<<endl;

i = 0;

while(i < stringlength){

int n = 0;

int u = 0;

cout<<"Enter number: ";

cin>>n;

if(n>stringlength){

cout<<"Number too high! Try again!";

cin>>n;

i--;}

permuter[i] = n;

i++;}

i = 0;

cout<<"Ok now we have a permutor, let put it to use!"<<endl;

while(plaintext2[i] != '\0'){

flipcipher[i] = plaintext2[permuter[i]];

i++;}

flipcipher[i+1] = '\0';

i = 0;

cout<<"Ok now we have flipped the plaintext, let's look at it: "<<endl;

while(flipcipher[i] !='\0'){

cout<<flipcipher[i];

i++;}

cout<<endl<<"OK! Now let us see if we can decode the text as well!"<<endl<<endl;

i = 0;

while(flipcipher[i] !='\0'){

plaintext1[permuter[i]] = flipcipher[i];

i++;}

cout<<"If what comes next is your real text then we've won!"<<endl;

i = 0 ;

while(plaintext1[i] !='\0'){

cout<<plaintext1[i];

i++;}

cout<<endl;

return 0;

}

My question is this: As you can see my method for generating a "permuter" (I suppose the correct term is a key) is far from elegant, I tried to incorporate a check for duplicate numbers but it only worked some of the time, and even the length check is sort of dodge, so I am looking for a method to generate a key in a way that is half-way random. I can't just generate a string of random numbers since the cipher requires it to be a permutation of the number string from 0 tostringlength, so I was thinking is there any relatively simple way to generate an arbitrary permutation of said string?

Also any general comments on the code are welcome, mostly it was written to show the principle of a transposition cipher and for me to learn a bit more C++, so any comments are welcome! :)