Decryption code

Pages: 12
So I was making my encryption / decryption program a little while back and someone gave me this code:

1
2
3
4
5
6
7
               size_t j = pdphrase.find_first_of('a');
               while(j != std::string::npos)
               {
                       pdphrase.replace(j, 1, "Z2");
                       j = pdphrase.find_first_of('a', j + 1);

               }


I used that and it works perfectly for encryption but when I go to use it for decrpytion:

1
2
3
4
5
6
7
8
              size_t j = pephrase.find_first_of("V62");
              while (j != std::string::npos)
              {
                    
                    pephrase.replace(j, 1, "a");
                    j = pephrase.find_first_of("V62", j + 1);
                    
              }


It prints out three a's instead of only 1. Does anyone see a quick fix to this? I tried changing it to find and changing the j + 1 to j + 3 but both of those changes made it print out a62.
Last edited on
Please? Can anyone figure out what I'm doing wrong?
Hi,

I do not understand that code. Could you please give a short explanation of the idea and what you are doing there?
I'm kind of confused my self as the person who gave it to me never explained it but I know it loops through the phrase that the user types in looking for the characters specified and changes it to the string on the 3rd line. Thats what I know must happen becuase thats what my Java program does(I'm trying to make this program in C++ and Java), but the problem is that the 2nd one makes 3 a's instead of 1 a. Also if I use getline(cin, pdphrase); its supposed to get the whole line even if there are no spaces. It won't work in this program though. Does it have to do with the way the max length of the string is calculated?
Last edited on
I did a little bit of searching around and here is my first post: http://www.cplusplus.com/forum/beginner/34321/
...I have the impression you want to do an easy encryption thing like substitution of letters in a text - am I right? If you don't understand the library you are using perhaps you better work it out yourself? something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;

class Code
{
public:
    int alphabetLength;
    char* clearAlphabet;
    char* encryptedAlphabet;
public:
    Code()
    {
        alphabetLength = 26;
        clearAlphabet =   "abcdefghijklmnopqrstuvwxyz";
        encryptedAlphabet="qwertzuiopasdfghjklyxcvbnm";
    }
};

void encrypt(char* text, int textLength, Code code)
{
    for(int i=0; i<textLength; i++)
    {
        for(int j=0; j<code.alphabetLength; j++)
        {
            if( text[ i ] == code.clearAlphabet[ j ])
            {
                text[ i ] = code.encryptedAlphabet[ j ];
                break;
            }
        }
    }
}
void decrypt(char* text, int textLength, Code code)
{
    for(int i=0; i<textLength; i++)
    {
        for(int j=0; j<code.alphabetLength; j++)
        {
            if( text[ i ] == code.encryptedAlphabet[ j ])
            {
                text[ i ] = code.clearAlphabet[ j ];
                break;
            }
        }
    }
}
int main()
{
    char* text = "hello world";
    
    cout << "original clear text: " << text << endl;
    encrypt( text, 11, Code() );
    cout << "encrypted text: " << text << endl;
    decrypt( text, 11, Code() );
    cout << "decrypted text: " << text << endl;
    
    // while(true);//wait
    char c;
    cin >> c;
}
Last edited on
Alright I'll try and figure out a new way to do it. By the way, your program crashes after it prints out original clear text: hello world. I don't know if that was intended or not, but I'm just pointing that out.
well - copy and paste it - it should wok ;)
i did copy and paste it.
...perhaps your compiler doesn't like while(true); statement? try again
Last edited on
I took it out and it still crashes. I've come to the conclusion that DevC++ sucks. I have programs that use the same syntax and they don't do the same thing, it says there are no matching function calls when there are...and suggestions for a new IDE with a different compiler?
..u are right - c++ really sucks!! its old and the compilers are really lame in supporting usefull error messages...

and all this stuff like pointers adresses delete clauses - with a modern IDE u do not even have to think about that things at all. I mean hello? did these c++ guys ever hear about a garbage collector? its all stupid automatism that these old compilers want you to do when developing with c++.

my advise: go and get yourself a modern IDE and develop with an up to date programming language like C#! IDE is for free: Visual Studio Express 2008 or 2010 ;)

here u are:
http://www.chip.de/downloads/c1_downloads_auswahl_24082797.html?t=1296396860&v=3600&s=927366d06f8241525be9d2c129717aa3
Last edited on
Whay are you posting in a C++ forum??
because sometimes someone just decides u ve to do it in c++...
but if one has free choice of language and is sane he will allways do it with c# - its default choice.

and by the way: c++ is not the name of a religion - its just a tool. ... and there are better tools. modern languages really support u as a developer by giving precise error messages and doing all that stuff like garbage collection... so u can concentrate on your work and that are these things the computer can not do - like develop tricky algorithms - not like collect garbage!!

Last edited on
I actually have to program this in C++. I'm doing a science fair project and for the project I have to make programs in Java and C++ that do the exact same thing. It sucks though because I'm a lot better at Java than C++ so the program in C++ are a lot harder for me to make. The only program that I have exactly the same is a program that does the distance formula. I have to make a C++ program that encrypts and decrypts stings which is what I'm trying to figure out here, and I have to make a calculator, I have squaring numbers, getting the square root, and the other basic operations but i have to add doing sine cosine and tangent and the inverses of those in the C++ program. All of these programs are done in Java but I just need to figure them out in C++.
Last edited on
cdoubleplus
you don't need to collect the garbage by yourself at most of the time by using C++
if you do need to do that, it means you don't know how to use C++ effectively
language itself is innocent, before you blame the language, maybe you should
ask yourself : the language fail me or I fail to take a good use of the language

besides, garbage collection is not that difficult with the help of destructor and smart pointer
If you're wondering why the code crashes, it's that by using char*, you opened up a possibility of a segmentation fault, which happened at line 27. I don't think it's the ancient MinGW compiler's fault this time.

EDIT: @OP: I'd be curious to see your whole code and the string you're running through it.

-Albatross
Last edited on
Does anyone have any suggestions on either how to fix the code from the first post or a better way to do this?
Well...

You should use find(), instead of find_first_of(). The former searches for a whole string, the latter searches for any character present in the string. ;)

I would do the encryption/decryption differently, however that's not too important. For reference, though, I'll put it down here.

I would define two maps, or one map and two functions to change it. One would have the alphabet as keys and the encrypted letters as values, the other would have the encrypted letters as keys and the decrypted letters as the values. From there, it's a simple single for loop without any ifs. This could be tricky for someone unfamiliar with maps, though.

EDIT4: Done editing. I hope.

-Albatross
Last edited on
i don't know what do you want to do
do you want to map 'a' to 'aaa' , 'b' to '6' or somthing like that?
maybe "map" or "multimap" would be a good choice

just some easy example
1
2
3
4
5
6
7
8
9
  std::map<char, char> dictionary;
  dictionary.insert(std::make_pair('a', 'A')); //in this example, 'a' is the  key, 'A' is the value
  dictionary.insert(std::make_pair('b', 'B'));
  dictionary.insert(std::make_pair('4', 'r'));
  std::map<char, char>::iterator it = dictionary.find('a');
  cout<<it->first<<", "<<it->second<<"\n";

  it = dictionary.find('4');
  cout<<it->first<<", "<<it->second<<"\n";

you could build one map to encryption and one to decryption like Albatross told you
Pretty easy and don't need to do any garbage collection jobs like cdoubleplus said
if you need to map a key to different values, you could use multimap
Last edited on
Pages: 12