Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct key{
- int a, b, g;
- };
- struct output{
- QString numericInput;
- QString gamma;
- QString numericOutput;
- QString Output;
- };
- // синглтоновый объект, реализующий шифр гаммирования
- class Crypter{
- private:
- static Crypter* _instance;
- Crypter(){} // пустой конструктор
- ~Crypter(){} // и деструктор
- public:
- static Crypter* getInstance(){
- if(!_instance) // если объекта нет
- _instance = new Crypter(); // то создать (конструктор пуст)
- return _instance; // иначе - использовать существующий
- }
- //шифровка
- output crypt(key current, QString inVector){
- int *crypt; // зашифрованные данные
- int *gamma; // вектор для шифра замены
- output data;
- int len = inVector.length(); // узнаем сколько памяти надо
- crypt = new int [len]; // и выделяем её
- gamma = new int [len];
- gamma[0]=current.g;
- for(int i=0; i<len; i++){
- crypt[i] = inVector[i].unicode();
- data.numericInput += " " + QString::number(crypt[i]);
- if(i!=0)
- gamma[i]=(current.a*gamma[i-1]+current.b)%(len-1);
- data.gamma += " " + QString::number(gamma[i]);
- crypt[i]=(crypt[i]+gamma[i]);
- data.numericOutput += " " + QString::number(crypt[i]);
- data.Output += QChar(crypt[i]);
- }
- return data;
- }
- //дешифровка
- output decrypt(key current, QString outVector){
- int *crypt;
- int *gamma;
- output data;
- int len = outVector.length();
- crypt = new int [len];
- gamma = new int [len];
- gamma[0]=current.g;
- for(int i=0; i<len; i++){
- crypt[i] = outVector[i].unicode();
- data.numericInput += " " + QString::number(crypt[i]);
- if(i!=0)
- gamma[i]=(current.a*gamma[i-1]+current.b)%(len-1);
- data.gamma += " " + QString::number(gamma[i]);
- crypt[i]=(crypt[i]-gamma[i]);
- data.numericOutput += " " + QString::number(crypt[i]);
- data.Output += QChar(crypt[i]);
- }
- return data;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment