How to change letters of string?
Hi, I'm working my way through a book called "Information Security: Principles and Practice". At the end of each chapter there are problems that the author deals out to make sure you comprehend what you just read. Well, a lot of the problems are encrypted strings using the ceasar cipher, and I'm trying to shift the letters in a string to decrypt the string. How can I do this in C++?
Thanks for the help,
Nathan
Re: How to change letters of string?
What kind of string are you using?
char *, std::string or CString?
In case it is a char *, you can do the following:
Code:
char *pCursor = pszString;
while('\0' != *pCursor)
{
*pCursor = *pCursor + whatever; // Here you can modify every single char
pCursor++; // advance to next char
}
In case of std::string or CString, there are functions for replacing strings.
Or, you could get a ptr to the bytes and then use what I wrote above.
Hope that helps,
Andy
Re: How to change letters of string?
I haven't written the program yet, cause I wasn't sure how :) In your code you posted does it actually change a letter? I don't understand it very well, sorry. I'm kind of new to C++ so thanks for the help :)
Re: How to change letters of string?
No, but in the loop you can add/subtract the number you want (I understand that is what Caesarian cipher is, adding 3 to 'A', so it becomes 'D'.
E.g.
'A' + 3 = 'D' and
'D' - 3 = 'A'
If you don't understand why, look at an ASCII table.
-Andy
Re: How to change letters of string?
Ok, but what does pszString mean?
Re: How to change letters of string?
How about;
Code:
int main()
{
std::string s = "Hello";
std::cout << s << std::endl;
for(int index=0; index < s.length(); index++)
{
s[ index ] = s[ index ] + 1;
}
std::cout << s << std::endl;
system("PAUSE");
return 0;
}
Output
Re: How to change letters of string?
So adding a number to a letter will shift the letter by the numbers amount?
Re: How to change letters of string?
Quote:
Originally Posted by Nathand
So adding a number to a letter will shift the letter by the numbers amount?
as suggested earlier look at the asciitable.
search asciitable on google...
Re: How to change letters of string?
@namezero111111: Warning, your encoded string may contain nul character.
Or, assuming that the source character set contains only A-Za-z and space, the destination character set doesn't match the source character set.
Re: How to change letters of string?
When I try to compile that C++ program I get: http://rafb.net/paste/results/fHSKTq95.html.
Re: How to change letters of string?
@Nathand: Check that you include the <string> and <iostream> headers (both of them must be included).
Re: How to change letters of string?
A proper cipher (assuming only capital letters in the source)
Code:
char *in = source;
char *out = *dest; // assuming dest is pre-allocated
while (*in)
{
*out = ((*in -'A') + offset) %26;
in++; // not optimal, but possibly easier to read for a beginner...
out++;
}
*out = 0; // terminate the output.
Re: How to change letters of string?
You were right :) I forgot to add "#include <string>.
Thanks,
Nathan
Re: How to change letters of string?
TheCPUWizard: Wouldn't it be:
Code:
*out = 'A' + ((*in -'A') + offset) %26;
:p
And if space characters have to be handled:
Code:
if (*in == ' ') *out=*in;
else *out = 'A' + ((*in -'A') + offset) %26;
Yes, I know, space characters are not encrypted, it's very weak, but AFAIK, the original caesar encryption algorithm does that.
Re: How to change letters of string?
Ok, last question :)
When I use this code:
Code:
#include <iostream>
#include <string>
int key = 1;
int main()
{
std::string s = "CSYEVIXIVQMREXIH";
std::cout << s << std::endl;
for(int i = 0; i < 26; i++)
{
for(int index=0; index < s.length(); index++)
{
s[ index ] = s[ index ] + key;
}
std::cout << s << std::endl;
key += 1;
}
system("PAUSE");
return 0;
}
The caesar's cipher is "youareterminated", but the program returns "_ouareterminated". Why would this be? This also happens with some other ciphers I tried.
And if someone feels so inclined to compile and run this on their system, let me know if you get a very strange "beeping" noise coming from your computer?
Thanks,
Nathan