Skip to content

Instantly share code, notes, and snippets.

@pif
Created February 14, 2011 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pif/825660 to your computer and use it in GitHub Desktop.
Save pif/825660 to your computer and use it in GitHub Desktop.
chastokil
#include <iostream>
#include <string>
#include <fstream>
//#include <stdlib.h>
using namespace std;
int* generateHeightArray(int h, int mesLen) {
int* idxs = new int[mesLen];
int pos = 0;
int offset = 1;
for (int i = 0; i < mesLen; ++i) {
idxs[i] = pos;
pos += offset;
if (pos == h - 1) offset = -1;
if (pos == 0) offset = 1;
}
// for (int i = 0; i < mesLen; ++i) cout << idxs[i];
// cout << endl;
return idxs;
}
string encrypt(string m, int k) {
int* idxs = generateHeightArray(k, m.length());
string* arr = new string[k];
for (int i = 0; i < k; ++i) {
arr[i] = "";
}
for (int i = 0; i < m.length(); ++i) {
arr[idxs[i]] += m[i];
}
string res = "";
for (int i = k - 1; i >= 0; --i) {
res += arr[i];
}
return res;
}
string encrypt(string m, int k, int len) {
string res = "";
int pos = 0;
do {
res+=encrypt(m.substr(pos,len),k);
pos+=len;
} while (pos <= m.length());
return res;
}
string decrypt(string m, int k) {
int mLen = m.length();
int* idxs = generateHeightArray(k, mLen);
int* lens = new int[k];
for (int i = 0; i < k; ++i) {
lens[i] = 0;
}
for (int i = 0; i < mLen; ++i) {
lens[idxs[i]]++;
}
string* arr = new string[k];
for (int i = 0; i < k; ++i) {
arr[i] = "";
}
int pos = mLen;
for (int i = 0; i < k; ++i) {
pos -= lens[i];
arr[i] = m.substr(pos, lens[i]);
// cout << "current pos\t" << pos << "\tphrase\t" << arr[i] << endl;
}
string res = "";
int* arrPos = new int[k];
for (int i = 0; i < k; i++) {
arrPos[i] = 0;
}
// for (int i = 0; i < k; i++) {
// cout << "arr\t" << i << ":\t" << arr[i] << endl;
// }
// cout << endl;
for (int i = 0; i < mLen; ++i) {
res += arr[idxs[i]][arrPos[idxs[i]]];
++arrPos[idxs[i]];
}
return res;
}
string decrypt(string m, int k, int len) {
string res = "";
int pos = 0;
do {
res+=decrypt(m.substr(pos,len),k);
pos+=len;
} while (pos <= m.length());
return res;
}
void printHelp() {
cout << "Utility to crypt text using drabynka: 01232101232..." << endl;
cout << "Usage:" << endl;
cout << "lesenka e|d KEY LEN INPUT OUTPUT" << endl << endl;
cout << "Command-line options: " << endl;
cout << "\te - ecrypt file" << endl;
cout << "\td - decrypt file" << endl;
cout << endl;
cout << "KEY - positive integer. key to crypt/decrypt with." << endl << endl;
cout << "LEN - positive integer. length of block." << endl << endl;
cout << "INPUT and OUTPUT are filenames." << endl;
}
int main(int argc, const char* argv[]) {
/*for(int i=0;i<argc;++i)
cout<<argv[i]<<endl;
*/
if (argc != 6) {
printHelp();
return 1;
}
bool d = true;
if (argv[1][0] != '-') {
printHelp();
return 1;
}
char command = argv[1][1];
int key = atoi(argv[2]);
if (key<=0) {
printHelp();
return -1;
}
int length = atoi(argv[3]);
if (length<=0) {
printHelp();
return -1;
}
ifstream ifs(argv[4], ifstream::in);
ofstream ofs(argv[5], ofstream::out);
if (ifs == NULL || ofs == NULL) {
printHelp();
return -2;
}
switch (command) {
case 'e':
d = false;
case 'd':
cout << "Processing file:\t" << argv[4] << endl;
while (ifs.good()) {
string str;
getline(ifs, str);
if (d) {
ofs << decrypt(str, key, length);
} else {
ofs << encrypt(str, key, length);
}
if (!ifs.eof()) {
ofs<<endl;
}
}
cout << "File " << argv[4] << " successfully " << (d ? "decrypted" : "encrypted") << "." << endl;
break;
default:
printHelp();
return 2;
}
ifs.close();
ofs.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment