Šiame įraše yra parodyti skirtingi teksto šifravimo pavyzdžiai, naudojant nuskaitymą iš failo ir rezultato įrašymą į failą.
Šifravimas, naudojant XOR metodą ir slaptažodį
#include <iostream> #include <fstream> using namespace std; int main(){ cout << "Kodavimas atliekamas, panaudojant XOR metoda\n \n"; //-------------Meniu1-------------------- cout << endl <<"Ka dariti?\n"; cout << "1. txt --> Code\n"; cout << "2. Code --> txt\n"; int in; cin >> in; string a,b; if(in==1){ a="input.txt"; b="output.txt"; } else { a="output.txt"; b="output2.txt"; } ifstream infile; ofstream outfile; infile.open (a.c_str()); outfile.open (b.c_str()); if (!infile) cout << "Failo nera" << endl; string s; char temp; infile.unsetf(ios::skipws); while(infile >> temp) s += temp; unsigned long key=0; string pass; cout << "Iveskite slaptazodi ir isimikite ji\n"; cin >> pass; for (int i=0; i<=pass.size(); i++) key=key+pass[i]; if(in==1){ for (int i = 0; i < s.size(); i++) s[i] = s[i] ^ key; outfile << s ; } if(in==2){ for (int i = 0; i < s.size(); i++) s[i] = s[i] ^ key; outfile << s ; } outfile.close(); system("PAUSE"); return EXIT_SUCCESS; } |
Šifravimas, naudojant ASCII kodus
Šis būdas tikrina raidės ASCII kodą, jeigu jis yra ltginis, prideda prie jo +2 ir išveda tą ženklą, jei nelyginis, tada -2.
#include <iostream> #include <fstream> using namespace std; inline bool odd(long value){ return (value & 1) == 1; } int main(){ cout << "1. Sifruoti\n"; cout << "2. Issifruoti\n"; int pasirinkimas; cin >> pasirinkimas; if (pasirinkimas == 1){ ifstream infile("infile.txt"); ofstream outfile("outfile.txt"); if (!infile) cout << "Failo nera" << endl; string s; char temp; infile.unsetf(ios::skipws); while(infile >> temp) s += temp; for (int i=0; i<s.size(); i++) { if (odd(i)==false){ temp=(char) s[i]+2; outfile << temp; } else{ temp=(char) s[i]-2; outfile << temp; } } outfile.close(); } else if (pasirinkimas==2){ ifstream infile("outfile.txt"); ofstream outfile("infile_new.txt"); if (!infile) cout << "Failo nera" << endl; string s; char temp; infile.unsetf(ios::skipws); while(infile >> temp) s += temp; for (int i=0; i<s.size(); i++) { if (odd(i)==false){ temp=(char) s[i]-2; outfile << temp; } else{ temp=(char) s[i]+2; outfile << temp; } } outfile.close(); } system("PAUSE"); return 0; } |