I made a program to encode and decode messages using a Vigenere Cipher and a key. (if you dont know what a Vigenere Cipher is... then check Wikipedia.) It should work *fine*, but it keeps hanging at the point when it should show the encrypted message/decrypted message.
Code:
#include<iostream>
#include<windows.h>
using namespace std;
class Frame
{
public:
char square();
int enc();
int dec();
};
char Frame::square()
{
// ===(Vigenere Square Generator)===
char a[26][26], alpha='a', flag='a', showvig;
for(int col=0; col<26; col++)
{
for(int row=0; row<26; row++)
{
a[col][row]=alpha; //'a[][]' is assigned alpha's value
alpha++; //Alpha increased
if(alpha>'z')
alpha='a'; //continues the series after z
// cout<<a[col][row]<<" "; //optional display statement
}
flag++; //Value of flag increased
alpha=flag; //Value of flag assigned to a
}
/* cout<<"\nDisplay Vigenere Square?(Y/N) ";
cin>>showvig;
if(showvig=='Y' || showvig=='y')
{
for(int col=0; col<26; col++)
{
for(int row=0; row<26; row++)
{
cout<<a[col][row]<<" ";
}
cout<<endl;
}
} */
return a[26][26];
}
int Frame::enc()
{
char b[26][26];
b[26][26]=square();
// ===(ACME Encrypt O Matic)===
char key[50], srchk, srchm, message[100], encoded[100];
int keylen, msglen, arow, bcol, k;
cout<<"\nEncrypter ready";
cout<<"\nInsert key... ";
cin.getline(key,50);
cin.getline(key,50);
cout<<"\nShowing the key..... "<<key<<key<<key; //Comparison purposes
cout<<"\nEnter the message... ";
cin.getline(message,100);
keylen=strlen(key);
msglen=strlen(message);
cout<<"\nEncrypted message... ";
for(int i=0; i<msglen; i++)
{
k=i; //Allows for the messages
if(i>keylen) //to be longer than the
k=0; //key ie repeating key behaviour
srchk=key[k];
srchm=message[i];
if(message[i]==' ')
{
encoded[i]=' ';
goto label;
}
for(int col=0, row=0; col<26; col++)
{
if(srchk==b[row][col])
{
bcol=col;
break;
}
}
for(int row=0, col=0; row<26; row++)
{
if(srchm==b[row][col])
{
arow=row;
break;
}
}
encoded[i]=b[arow][bcol];
label:
cout<<encoded[i];
}
cout<<endl;
system("PAUSE");
return 0;
}
int Frame::dec()
{
char c[26][26];
c[26][26]=square();
// ===(ACME Encryption-Be-Gone)===
char key[50], srchk, srchm, message[100], encoded[100];
int keylen, msglen, arow, bcol, k;
cout<<"\nInitializing...";
Sleep(5);
cout<<"\nDecrypter ready";
cout<<"\nInsert key... ";
cin.getline(key,50);
cin.getline(key,50);
cout<<"\nShowing the key..... "<<key<<key<<key; //Comparison purposes
cout<<"\nEnter coded mssg.... ";
cin.getline(encoded,100);
keylen=strlen(key);
msglen=strlen(encoded);
cout<<"\nDecrypted message... ";
for(int i=0; i<msglen; i++)
{
k=i; //Allows for the messages
if(i>keylen) //to be longer than the
k=0; //key ie repeating key behaviour
srchk=key[k];
srchm=encoded[i];
if(encoded[i]==' ')
{
message[i]=' ';
goto label2;
}
for(int col=0, row=0; col<26; col++)
{
if(srchk==c[row][col])
{
bcol=col;
break;
}
}
for(int row=0, col=bcol; row<26; row++) //col=0 changed
{
if(srchm==c[row][col])
{
col=0;
bcol=col;
arow=row;
break;
}
}
message[i]=c[arow][bcol];
label2:
cout<<message[i];
}
cout<<endl;
system("PAUSE");
return 0;
}
int main()
{
Frame ob;
int ch;
cout<<"\n(1)Encrypt or (2)decrypt?";
cin>>ch;
if(ch==1)
ob.enc();
else
if(ch==2)
ob.dec();
return 0;
}
What exactly is t3h problem? It worked fine when I ran the encrypt/decrypt sections separately (by putting all the code in just the main(), and putting comments on the encrypt or decrypt sections)
-regards,
ultrabot90