/* * mpepcrypt.cpp * * Copyright (C) 2007 Marc Espin Pedrol (Spin_555) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see */ #include #include #include using namespace std; int genera(string& s){ int password = 0; for(int i = 0; i < (int) s.size(); ++i)password += (int)s[i]; return password; } void inicialitza(vector < vector >& alfabet){ int queposo = 32; for (int i = 0; i < 95; ++i, ++queposo){ alfabet[i][0] = (char)queposo; alfabet[i][2] = 'a'; } } bool hemacabat(vector >& alfabet){ bool b = true; for(int i = 0; i < 95 and b; ++i){ if (alfabet[i][2] == 'a') b = false; } return b; } void alfabetencriptat(vector < vector >& alfabet, int password){ int gg = 95; for (int i = 0, aux = password; i < gg; ++i, aux += password){ aux %= gg; bool b = hemacabat(alfabet); while (alfabet[aux][2] == 'b' and not b){ ++aux; aux %= gg; } alfabet[i][1] = alfabet[aux][0]; alfabet[aux][2] = 'b'; } } void encripta(vector < vector >& alfabet, string t, string& u){ for (int i = 0; i < (int) t.size(); ++i){ u += alfabet[(int)t[i]-32][1]; } } void desencripta(vector < vector >& alfabet, string t, string& u){ for (int i = 0; i < (int) t.size(); ++i){ int j = 0; for (bool b = true; j < 95 && b; ++j){ if (alfabet[j][1] == t[i]) b = false; } u += alfabet[j-1][0]; } } char* cripto(const char* ss, const char* tt, bool b){ string s, t; for(int i=0;i < (int) strlen(ss);i++) s+=ss[i]; for(int i=0;i < (int) strlen(tt);i++) t+=tt[i]; int password = genera(s); vector< vector > alfabet(95, 3); inicialitza(alfabet); alfabetencriptat(alfabet, password); string u; for (int i = 0; i < (int)alfabet.size(); i++) { fprintf(stderr, "caracter:%c;", alfabet[i][1]); } fprintf(stderr, "\n"); if (b)encripta(alfabet, t, u); else desencripta(alfabet, t, u); u += '\0'; char uu[(int) u.size()]; for(int i=0;i < (int) u.size() ;i++) uu[i]= u[i]; char *ret = uu; return ret; }