Hello All :

I tried to write a simple program in C++ for encrypt a word using a keyword and a key depending on an alphabet ,for example

keyword:HELO
Key : D

I want to genrate the cipher alphabet by puts the positions of letters of the keyword(HELO)
in the cipher alphabet & must be instead of the position of the Key(D) in the orginal alphabet. and then resume the other letters that not exist in the keyword put these letters
in the cipher alphabet too ....

ABC DEFG HIJKLMNOPQRSTUVWXYZ (English alpabet)
XYZ HELO ABCDFGHIJKMNPQRSTUV (cipher alphabet)

I tried to enter the alphabet

``````int y=0;
for(int x='a';x<='z';x++){
en[y]=x;
cout<<en[y];
y++;
}

puts("Enter the keyword");
gets(keyw);
puts("Enter the key");
gets(k);``````

// until know it's OK, but I don't how to generate the cipher alphabet.....

lines 9, 10, and 11: I thought this was supposed to be c++, not C ?????? So why are you writing C-style code in a c++ program?

``````cout << "Enter the keyword";
cin >> keyw;
cout << "Enter the key";
cin >> key;``````
Answered by Lerner 582 in a post from

1) Determine length of the keyword
2) Create an array of the English alphabet
2) Find the index of the element in the English alphabet equal to the the offset char.
3) Remove the letters in the keyword from the English alphabet array creating an array holding the depleted English …

## All 7 Replies

lines 9, 10, and 11: I thought this was supposed to be c++, not C ?????? So why are you writing C-style code in a c++ program?

``````cout << "Enter the keyword";
cin >> keyw;
cout << "Enter the key";
cin >> key;``````

Hello :
Thanks for the answer, It was realy a good point , but how can I genrate the cipher algo.
depending on the key and the keyword, if I enter the key(D) the keyword postion started form the position of key(D) in the original alphabet.....any idea how to do it??

1) Determine length of the keyword
2) Create an array of the English alphabet
2) Find the index of the element in the English alphabet equal to the the offset char.
3) Remove the letters in the keyword from the English alphabet array creating an array holding the depleted English alphabet.
4) Create an new array to hold the cipher alphabet
5) Place the keyword in the array starting at the index of the offset char
6) Add the index of the offset char to the length of the keyword (you may have to adjust this value by one, work it out yourself)
7) Subtract the value from #6 above from the size of the depleted English alphabet.
8) Use a loop to copy the char in the depleted English alphabet starting into the cipher alphabet array starting at index 0 of the depleted English alphabet array and at the end of the keyword in the cipher alphabet array. This loop should copy the number of char determined in #7 above
9) copy the remaining elements of the depleted English alphabet into the cipher alphabet array starting at element zero of the cipher array.

Instead of copying char by char in steps 5, 8 and 9 you could create three strings: the keyword, the first part of the depleted English alphabet from step 8 and the second part of the depleted English array in step 9, and then concatenate them in the appropriate order.

I'm sure there are other clever ways to do it, too, but I think this approach should work for the encryptment. Of course encryption is only useful if you can decrypt the message at some point so you have a lot of work to do here, even if you can get the above protocol set up successfully.

Hello Lerner:
Thanks a lot for the explation...but the steps 6 and 7 could you explain it with codes......Thanks in advance

Hello , I have solved out the code in your program and you can take in the code below.

But there are a few backfalls to the program which i was not able to correct because i am still a beginener in programming.

they are

1) The keyword should contain the same letters twice.
Eg: If you are taking "letters" as the keyword you will need to type "lettrs" for the code to stay viable.

2) The message shouldnt contain any spaces in between. This is because i have used the function cin instead of gets. I guess if we use gets that will be solved. But some additional code has to be added in.

3) Write in a De- Encrypter which gets the encrypted message back. This can be done easily by modifying the code in the end of the file.

So if these are viable then the following code wll do the encrypting

``````#include <iostream>
using namespace std;
/***********************************************************************
---------------------------------------Encrypter Program----------------------------------------------

Written by : Sky Diploma
Personal experiences in writing the program:
In trying to write this program i spent over 22 hours thinking in the day and was only able to manage 2 hours of sleep.
I had an immense problem but solved it when i seeked help at the DANIWEB FORUMS -I am thankful to niek_e for showing me the approach and iamthwee for showing me a new mechanism. Though i dint use it

Things to be done:
) The keyword should contain the same letters twice.
Eg: If you are taking "letters" as the keyword you will need to type "lettrs" for the code to stay viable.

2) The message shouldnt contain any spaces in between. This is because i have used the function cin instead of gets. I guess if we use gets that will be solved. But some additional code has to be added in.
3) Write in a De- Encrypter which gets the encrypted message back. This can be done easily by modifying the code in the end of the file.
*/
int s;
int main()
{         int s1,j;
char en[26],ci[26],keyword[10],key,message[40],demessage[40];
cout<<"\nEnter the keyword\n";//beta
cin>>keyword;
cout<<"Enter the key\n";//r
cin >>key;
cout<<"Enter the message that you want to cipher: \n";
cin >> message;

int y=0;
for(int x='a';x<='z';x++)
{//This creats the English Alphabets placed in en
en[y]=x;
y++;
}

for (int j=0;j<=25;j++)
{
if (key == en[j])
{
::s = j;
}
}
cout<<"The cypher Key Number is "<<s<<"\n";

int b=0;
for (::s;b<=strlen(keyword);s++)
{
ci[s]= keyword[b];
b++;
}
for(b=0;b<(s-(strlen(keyword)+1));b++)
{ci[b]='0';}
for(b=(s-1);b<=25;b++)
{
ci[b]='0';
}
int a=(s-1);
int term;

for(b=0;a<=25;b++)
{
for(int c=0;c<=25;(c++))
{
if(en[b]!=ci[c])
{
term=1;

}
else{
term=0;
break;
}
}
if(term==1)
{
ci[a]=en[b];
a++;
term=0;
}

}
a=0;
for(b=0;a<=s-(strlen(keyword-2));b++)
{
for(int c=0;c<=25;(c++))
{
if(en[b]!=ci[c])
{
term=1;

}
else{
term=0;
break;
}
}
if(term==1)
{
ci[a]=en[b];
a++;
term=0;
}

}

for(int d=0;d<=25;d++){cout<<ci[d];}
cout<<"\n"<<en;

for (int k=0;k<=strlen(message);k++)//Modify this to get the code for the De-Encrypter
{
for(int n=0;n<=25;n++)
{
if(message[k]==en[n])
{
demessage[k]=ci[n];
break;
}
}
}
cout<<"\nYour CODED message is :: "<<demessage<<"\n";
cin>>a;
cin.get();
}``````

Sorry that the code is a little messy.
The main aim of posting the whole code is for the poster to understand how it was done.

Although it was a challenge for me to do it too. I have done my best in keeping it simple.

Hello Sky:

Thanks a lot for the answer ,

No Problem.
If you have any doubts you know where to ask ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.