unvisibleman

С++ Simple crypt class

May 5th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. struct key{
  2.     int a, b, g;
  3. };
  4.  
  5. struct output{
  6.     QString numericInput;
  7.     QString gamma;
  8.     QString numericOutput;
  9.     QString Output;
  10. };
  11.  
  12. // синглтоновый объект, реализующий шифр гаммирования
  13.  
  14. class Crypter{
  15.     private:
  16.         static Crypter* _instance;
  17.         Crypter(){} // пустой конструктор
  18.         ~Crypter(){} // и деструктор
  19.  
  20.     public:
  21.  
  22.         static Crypter* getInstance(){
  23.             if(!_instance) // если объекта нет
  24.                 _instance = new Crypter(); // то создать (конструктор пуст)
  25.             return _instance; // иначе - использовать существующий
  26.         }
  27.  
  28.         //шифровка
  29.         output crypt(key current, QString inVector){
  30.             int *crypt; // зашифрованные данные
  31.             int *gamma; // вектор для шифра замены
  32.             output data;
  33.             int len = inVector.length(); // узнаем сколько памяти надо
  34.             crypt = new int [len]; // и выделяем её
  35.             gamma = new int [len];
  36.             gamma[0]=current.g;
  37.             for(int i=0; i<len; i++){
  38.                 crypt[i] = inVector[i].unicode();
  39.                 data.numericInput += " " + QString::number(crypt[i]);
  40.                 if(i!=0)
  41.                     gamma[i]=(current.a*gamma[i-1]+current.b)%(len-1);
  42.                 data.gamma += " " + QString::number(gamma[i]);
  43.                 crypt[i]=(crypt[i]+gamma[i]);
  44.                 data.numericOutput += " " + QString::number(crypt[i]);
  45.                 data.Output += QChar(crypt[i]);
  46.             }
  47.             return data;
  48.         }
  49.  
  50.         //дешифровка
  51.         output decrypt(key current, QString outVector){
  52.             int *crypt;
  53.             int *gamma;
  54.             output data;
  55.             int len = outVector.length();
  56.             crypt = new int [len];
  57.             gamma = new int [len];
  58.             gamma[0]=current.g;
  59.             for(int i=0; i<len; i++){
  60.                 crypt[i] = outVector[i].unicode();
  61.                 data.numericInput += " " + QString::number(crypt[i]);
  62.                 if(i!=0)
  63.                     gamma[i]=(current.a*gamma[i-1]+current.b)%(len-1);
  64.                 data.gamma += " " + QString::number(gamma[i]);
  65.                 crypt[i]=(crypt[i]-gamma[i]);
  66.                 data.numericOutput += " " + QString::number(crypt[i]);
  67.                 data.Output += QChar(crypt[i]);
  68.             }
  69.             return data;
  70.         }
  71. };
Advertisement
Add Comment
Please, Sign In to add comment