Encryption

Pages: 12
closed account (4i67ko23)
Hi there!
I'm new to c++, I've tried to compile some code's, just to try it..
Well, encryption is very interresting to me, I've found this code:
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
#include <iostream>
#include <windows.h>
using std :: cout;
using std :: endl;
void encrypt( char [ ] ); // prototypes of functions used in the code
void decrypt( char * ePtr );
using namespace std;
int main( )
{
    // create a string to encrypt
    char string[ ] = "this is a secret!";
    cout << "Original string is: " << string << endl;
    encrypt( string );
    // call to the function encrypt( )
    cout << "Encrypted string is: " << string << endl;
    decrypt( string );
    ofstream encode;
    encode.open ("encoded.txt");
    encode << string;
    encode.close();
    // call to the function decrypt( )
    cout << "Decrypted string is: " << string << endl;
    ofstream decode;
    decode.open ("decoded.txt");
    decode << string;
    decode.close();
    Sleep(10000);
    return 0;
}// main

//encrypt data
void encrypt (char e[] ) 
{
    for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt

//decrypt data
void decrypt( char * ePtr ) 
{
    for( ; * ePtr != '\0'; ++ ePtr ) --(* ePtr);
}


It gives me an error: "18 C:\Users\Mathijs\Documents\C++\test7.cpp aggregate `std::ofstream encode' has incomplete type and cannot be defined " & "24 C:\Users\Mathijs\Documents\C++\test7.cpp aggregate `std::ofstream decode' has incomplete type and cannot be defined "

Can anyone help me with this?
-Mathijs
I'm also new to C++ but I think you have to #include <fstream> to use ofstream? Maby this will help, maby not. But it's worth a try? =)
closed account (4i67ko23)
Wow, I'm gone remove my profile because of shame...
Thanks Man!
closed account (4i67ko23)
Alride, another problem...
This is my source:
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
#include <iostream>
#include <windows.h>
#include <fstream>
using std :: cout;
using std :: endl;
void encrypt( char [ ] ); // prototypes of functions used in the code
void decrypt( char * ePtr );
using namespace std;
int main( )
{
    // create a string to encrypt
    char string[ ] = "this is a secret!";
    cout << "Original string is: " << string << endl;
    encrypt( string );
    // call to the function encrypt( )
    cout << "Encrypted string is: " << string << endl;
    decrypt( string );
    ofstream encode;
    encode.open ("encoded.txt");
    encode << string;
    encode.close();
    // call to the function decrypt( )
    cout << "Decrypted string is: " << string << endl;
    ofstream decode;
    decode.open ("decoded.txt");
    decode << string;
    decode.close();
    Sleep(10000);
    return 0;
}// main

//encrypt data
void encrypt (char e[] ) 
{
    for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt

//decrypt data
void decrypt( char * ePtr ) 
{
    for( ; * ePtr != '\0'; ++ ePtr ) --(* ePtr);
}

The output isn't good...

1
2
encoded.txt
this is a secret!

and
1
2
decoded.txt
this is a secret!


It's the same, what's wrong?
line 14: encrypt( string ); The string is encrypted.

line 17: decrypt( string ); The string is decrypted.

line 18-21:
1
2
3
4
ofstream encode;
encode.open ("encoded.txt");
encode << string;
encode.close();
The decrypted string is written to the file encoded.txt.

line 24-27:
1
2
3
4
ofstream decode;
decode.open ("decoded.txt");
decode << string;
decode.close();
The decrypted string is written to the file decoded.txt.

Last edited on
Wow, I'm gone remove my profile because of shame...
Thanks Man!


There is no shame in not getting it right from the start. I'm also new to C++ and there is so much to think about. Can't get it all to be perfect first time around, right? =)
Last edited on
closed account (4i67ko23)
Thanks man! It works now, great forum!
closed account (4i67ko23)
New problem, I want that the input (what must be encypted) is a text file
Code:
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
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
using std :: cout;
using std :: endl;
void encrypt( char [ ] ); // prototypes of functions used in the code
void decrypt( char * ePtr );
using namespace std;
int main( )
{
    string line;
    ifstream read_input ("text.txt");
    if (read_input.is_open())
    {
        while ( read_input.good() )
        {
            getline (read_input,line);
            char string[ ] = line; 
            cout << "Original string is: " << string << endl;
            encrypt( string );
            // call to the function encrypt( )
            cout << "Encrypted string is: " << string << endl;
            ofstream encode;
            encode.open ("encoded.txt");
            encode << string;
            encode.close();
            decrypt( string );
            // call to the function decrypt( )
            cout << "Decrypted string is: " << string << endl;
            ofstream decode;
            decode.open ("decoded.txt");
            decode << string;
            decode.close();
        }
        read_input.close();
    }
    else cout << "Unable to open file"; 
    Sleep(10000);
    return 0;
}// main

//encrypt data
void encrypt (char e[] ) 
{
    for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt

//decrypt data
void decrypt( char * ePtr ) 
{
    for( ; * ePtr != '\0'; ++ ePtr ) --(* ePtr);
}


Text file:
Hello123

The error:
19 C:\Users\Mathijs\Documents\C++\test7.cpp initializer fails to determine size of `string'

19 C:\Users\Mathijs\Documents\C++\test7.cpp invalid initializer
Last edited on
closed account (o3hC5Di1)
Hi Mathijs,

the line:

char string[ ] = line; //line 19

Is invalid.

when defining a standard array you always have to say how big it will be, so the compiler knows how much memory to reserve for it.

You would do this as such:

char string[100];

Which tells the compiler reserve memory for 100 items of type character (100 bytes).
Make the array large enough for it tohold the filename (easiest option) or read the size of the input file using ios::end and .tellg() ( http://cplusplus.com/reference/iostream/istream/tellg/ ) to determine the size of the input.

Hope that helps.

All the best,
NwN
Last edited on
closed account (4i67ko23)
Alride, I made the size 1000, one error is away, now the other one
My source:
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
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using std :: cout;
using std :: endl;
void encrypt( char [ ] ); // prototypes of functions used in the code
void decrypt( char * ePtr );
using namespace std;
int main( )
{
    string line;
    ifstream read_input ("text.txt");
    if (read_input.is_open())
    {
        while ( read_input.good() )
        {
            getline (read_input,line);
            char string[1000] = line; 
            cout << "Original string is: " << string << endl;
            encrypt( string );
            // call to the function encrypt( )
            cout << "Encrypted string is: " << string << endl;
            ofstream encode;
            encode.open ("encoded.txt");
            encode << string;
            encode.close();
            decrypt( string );
            // call to the function decrypt( )
            cout << "Decrypted string is: " << string << endl;
            ofstream decode;
            decode.open ("decoded.txt");
            decode << string;
            decode.close();
        }
        read_input.close();
    }
    else cout << "Unable to open file"; 
    Sleep(10000);
    return 0;
}// main

//encrypt data
void encrypt (char e[] ) 
{
    for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt

//decrypt data
void decrypt( char * ePtr ) 
{
    for( ; * ePtr != '\0'; ++ ePtr ) --(* ePtr);
}


The error:
20 C:\Users\Mathijs\Documents\C++\test7.cpp invalid initializer
thanks all for your help :D
Last edited on
What is the point of char string[1000] ?
closed account (4i67ko23)
reserve enough bytes, I think, can anyone fix the error?
20 C:\Users\Mathijs\Documents\C++\test7.cpp invalid initializer
Last edited on
Just use a string instead.
closed account (4i67ko23)
Yes, but I want that the string is read from a file
anyone?
Last edited on
closed account (4i67ko23)
anyone can help?
closed account (o3hC5Di1)
Little bit more patience Mathijs :)
There are more people than yourself waiting for answers.

Coming to think of it - you dont't need to use that string[] variable, just do:

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
getline (read_input,line);  //input is already read into variable "line"
            cout << "Original string is: " << line << endl;
            encrypt( line );
            // call to the function encrypt( )
            cout << "Encrypted string is: " << line << endl;
            ofstream encode;
            encode.open ("encoded.txt");
            encode << line;
            encode.close();
            decrypt( line );
            // call to the function decrypt( )
            cout << "Decrypted string is: " << line << endl;
            ofstream decode;
            decode.open ("decoded.txt");
            decode << line;
            decode.close();


Also, "string" is not a valid name for a variable because it has the same name as the datatype.
Choose variable names carefully, to represent their contents, in this case "inputstring" for example.

All the best,
NwN
Last edited on
closed account (4i67ko23)
I'm sorry man, I'm just excited to test it :D
closed account (4i67ko23)
Another error, two the same,,
Source:
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
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

#define ENCRYPT "123456789 - abcdefg"

using std :: cout;
using std :: endl;
void encrypt( char [ ] ); // prototypes of functions used in the code
void decrypt( char * ePtr );
using namespace std;
int main( )
{
    string line;
    ifstream read_input ("text.txt");
    if (read_input.is_open())
    {
        while ( read_input.good() )
        {
            getline (read_input,line);  //input is already read into variable "line"
            cout << "Original string is: " << line << endl;
            encrypt( line );
            // call to the function encrypt( )
            cout << "Encrypted string is: " << line << endl;
            ofstream encode;
            encode.open ("encoded.txt");
            encode << line;
            encode.close();
            decrypt( line );
            // call to the function decrypt( )
            cout << "Decrypted string is: " << line << endl;
            ofstream decode;
            decode.open ("decoded.txt");
            decode << line;
            decode.close();
        }
        read_input.close();
    }
    else cout << "Unable to open file"; 
    Sleep(10000);
    return 0;
}// main

//encrypt data
void encrypt (char e[] ) 
{
    for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt

//decrypt data
void decrypt( char * ePtr ) 
{
    for( ; * ePtr != '\0'; ++ ePtr ) --(* ePtr);
}


Errors:
23 C:\Users\Mathijs\Documents\C++\encrypter.cpp cannot convert `std::string' to `char*' for argument `1' to `void encrypt(char*)'


30 C:\Users\Mathijs\Documents\C++\encrypter.cpp cannot convert `std::string' to `char*' for argument `1' to `void decrypt(char*)'
Last edited on
closed account (o3hC5Di1)
Try this:

1
2
encrypt( line.c_str() );  //line 23
decrypt( line.c_str() ); //line 30 


All the best,
NwN
@NwN : Most likely, this doesn't work. c_str() function returns a const char*, but the function encrypt has a parameter of char*.
Pages: 12