Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <math.h>
- #include <vector>
- #include <windows.h>
- #include <random>
- #include <time.h>
- #include <fstream>
- using namespace std;
- string Crypt(string str) {
- int cipherSize = str.size() * 3;
- int n = sqrt(cipherSize);
- int m = cipherSize / n;
- if (cipherSize % n)
- m++;
- vector < vector <bool> > siele(n, vector <bool>(m, 1));
- vector < vector <char> > cifer(n, vector <char>(m));
- int holes = 0;
- while (holes < str.size()) {
- srand(time(0));
- int i = rand() % n;
- int j = rand() % m;
- if (siele[i][j] == 0)
- continue;
- siele[i][j] = 0;
- holes++;
- }
- ofstream f;
- f.open("siele.txt");
- f << m << '\n';
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- //cout << siele[i][j] << ' ';
- f << siele[i][j] << ' ';
- }
- //cout << '\n';
- f << '\n';
- }
- f.close();
- int cnt = 0;
- string ciferMsg = "";
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (siele[i][j] == 0) {
- cifer[i][j] = str[cnt];
- cnt++;
- }
- else {
- bool coin = rand() % 2;
- if (coin)
- cifer[i][j] = rand() % 26 + 'a';
- else
- cifer[i][j] = rand() % 26 + 'A';
- }
- ciferMsg += cifer[i][j];
- }
- }
- /*cout << '\n';
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++)
- cout << cifer[i][j] << ' ';
- cout << '\n';
- }*/
- return ciferMsg;
- }
- string Decrypt(string str) {
- int n, m;
- ifstream f;
- f.open("siele.txt");
- f >> m;
- n = str.size() / m;
- string res = "";
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- bool h;
- f >> h;
- if (!h)
- res += str[i * m + j];
- }
- }
- return res;
- }
- int main()
- {
- string msg = Crypt("IMI BSU");
- cout << msg << "\n\n";
- /*==---==*/
- cout << Decrypt(msg);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement