Hey.

I made a program to encrypt files. Encrypted normally. But back - not all the characters are the same as they were - change to something else. Help to figure out what's wrong. Already someone just did not ask. There's more statistics in the program, but it doesn't bother me at the moment. Here is the code:

#include <iostream> #include <fstream> #include <stdlib.h> #include <iomanip> using namespace std; void ZAPMASKEY(ifstream &, int *, int &); void NEWDMASS(int *&, int &); int main() { int S1 = 50; //Для ключей ifstream FDK("fdk.txt", ios::in | ios::binary); if (!FDK) { cout << "Can not open." << endl; return -1; } int *maskey = new int[S1]; //отправляет заполнять массив ключей ZAPMASKEY(FDK, maskey, S1); //S1 - число ключей //Основная часть char w1, w2; char file1[100]; char file2[100]; //int b; //int kode; int i = -1; cout << "Your file input:"; cin >> file1; cout << endl << "Your file output:"; cin >> file2; ifstream KF1(file1, ios::binary); ofstream KF2(file2, ios::binary); //шифрует файл while (KF1) { KF1.get(w1); if (!KF1) break; i++; KF2.put((maskey[i % S1] ^ w1) % 256); } KF1.close(); KF2.close(); //Для статистики char c; cout << endl << "Do you want to see statistics?:"; cin >> c; if (c == 'y') { char ck = 'y'; while (ck == 'y') { int d; int MASSS[256]; for (d = 0; d < 256; d++) MASSS[d] = 0; //char symbol; int code; cout << "symbol code: "; cin >> code; cout << "symbol: " << char (code) << endl; //symbol=char(code); //cout<<"symbol:"; cin>>symbol; ifstream KF3(file1, ios::binary); ifstream KF4(file2, ios::binary); //KF3.seekg(0); KF4.seekg(0); while (KF3) { KF3.get(w1); KF4.get(w2); if (w1 == char (code)) { //kode=static_cast<int>(w2); MASSS[unsigned char (w2)] = MASSS[unsigned char (w2)] + 1; } } for (d = 0; d < 256; d++) { if (d % 16 == 0) cout << endl; cout << setw(4) << MASSS[d]; } KF3.close(); KF4.close(); cout << endl << "More?"; cin >> ck; cout << endl; } } cout << endl; system("pause"); return 0; } void ZAPMASKEY(ifstream & FDKF, int *maskey, int &s1) { int b; char w; int i = 0; for (int j = 0; j < s1; j++) maskey[j] = 0; while (FDKF) { FDKF.get(w); if (w != ' ') //у меня в файле для ключей нет точек и переносов строки. я проверяла и у меня совпадало кол-во символов, размеры файлов, кол-во слов и т.д. { if (!FDKF) break; b = static_cast < int >(w); maskey[i] = maskey[i] + b; } else { //cout<<k<<endl; maskey[i] = maskey[i] % 256; i++; } if ((i - s1) >= 0) NEWDMASS(maskey, s1); } s1 = i + 1; //пробовала возвращать и просто i, но мало чем помогло } void NEWDMASS(int *&MASKEY, int &s) { int *maskey2 = new int[s * 2]; for (int i = 0; i < s * 2; i++) maskey2[i] = 0; for (int j = 0; j < s; j++) maskey2[j] = MASKEY[j]; delete[]MASKEY; MASKEY = maskey2; s *= 2; } 
  • @ Layla go better and cook borsch, I think it will be better than to write such bydlokod. Nothing personal. - Barmaley
  • one
    Yes, the code (to put it mildly) is lame, but the error is not yet visible ..... @HashCode, please do not delete, I will try to figure it out. - avp

1 answer 1

@ Layla , there was a mistake!

The first (obviously typo) is not compiled in line 74 unsigned char must be enclosed in brackets.

The second - because of her and did not work - work with links . In the declaration and definition instead of

 void ZAPMASKEY(ifstream&, int*, int&); 

and correspondingly

 void ZAPMASKEY (ifstream &FDKF, int *maskey, int &s1) { ... 

need to write

 void ZAPMASKEY(ifstream&, int*&, int&); 

and correspondingly

 void ZAPMASKEY (ifstream &FDKF, int *&maskey, int &s1) { ... 

because in ZAPMASKEY () you call NEWDMASS (), which changes the address in the memory of the maskey array.

Then I can only swear for a long time with the authors of C ++, who made the link mechanism in the language parallel with the pointers.

Regarding the question itself, I can say that it is not correctly formulated. In fact, both encryption and decryption did not work, because for a long key file (more than 50 words) the array, which is "xorim", was just random memory contents.

As to how to write (and not need to), I think that here and without me there will be enough people willing to speak.