Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- using namespace std;
- string chiffre(string cleartext, int cle)
- {
- string cryptext{""};
- int val{0};
- for(int i{0}; i < cleartext.size(); i++)
- {
- if((cleartext[i] + cle) > 126)
- {
- val = ((cleartext[i] + cle) % 127) + 32;
- cryptext.push_back(char(val));
- }
- else
- {
- cryptext.push_back(char(cleartext[i] + cle));
- }
- }
- return cryptext;
- }
- string dechiffre(string cryptext, int cle)
- {
- string cleartext{""};
- int val{0};
- for(int i{0}; i < cryptext.size(); i++)
- {
- if((cryptext[i] - cle) < 32)
- {
- val = (95 + (cryptext[i] - cle));
- while(val < 32)
- {
- val = (95 + (val));
- }
- if(val < 0)
- {
- val = val * -1;
- cleartext.push_back(char(val));
- }
- else
- {
- cleartext.push_back(char(val));
- }
- }
- else
- {
- cleartext.push_back(char(cryptext[i] - cle));
- }
- }
- return cleartext;
- }
- void filetociph(string filename, int cle)
- {
- string ligne;
- string temp;
- ifstream fin(filename);
- while(getline(fin, ligne))
- {
- ofstream fout("ciph1.txt", ios::app);
- temp = chiffre(ligne, cle);
- fout << temp << endl;
- }
- }
- void ciphfiletotext(string filename, int cle)
- {
- string ligne;
- string temp;
- ifstream fin(filename);
- while(getline(fin, ligne))
- {
- ofstream fout("text1.txt", ios::app);
- temp = dechiffre(ligne, cle);
- fout << temp << endl;
- }
- }
- void unknowkey(string filename)
- {
- string ligne;
- string temp;
- ofstream fout("unknowtext.txt", ios::app);
- for(int i{1}; i < 95; i++)
- {
- ifstream fin(filename);
- fout << endl;
- fout << i;
- fout << endl;
- while(getline(fin, ligne))
- {
- temp = dechiffre(ligne, i);
- fout << temp << endl;
- }
- fout << "\n\n\n";
- fin.close();
- }
- }
- int main()
- {
- filetociph("C:\\Users\\Z.GHANMI\\Desktop\\caesreer\\text.txt", 50);
- ciphfiletotext("C:\\Users\\Z.GHANMI\\Desktop\\caesreer\\ciph1.txt", 50);
- unknowkey("C:\\Users\\Z.GHANMI\\Desktop\\caesreer\\ciph1.txt");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement