Caesar Cipher using ASCII

Hi everyone
I have to follow this algorithm in C++:

Encrypt (PlainText,shift)
Loop over PlainText
NextCharacter = GetNext(PlainText)
EncryptedCharacter = ( (ASCII of NextCharacter - ASCII of first charactar in
character's list + shift) MOD size of character's list) + ASCII of first charachtar in character's list
Append EncryptedCharacter to EncryptedString
End Loop
End Encrypt

and I did this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main (){
	string password=" ";
	int shift=2;
	int c;
	cout<<"PLEASE ENTER YOUR PASSWORD\n";
	cin>>password;
	 c = getch();
		for (int i=0;i<3;i++){
			char encryptedch;
			string encryptedstr;
			encryptedch= (((((int)password)+1) - 1 + shift) % 127)+ 1;
			encryptedstr.push_back();

		}
cout<<encryptedstr;
		return 0;
}


I've tried many ways without any result!
the problem is in converting from Password string to indivisual charactars
and vice versa..
output should be like
PLEASE ENTER YOUR PASSWORD
abc1
def4 

Thanks in advance
Last edited on
Why not simply take each char and add 3? That will set the ascii code for each char 3 more then its original value. Something like:

1
2
for (char &c : password)
    c +=3;


That would replace everything from line 12 to line 19 I believe.
Last edited on
I think my teacher wants us to know how to use caesar cipher, and how to control the individual characters in string.
Anyway thank you.
I still need help.
Only thing I can really see wrong is that you are trying to manipulate and then assign the entire string to a char type variable on line 16. Try getting a single char from the string at a time using either the [] operator or the at member function. Both are described here:

http://www.cplusplus.com/reference/string/string/

Using that you can get the string size to use as the test against i for the for loops to ensure you iterator over every character in the string (the way you have it set up, it will only do the first 3 characters) replace password with password[i] on line 16. However, you'll also need to re-assign the encrypted char back to the string, so you could do something like simply do that back to password[i]. Better yet, you could do all that on one line and not use a separate char variable at all, something like:

1
2
3
4
string encstr; // moved this outside the for loop so it won't go out of scope when the loop ends

for (int i=0; i < password.size; i++)
      encstr[i] = (((((int)password[i])+1) - 1 + shift) % 127)+ 1;


I haven't tested that code so it may need some fine tuning, but that should help some. You might need to recast the result of the expression back to char before assigning it to encstr[i], not sure on that.

All in all though, the whole algorithm for the encoding could be simplified alot. If you look at it, you are adding 1, then subtracting one, then adding 2 before the modulus. Why not just add 2 after the cast? It reduces the code and the amount of work considerably with exactly the same effect.

encstr[i] = (((int)password[i] + 2) % 127)+ 1;




Edit: Moved encstr outside the for loop.
Last edited on
Wow!! Really your explanation is very clear and helpful!!
I've used this operator [] as a charactar array to store the password..
about password size: I mean put 3 and there is if stmt before it (in the whole program)

here it is
PLEASE ENTER YOUR PASSWORD
a12b
d45eā™„Press any key to continue . . .


Thank you so much ,Raezzor.
I appreciate it.
Last edited on
Topic archived. No new replies allowed.