Hi,
I'm trying to encrypt a simple .txt file. What I want to do is is described by the function definitions. I want to change all my characters in the .txt file to lower case and then convert any digits found into *. I've done this before in the past, but I wasn't using a file. My output file is not receiving the changes.

Any help would be appreciated.

Brent

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

void GetLetter(ifstream& in_stream, ofstream& out_stream);
void UnderscoreToSpace(ifstream& in_stream, ofstream& out_stream); //needs the input file & output file; & means file address - call by reference.
void ChangeToLowerCase(ifstream& in_stream1, ofstream& out_stream1);
void NumToSymbol(ifstream& in_stream2, ofstream& out_stream2);


int main( )
{
	// Declarations
    ifstream fin; // input file
    ofstream fout; // output file
	ifstream fin1; 
    ofstream fout1;
	ifstream fin2; 
    ofstream fout2;
	ifstream fin0; 
    ofstream fout0;

    cout << "Begin editing files.\n"; // Declares the file is working

	// Attach the internal file(fin.open) to the external file ( cad.dat)
	fin.open("C:\\Documents and Settings\\Brent\\My Documents\\textIn.txt"); //(directory - file name)
    if (fin.fail( )) // if the data doesn't load
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

	// open output file
	fout.open("C:\\Documents and Settings\\Brent\\My Documents\\textOut.txt"); //(directory - file name)
    if (fout.fail( ))// if the data doesn't load
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }

	
	GetLetter(fin0, fout0);
    UnderscoreToSpace(fin, fout); //function to process the input & output file - file pointer
	ChangeToLowerCase(fin1, fout1);
	NumToSymbol(fin2, fout2);

    fin.close( );
    fout.close( );

    cout << "\n\nEnd of editing files.\n";
	system("pause");
    return 0;
}

void GetLetter(ifstream& in_stream0, ofstream& out_stream0)// function header or definition
{
    char next; // char = character data; 1 byte variable

    in_stream0.get(next); // get function 'gets the next character in line; priming read

	while (! in_stream0.eof( ))// eof = end of file; while (! or not) we don't have an end of file marker
 
	for(int n=0; n<6; n++)
	{
		next = toupper(next +10);
		cout <<next<<endl;
	}
}

void UnderscoreToSpace(ifstream& in_stream, ofstream& out_stream)// function header or definition
{
    char next; // char = character data; 1 byte variable

    in_stream.get(next); // get function 'gets the next character in line; priming read

	while (! in_stream.eof( ))// eof = end of file; while (! or not) we don't have an end of file marker
    {
        if (next == '_')
            out_stream << " "; // file out while cout is monitor out
        else
            out_stream.put(next);
		
        in_stream.get(next);
		
    }
}

void ChangeToLowerCase(ifstream& in_stream1, ofstream& out_stream1)
{
    char next; 

    in_stream1.get(next); 

	while (! in_stream1.eof( ))
    {
	if(isupper(next))
	
		out_stream1 << static_cast<char>(tolower(next));
	
	else
		out_stream1.put(next);

	in_stream1.get(next);
	}	
    
}

void NumToSymbol(ifstream& in_stream2, ofstream& out_stream2)
{
    char next; 

    in_stream2.get(next); 

	while (! in_stream2.eof( ))
    {
		if(isdigit(next))
		    out_stream2 << '*'; 
		else 
			out_stream2.put(next);

		in_stream2.get(next);
		
    }
}

If you want to encrypt something, why wouldn't you use like a simple RSA encryption?

Somethin like this;

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int M=0; //input number (unencrypted).
    int C; //output number (encrypted).
    unsigned long long calcul; //used to store calculations.
    int a=0;
    int n=143; //half of the key.
    int e=53; //half of the public key.
    int d=77; //half of the private key.    

while (a<=143){
    calcul = pow(M,2); calcul = calcul %n;    
    calcul = calcul*M; calcul = calcul %n;
    calcul = pow(calcul,2); calcul = calcul %n;   
    calcul = pow(calcul,2); calcul = calcul %n; 
    calcul = calcul*M; calcul = calcul %n;
    calcul = pow(calcul,2); calcul = calcul %n; 
    calcul = pow(calcul,2); calcul = calcul %n; 
    calcul = calcul*M; calcul = calcul %n;   
    C = calcul %n; //set "C" to the remainder when "calcul" is divided by "n".   
    cout << "Your encrypted number to:\t" << M << "\tis:\t" << C << endl; //write output number "C".
    M++;
    a++;
}
    system("PAUSE"); //Pause program, I know I shouldn't use the "system("Pause")" command, but it's just for testing
    return 0;
}

Simply change any input to ASCII decimal, and use that number in the RSA encryption, then make the new decimal into ASCII char, and you'll have a encrypted text for the original?

The encryption will output the following, based on input;

Your encrypted number to:	0	is:	0
Your encrypted number to:	1	is:	1
Your encrypted number to:	2	is:	19
Your encrypted number to:	3	is:	126
Your encrypted number to:	4	is:	75
Your encrypted number to:	5	is:	70
Your encrypted number to:	6	is:	106
Your encrypted number to:	7	is:	24
Your encrypted number to:	8	is:	138
Your encrypted number to:	9	is:	3
Your encrypted number to:	10	is:	43
Your encrypted number to:	11	is:	33
Your encrypted number to:	12	is:	12
Your encrypted number to:	13	is:	52
Your encrypted number to:	14	is:	27
Your encrypted number to:	15	is:	97
Your encrypted number to:	16	is:	48
Your encrypted number to:	17	is:	62
Your encrypted number to:	18	is:	57
Your encrypted number to:	19	is:	28
Your encrypted number to:	20	is:	102
Your encrypted number to:	21	is:	21
Your encrypted number to:	22	is:	55
Your encrypted number to:	23	is:	56
Your encrypted number to:	24	is:	85
Your encrypted number to:	25	is:	38
Your encrypted number to:	26	is:	130
Your encrypted number to:	27	is:	92
Your encrypted number to:	28	is:	84
Your encrypted number to:	29	is:	35
Your encrypted number to:	30	is:	127
Your encrypted number to:	31	is:	135
Your encrypted number to:	32	is:	54
Your encrypted number to:	33	is:	11
Your encrypted number to:	34	is:	34
Your encrypted number to:	35	is:	107
Your encrypted number to:	36	is:	82
Your encrypted number to:	37	is:	20
Your encrypted number to:	38	is:	103
Your encrypted number to:	39	is:	117
Your encrypted number to:	40	is:	79
Your encrypted number to:	41	is:	6
Your encrypted number to:	42	is:	113
Your encrypted number to:	43	is:	10
Your encrypted number to:	44	is:	44
Your encrypted number to:	45	is:	67
Your encrypted number to:	46	is:	63
Your encrypted number to:	47	is:	60
Your encrypted number to:	48	is:	42
Your encrypted number to:	49	is:	4
Your encrypted number to:	50	is:	7
Your encrypted number to:	51	is:	90
Your encrypted number to:	52	is:	39
Your encrypted number to:	53	is:	14
Your encrypted number to:	54	is:	32
Your encrypted number to:	55	is:	22
Your encrypted number to:	56	is:	23
Your encrypted number to:	57	is:	96
Your encrypted number to:	58	is:	93
Your encrypted number to:	59	is:	141
Your encrypted number to:	60	is:	125
Your encrypted number to:	61	is:	29
Your encrypted number to:	62	is:	134
Your encrypted number to:	63	is:	72
Your encrypted number to:	64	is:	25
Your encrypted number to:	65	is:	65
Your encrypted number to:	66	is:	66
Your encrypted number to:	67	is:	45
Your encrypted number to:	68	is:	74
Your encrypted number to:	69	is:	49
Your encrypted number to:	70	is:	31
Your encrypted number to:	71	is:	15
Your encrypted number to:	72	is:	128
Your encrypted number to:	73	is:	112
Your encrypted number to:	74	is:	94
Your encrypted number to:	75	is:	69
Your encrypted number to:	76	is:	98
Your encrypted number to:	77	is:	77
Your encrypted number to:	78	is:	78
Your encrypted number to:	79	is:	118
Your encrypted number to:	80	is:	71
Your encrypted number to:	81	is:	9
Your encrypted number to:	82	is:	114
Your encrypted number to:	83	is:	18
Your encrypted number to:	84	is:	2
Your encrypted number to:	85	is:	50
Your encrypted number to:	86	is:	47
Your encrypted number to:	87	is:	120
Your encrypted number to:	88	is:	121
Your encrypted number to:	89	is:	111
Your encrypted number to:	90	is:	129
Your encrypted number to:	91	is:	104
Your encrypted number to:	92	is:	53
Your encrypted number to:	93	is:	136
Your encrypted number to:	94	is:	139
Your encrypted number to:	95	is:	101
Your encrypted number to:	96	is:	83
Your encrypted number to:	97	is:	80
Your encrypted number to:	98	is:	76
Your encrypted number to:	99	is:	99
Your encrypted number to:	100	is:	133
Your encrypted number to:	101	is:	30
Your encrypted number to:	102	is:	137
Your encrypted number to:	103	is:	64
Your encrypted number to:	104	is:	26
Your encrypted number to:	105	is:	40
Your encrypted number to:	106	is:	123
Your encrypted number to:	107	is:	61
Your encrypted number to:	108	is:	36
Your encrypted number to:	109	is:	109
Your encrypted number to:	110	is:	132
Your encrypted number to:	111	is:	89
Your encrypted number to:	112	is:	8
Your encrypted number to:	113	is:	16
Your encrypted number to:	114	is:	108
Your encrypted number to:	115	is:	59
Your encrypted number to:	116	is:	51
Your encrypted number to:	117	is:	13
Your encrypted number to:	118	is:	105
Your encrypted number to:	119	is:	58
Your encrypted number to:	120	is:	87
Your encrypted number to:	121	is:	88
Your encrypted number to:	122	is:	122
Your encrypted number to:	123	is:	41
Your encrypted number to:	124	is:	115
Your encrypted number to:	125	is:	86
Your encrypted number to:	126	is:	81
Your encrypted number to:	127	is:	95
Your encrypted number to:	128	is:	46
Your encrypted number to:	129	is:	116
Your encrypted number to:	130	is:	91
Your encrypted number to:	131	is:	131
Your encrypted number to:	132	is:	110
Your encrypted number to:	133	is:	100
Your encrypted number to:	134	is:	140
Your encrypted number to:	135	is:	5
Your encrypted number to:	136	is:	119
Your encrypted number to:	137	is:	37
Your encrypted number to:	138	is:	73
Your encrypted number to:	139	is:	68
Your encrypted number to:	140	is:	17
Your encrypted number to:	141	is:	124
Your encrypted number to:	142	is:	142
Your encrypted number to:	143	is:	0

wow!! Thanks. I'll have to try and convert that to a function.

Brent

wow!! Thanks. I'll have to try and convert that to a function.

Brent

You could try to use; the following code to convert ASCII to decimal, so you're able to encrypt your input, you could change the vector into an array if you want to, but in that way you would lose the dynamic.

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
    char string[1024];
    int a;
    vector<int> BufferVector;
      
    cout << "Indtast dit input [ASCII]\n";
    gets(string);
    cout << endl << "Decimal output [Decimal]\n";
    for(a = 0; a < strlen(string); a++)
     {
     BufferVector.push_back (("%d ", string[a]));
     }

   cout << endl << endl << "Variable skrivning er fuldført!" << endl;

   for (a = 0; a < BufferVector.size(); a++)
       cout << "random værdi; " << BufferVector[a] << endl;

   cin.get();
   return 0;
   }

If you're using this to mod your input, simply send the vector to the function and like run with the vector instead of "a", by removing the while loop around it. you could make a for loop, calling the vector, and like, running it with the same argument as I'm using in my:

for (a = 0; a < BufferVector.size(); a++)
       cout << "random værdi; " << BufferVector[a] << endl;

Then just looping the encryption instead, and btw, this encryption would send the same encrypted value each time, since it's ALWAYS based upon the same key, like:

int n=143; //half of the key.
int e=53; //half of the public key.
int d=77; //half of the private key.

You really should generate some primes, and like save the key, then encrypt with that, sending the "n and e" value for decrypting, as that will be able to and "n and d" for encrypting.

The key's are found using inverse modulo, and if you're interested in making your own key, then like try google "inverse modulo" among with "RSA encryption" or somethin like "Primenumber Encryption".

wow!! Thanks. I'll have to try and convert that to a function.

Brent

You could try to use; the following code to convert ASCII to decimal, so you're able to encrypt your input, you could change the vector into an array if you want to, but in that way you would lose the dynamic.

#include <iostream>
#include <vector>
using namespace std;

int main()
    {
    char string[1024];
    int a;
    vector<int> BufferVector;
      
    cout << "Indtast dit input [ASCII]\n";
    gets(string);
    cout << endl << "Decimal output [Decimal]\n";
    for(a = 0; a < strlen(string); a++)
     {
     BufferVector.push_back (("%d ", string[a]));
     }

   cout << endl << endl << "Variable skrivning er fuldført!" << endl;

   for (a = 0; a < BufferVector.size(); a++)
       cout << "random værdi; " << BufferVector[a] << endl;

   cin.get();
   return 0;
   }

If you're using this to mod your input, simply send the vector to the function and like run with the vector instead of "a", by removing the while loop around it. you could make a for loop, calling the vector, and like, running it with the same argument as I'm using in my:

for (a = 0; a < BufferVector.size(); a++)
       cout << "random værdi; " << BufferVector[a] << endl;

Then just looping the encryption instead, and btw, this encryption would send the same encrypted value each time, since it's ALWAYS based upon the same key, like:

int n=143; //half of the key.
int e=53; //half of the public key.
int d=77; //half of the private key.

You really should generate some primes, and like save the key, then encrypt with that, sending the "n and e" value for decrypting, as that will be able to and "n and d" for encrypting.

The key's are found using inverse modulo, and if you're interested in making your own key, then like try google "inverse modulo" among with "RSA encryption" or somethin like "Primenumber Encryption".

EDIT: if you were to encrypt a text file, then like just load the text file info into the char array, called "string", and then you'll be ready to go :)

btw, you'll need the following code to decrypt, again this is build around the "n and public key", where the encrypt algoritm was build around the "n and the private key", so this is when you want to decrypt the encrypted file:

calcul = pow(M,2); calcul = calcul %n;    
        calcul = pow(calcul,2); calcul = calcul %n;
        calcul = pow(calcul,2); calcul = calcul %n;
        calcul = calcul*M; calcul = calcul %n;
        calcul = pow(calcul,2); calcul = calcul %n;  
        calcul = calcul*M; calcul = calcul %n;
        calcul = pow(calcul,2); calcul = calcul %n;  
        calcul = pow(calcul,2); calcul = calcul %n; 
        calcul = calcul*M; calcul = calcul %n;  
        C = calcul %n; //set "C" to the remainder when "calcul" is divided by "n".

I thought that I should add, like the full testing unit for the decryption, and the results it givin me; so here goes:

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int M; //input number (unencrypted).
    int C; //output number (encrypted).
    unsigned long long calcul; //used to store calculations.
    int a;
    int n=143; //half of the key.
    int e=53; //half of the public key.
    int d=77; //half of the private key.    

//    cout << "You choose to encrypt, please enter the number, you want to encrypt"; //ask for input.
//    cin >> M; //get input.
M=0;
a=0;
while (a<=143){
    calcul = pow(M,2); calcul = calcul %n;    
    calcul = pow(calcul,2); calcul = calcul %n;
    calcul = pow(calcul,2); calcul = calcul %n;
    calcul = calcul*M; calcul = calcul %n;
    calcul = pow(calcul,2); calcul = calcul %n;  
    calcul = calcul*M; calcul = calcul %n;
    calcul = pow(calcul,2); calcul = calcul %n;  
    calcul = pow(calcul,2); calcul = calcul %n; 
    calcul = calcul*M; calcul = calcul %n;   
    C = calcul %n; //set "C" to the remainder when "calcul" is divided by "n".   
    cout << "Your decrypted number to:\t" << M << "\tis:\t" << C << endl; //write output number "C".
    M++;
    a++;
}

    system("PAUSE"); //Pause program, I know I shouldn't use the "system("Pause")" command, but it's just for testing
    return 0;
}

The decryption will output the following, based on input;

Your decrypted number to:	0	is:	0
Your decrypted number to:	1	is:	1
Your decrypted number to:	2	is:	84
Your decrypted number to:	3	is:	9
Your decrypted number to:	4	is:	49
Your decrypted number to:	5	is:	135
Your decrypted number to:	6	is:	41
Your decrypted number to:	7	is:	50
Your decrypted number to:	8	is:	112
Your decrypted number to:	9	is:	81
Your decrypted number to:	10	is:	43
Your decrypted number to:	11	is:	33
Your decrypted number to:	12	is:	12
Your decrypted number to:	13	is:	117
Your decrypted number to:	14	is:	53
Your decrypted number to:	15	is:	71
Your decrypted number to:	16	is:	113
Your decrypted number to:	17	is:	140
Your decrypted number to:	18	is:	83
Your decrypted number to:	19	is:	2
Your decrypted number to:	20	is:	37
Your decrypted number to:	21	is:	21
Your decrypted number to:	22	is:	55
Your decrypted number to:	23	is:	56
Your decrypted number to:	24	is:	7
Your decrypted number to:	25	is:	64
Your decrypted number to:	26	is:	104
Your decrypted number to:	27	is:	14
Your decrypted number to:	28	is:	19
Your decrypted number to:	29	is:	61
Your decrypted number to:	30	is:	101
Your decrypted number to:	31	is:	70
Your decrypted number to:	32	is:	54
Your decrypted number to:	33	is:	11
Your decrypted number to:	34	is:	34
Your decrypted number to:	35	is:	29
Your decrypted number to:	36	is:	108
Your decrypted number to:	37	is:	137
Your decrypted number to:	38	is:	25
Your decrypted number to:	39	is:	52
Your decrypted number to:	40	is:	105
Your decrypted number to:	41	is:	123
Your decrypted number to:	42	is:	48
Your decrypted number to:	43	is:	10
Your decrypted number to:	44	is:	44
Your decrypted number to:	45	is:	67
Your decrypted number to:	46	is:	128
Your decrypted number to:	47	is:	86
Your decrypted number to:	48	is:	16
Your decrypted number to:	49	is:	69
Your decrypted number to:	50	is:	85
Your decrypted number to:	51	is:	116
Your decrypted number to:	52	is:	13
Your decrypted number to:	53	is:	92
Your decrypted number to:	54	is:	32
Your decrypted number to:	55	is:	22
Your decrypted number to:	56	is:	23
Your decrypted number to:	57	is:	18
Your decrypted number to:	58	is:	119
Your decrypted number to:	59	is:	115
Your decrypted number to:	60	is:	47
Your decrypted number to:	61	is:	107
Your decrypted number to:	62	is:	17
Your decrypted number to:	63	is:	46
Your decrypted number to:	64	is:	103
Your decrypted number to:	65	is:	65
Your decrypted number to:	66	is:	66
Your decrypted number to:	67	is:	45
Your decrypted number to:	68	is:	139
Your decrypted number to:	69	is:	75
Your decrypted number to:	70	is:	5
Your decrypted number to:	71	is:	80
Your decrypted number to:	72	is:	63
Your decrypted number to:	73	is:	138
Your decrypted number to:	74	is:	68
Your decrypted number to:	75	is:	4
Your decrypted number to:	76	is:	98
Your decrypted number to:	77	is:	77
Your decrypted number to:	78	is:	78
Your decrypted number to:	79	is:	40
Your decrypted number to:	80	is:	97
Your decrypted number to:	81	is:	126
Your decrypted number to:	82	is:	36
Your decrypted number to:	83	is:	96
Your decrypted number to:	84	is:	28
Your decrypted number to:	85	is:	24
Your decrypted number to:	86	is:	125
Your decrypted number to:	87	is:	120
Your decrypted number to:	88	is:	121
Your decrypted number to:	89	is:	111
Your decrypted number to:	90	is:	51
Your decrypted number to:	91	is:	130
Your decrypted number to:	92	is:	27
Your decrypted number to:	93	is:	58
Your decrypted number to:	94	is:	74
Your decrypted number to:	95	is:	127
Your decrypted number to:	96	is:	57
Your decrypted number to:	97	is:	15
Your decrypted number to:	98	is:	76
Your decrypted number to:	99	is:	99
Your decrypted number to:	100	is:	133
Your decrypted number to:	101	is:	95
Your decrypted number to:	102	is:	20
Your decrypted number to:	103	is:	38
Your decrypted number to:	104	is:	91
Your decrypted number to:	105	is:	118
Your decrypted number to:	106	is:	6
Your decrypted number to:	107	is:	35
Your decrypted number to:	108	is:	114
Your decrypted number to:	109	is:	109
Your decrypted number to:	110	is:	132
Your decrypted number to:	111	is:	89
Your decrypted number to:	112	is:	73
Your decrypted number to:	113	is:	42
Your decrypted number to:	114	is:	82
Your decrypted number to:	115	is:	124
Your decrypted number to:	116	is:	129
Your decrypted number to:	117	is:	39
Your decrypted number to:	118	is:	79
Your decrypted number to:	119	is:	136
Your decrypted number to:	120	is:	87
Your decrypted number to:	121	is:	88
Your decrypted number to:	122	is:	122
Your decrypted number to:	123	is:	106
Your decrypted number to:	124	is:	141
Your decrypted number to:	125	is:	60
Your decrypted number to:	126	is:	3
Your decrypted number to:	127	is:	30
Your decrypted number to:	128	is:	72
Your decrypted number to:	129	is:	90
Your decrypted number to:	130	is:	26
Your decrypted number to:	131	is:	131
Your decrypted number to:	132	is:	110
Your decrypted number to:	133	is:	100
Your decrypted number to:	134	is:	62
Your decrypted number to:	135	is:	31
Your decrypted number to:	136	is:	93
Your decrypted number to:	137	is:	102
Your decrypted number to:	138	is:	8
Your decrypted number to:	139	is:	94
Your decrypted number to:	140	is:	134
Your decrypted number to:	141	is:	59
Your decrypted number to:	142	is:	142
Your decrypted number to:	143	is:	0

EDIT: I might add that the "n" value defines the greatest value, you're able to encrypt and decrypt, you'll see that the 143 equals 0, and therefore beyond 142, this encryption wont work, however 142 is perfectly fine, for the ASCII system, as it contains 128 different values.
If you want to double encrypt, then this is possible ofc. just remember to double decrypt as well :)

Let me know if you need any other help, then what I gave u so far

Be a part of the DaniWeb community

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