[ create a new paste ] login | about

Link: http://codepad.org/5ynmhSkf    [ raw code | output | fork ]

k06a - C++, pasted on Nov 25:
// Задание 2.1
#include <stdio.h>

int CycleShift(int a, int b)
{
 return (((((1 << b) - 1) << (32 - b)) & (unsigned int)a) >> (32 - b)) | (a << b);
 // Не пашет, если заменить на:
 //return (a << b) | (a >> (32-b));
 // (разница при большОм пар-ре b)
}

void generateMas(int* mas, int* Dst)
{
 mas[0] = -1209970333;
 for(int i=1; i < 44; i++)
   mas[i] = mas[i-1] - 1640531527;

 int ms = 0, ds = 0, k = 0;
 for (int i=0, j=1; j <= 132; j++)
 {
   ms = mas[i] = CycleShift(ds + ms + mas[i], 3);
   ds = Dst[k] = CycleShift(ds + ms + Dst[k], ds + ms);

   i = (i + 1) % 44;
   k = (k + 1) % 8;
 }
} // void generateMas(...)

void EditPass(int* pInputPass, int* mas)
{
 int a = pInputPass[0];
 int b = pInputPass[1] + mas[0];
 int c = pInputPass[2];
 int d = pInputPass[3] + mas[1];

 for (int i=1; i<=20; i++)
 {
   int f = CycleShift((2*b+1)*b, 5);
   int h = CycleShift((2*d+1)*d, 5);
   a = mas[2*i]+CycleShift(f^a, h);
   c = mas[2*i+1]+CycleShift(h^c, f);

   // abcd => bcda
   int z=a; a=b; b=c; c=d; d=z;
 }

 pInputPass[0] = a + mas[42];
 pInputPass[1] = b;
 pInputPass[2] = c + mas[43];
 pInputPass[3] = d;
} // void EditPass(...)

void undoEditPass(int* pInputPass, int* mas)
{
 int a = pInputPass[0] - mas[42];
 int b = pInputPass[1];
 int c = pInputPass[2] - mas[43];
 int d = pInputPass[3];

 for (int i=20; i>=1; i--)
 {
   // abcd => dabc
   int z=d; d=c; c=b; b=a; a=z;

   int f = CycleShift((2*b+1)*b, 5);
   int h = CycleShift((2*d+1)*d, 5);
   a = CycleShift(a-mas[2*i], 32-h)^f;
   c = CycleShift(c-mas[2*i+1], 32-f)^h;
 }

 pInputPass[0] = a;
 pInputPass[1] = b - mas[0];
 pInputPass[2] = c;
 pInputPass[3] = d - mas[1];
} // void undoEditPass(...)

int main()
{
 int *Dst = new int [8];
 // 871754d2 1a74d654 37c21ff4 e12f336d
 // 26acb4c9 0e4c708d 9f79d72e 2161033d
 Dst[0] = 0x871754d2;
 Dst[1] = 0x1a74d654;
 Dst[2] = 0x37c21ff4;
 Dst[3] = 0xe12f336d;
 Dst[4] = 0x26acb4c9;
 Dst[5] = 0x0e4c708d;
 Dst[6] = 0x9f79d72e;
 Dst[7] = 0x2161033d;


 int *mas = new int [44];
 generateMas(mas, Dst);

 int *pass = new int [5];
 // 492dad79 a79ec554 2c91ef4b 87fc7191 cc 00
 pass[0] = 0x492dad79;
 pass[1] = 0xa79ec554;
 pass[2] = 0x2c91ef4b;
 pass[3] = 0x87fc7191;
 pass[4] = 0;

 printf("0x%.8x 0x%.8x 0x%.8x 0x%.8x - %s\n",
   pass[0], pass[1],
   pass[2], pass[3], (char*)pass);

 //EditPass(pass, mas);

 //printf("0x%.8x 0x%.8x 0x%.8x 0x%.8x - %s\n",
 //  pass[0], pass[1],
 //  pass[2], pass[3], (char*)pass );

 undoEditPass(pass, mas);

 printf("0x%.8x 0x%.8x 0x%.8x 0x%.8x - %s\n",
   pass[0], pass[1],
   pass[2], pass[3], (char*)pass );

 // После выполнения видно,
 // что найдена обратная функция
 // undoEditPass(...) к EditPass(...)
}


Output:
1
2
0x492dad79 0xa79ec554 0x2c91ef4b 0x87fc7191 - y�-IT�K�,�q�
0xca9866e7 0x51bfbf7a 0x1a958f43 0xc37eb572 - �f��z��QC��r�~�


Create a new paste based on this one


Comments: