Ejemplo n.º 1
0
int main(int argc, char* argv[])
{ 

//Rejects less than or more than 2 arguments       
   
    while(argc != 2) 
    {
        printf("Error!\n");
        return 1;
    }
    
        string k = argv[1];
 
//Check the Key: anything but letters is rejected    

for(int m = 0, n = strlen(k); m < n; m++)
{
    if(ispunct(k[m]) || isdigit(k[m]))
    {
        printf("Error!\n");
        return 1;
    }       
}

        string text = GetString();
        
//calling of the ciphering function   
        
        string citext = ciph(text,k);
        printf("%s", citext);
                    
    printf("\n");
    return 0;
}
Ejemplo n.º 2
0
int main()
{
	srand (time(NULL));

	//------------------------------------------------
	vector<int> my_s, example_s, my_p, andr_s;
	//------------------------------------------------
	int my_sub[16]			= {2, 11, 13, 0, 9, 7, 4, 14, 1, 12, 8, 15, 6, 10, 3, 5};
	int example_sub[16]		= {0, 13, 11, 8, 3, 6, 4, 1, 15, 2, 5, 14, 10, 12, 9, 7};
	int andr_sub[16]		= {0, 12, 15, 8, 2, 3, 5, 13, 6, 10, 7, 4, 14, 1, 11, 9};
	//------------------------------------------------
	my_s = vector<int>(my_sub, my_sub+16);
	example_s = vector<int>(example_sub, example_sub+16);
	andr_s = vector<int>(andr_sub, andr_sub+16);

	my_p = vector<int>(32); 
	//for (int i = 0; i < 32; i++) my_p[i] = (i*5)%32;
	for (int i = 0; i < 32; i++) my_p[i] = (i*5 + 11)%32;
	//for (int i = 0; i < 32; i++) my_p[i] = (i*7)%32; //Андрея
	//------------------------------------------------
	cout << "Working.." << endl;

	Cipher ciph(my_s,my_p);
	
	unsigned char str[] = "Hello world!";
	unsigned char key[] = "\x10\x03\xAA\x45";

	cout << ciph.DecryptBlock( ciph.CryptBlock(str, key), key) << endl;
	//cout << ciph.CryptBlock(str, key) << endl;
	
	cout << ciph.sub->PrintDiffTable() << endl;
	cout << ciph.sub->PrintDweightTable() << endl;

	cout << ciph.PrintSubstitution() << endl;
	cout << ciph.PrintPermutation() << endl;


	ciph.DiffCryptoanalyse(key);
	/*
	cout << p.PrintPermutation();
	cout << endl;
	/**/

	_getch();
	return 0;
}
Ejemplo n.º 3
0
int main()
{
	srand (time(NULL));

	//------------------------------------------------
	vector<int> my_s, my_p;
	//------------------------------------------------
	int my_sub[16]			= {2, 11, 13, 0, 9, 7, 4, 14, 1, 12, 8, 15, 6, 10, 3, 5};
	//------------------------------------------------
	my_s = vector<int>(my_sub, my_sub+16);

	my_p = vector<int>(32); 
	//for (int i = 0; i < 32; i++) my_p[i] = (i*5)%32;
	for (int i = 0; i < 32; i++) my_p[i] = (i*5 + 11)%32;
	//------------------------------------------------
	cout << "Working.." << endl;

	Cipher ciph(my_s,my_p);
	
	unsigned char x1[] = "Text", x2[4];
	unsigned char y1[4],y2[4];
	unsigned char key[] = "keykeykeykeykey";

	//"находим" открытый текст, ¤вл¤ющийс¤ результатом первого цикла шифровани¤
	memcpy(x2, x1, 4);
	ciph.CryptCycleBlock(x2, key);
	
	//зашифруем тексты
	memcpy(y1, x1, 4);
	memcpy(y2, x2, 4);
	for(int i = 0; i < CYCLES; i++)
	{
		ciph.CryptCycleBlock(y1, key);
		ciph.CryptCycleBlock(y2, key);
	}
	/*
	//если после циклов ксорить с ключом метод отсева не работает
	ciph.CryptBlock(y1, key);
	ciph.CryptBlock(y2, key);
	/**/
	/*
	int k = 0;
	unsigned char temp[4];
	memcpy(temp, x1, 4);
	ciph.CryptCycleBlock(temp, key);
	srand(time(NULL));
	while(1)
	{
		cout << k << endl;
		for (int i = 0; i < 4; i++) x2[i] = (unsigned char) rand();
		memcpy(y2, x2, 4);
		for(int i = 0; i < CYCLES; i++)
		{
			ciph.CryptCycleBlock(y2, key);
		}
		//провер¤ем критерий отбраковки
		if(!ciph.CheckCriterion(x1, x2, y1, y2))
		{
			if ((temp[0] == x2[0])&&(temp[1] == x2[1])&&(temp[2] == x2[2])&&(temp[3] == x2[3]))
			{
				cout << "Pair finded" << endl;
				break;
			}
		}
		k++;
	}
	/**/
	//провер¤ем критерий отбраковки
	if(ciph.CheckCriterion(x1, x2, y1, y2))
	{
		cout << "Pair sieved" << endl;
		_getch();
		return 0;
	}

	//вскрываем ключ
	unsigned char findkey[] = "\x00\x00\x00\x00\x00";
	ciph.DecryptCycleBlock(x2, findkey);

	for(int i = 0; i < 4; i++)
	{
		findkey[i] = x1[i]^x2[i];
	}

	cout << "Key finded: " << findkey << endl;

	_getch();
	return 0;
}
Ejemplo n.º 4
0
SubstitutionAlphabet *Vigenere::createSubstitution(const Alphabet &alph,
                                                   const QString &key) {
  Alphabet ciph(key);
  return new SubstitutionAlphabet(alph, ciph, true);
}
Ejemplo n.º 5
0
int main()
{
	srand (time(NULL));

	//------------------------------------------------
	vector<int>  my_s, my_p;
	//------------------------------------------------
	int my_sub[16]			= {2, 11, 13, 0, 9, 7, 4, 14, 1, 12, 8, 15, 6, 10, 3, 5};
	//------------------------------------------------
	my_s = vector<int>(my_sub, my_sub+16);
	my_p = vector<int>(32); 
	for (int i = 0; i < 32; i++) my_p[i] = (i*5 + 11)%32;
	//------------------------------------------------
	Cipher ciph(my_s,my_p);

	//for (int i = 0; i < 16; i++) cout << ciph.sub->subs_inv[i] << ", "; cout << endl;
	//cout << ciph.PrintPermutation();
	cout << "Working.." << endl;

	double dKK[32];
	double dK[32];
	double dKs[ROUND_PER_KEY][32];

	double dX[32];
	double dY[32];

	double dHx, dHmax;
	int j = 0;
	probability P[32];
	probability Q[32];

	unsigned char ucX[5] = "\x22\x6f\x3e\x65";//"0000";
	unsigned char ucY[5];
	unsigned char ucK[5] = "\x73\x56\xa3\x64";


	for (int i = 0; i < 32; i++)
	{
		P[i].p00 = 0;
		P[i].p01 = 0;
		P[i].p10 = 0;
		P[i].p11 = 0;
	}

	for (int j = 0; j < ROUND_PER_KEY; j++)
		for (int i = 0; i < 32; i++)
		{
			switch (rand()%5)
			{
			case 0: dKs[j][i] = 0.0;
				break;
			case 1: dKs[j][i] = 1.0;
				break;
			default: dKs[j][i] = 0.5;
				break;
			}
		}
	/**/
	for (int i = 0; i < NUMBER_OF_KEY; i++)
	{
		for (int i = 0; i < 4; i++)
		{
			ucK[i] = rand()%0x100;
			//ucX[i] = rand()%0x100;
		}

		for(int i = 0; i < 4; i++)
			ucY[i] = ucX[i];
	
		ciph.CryptBlock(ucY,ucK);

		//uctod(ucK, dK);
		uctod(ucK, dKK);
		uctod(ucX, dX);
		uctod(ucY, dY);
		/*
		cout << "-----------------K orig--------------" << endl;
		for (int i = 0; i < 32; i++)
		{	
			cout << dKK[i] << "\t";
			if(!((i+1)%8)) cout << endl;
		}
		cout << endl << endl;

		/**/
		//---------------------------------------------------------------------------------------

		for (int t = 0; t < ROUND_PER_KEY; t++)
		{
		
			for (int i = 0; i < 32; i++)
			{
				dK[i] = dKs[t][i];
				/*
				switch (rand()%50)
				{
				case 0: dK[i] = 0.0;
					break;
				case 1: dK[i] = 1.0;
					break;
				default: dK[i] = 0.5;
					break;
				}
				/**/
			}
			/*
			cout << "-----------------K-------------------" << endl;
			for (int i = 0; i < 32; i++)
			{	
				cout << dK[i] << "\t";
				if(!((i+1)%8)) cout << endl;
			}	
			cout << endl << endl;
			/**/
			//---------------------------------------------------------------------------------------

			dHx = alg1(dK, dX, dY, &ciph);

			for (int i = 0; i < 32; i++)
			{
				if((dK[i] == 0.0)&&(dKK[i] == 0.0)) P[i].p00++;
				if((dK[i] == 1.0)&&(dKK[i] == 0.0)) P[i].p10++;
				if((dK[i] == 0.0)&&(dKK[i] == 1.0)) P[i].p01++;
				if((dK[i] == 1.0)&&(dKK[i] == 1.0)) P[i].p11++;
				/*if(dK[i] != 0.5)
				{
					if (dK[i] == dKK[i]) P[i].p00++;
					if (dK[i] != dKK[i]) P[i].p10++;
				}*/

			}
		}
		/**/
		//---------------------------------------------------------------------------------------
	}
	
	cout << "\tN00\tN10\tN01\tN11" << endl ;
	cout << fixed ;
	cout << setprecision(2);
	for (int i = 0; i < 32; i++)
	{
		cout << i << "\t";
		cout << P[i].p00 << "\t";
		cout << P[i].p10 << "\t";
		cout << P[i].p01 << "\t";
		cout << P[i].p11 << endl;
	}

	double temp;
	for (int i = 0; i < 32; i++)
	{
		temp = P[i].p00 + P[i].p01;
		Q[i].p00 = P[i].p00/temp;
		Q[i].p10 = P[i].p01/temp;

		temp = P[i].p10 + P[i].p11;
		Q[i].p01 = P[i].p10/temp;
		Q[i].p11 = P[i].p11/temp;

		temp = P[i].p00 + P[i].p10;
		P[i].p00 = P[i].p00/temp;
		P[i].p10 = P[i].p10/temp;

		temp = P[i].p01 + P[i].p11;
		P[i].p01 = P[i].p01/temp;
		P[i].p11 = P[i].p11/temp;
	}
	/**/
	cout << endl << endl;
	cout << "\tp00\tp10\tp01\tp11" << endl ;
	for (int i = 0; i < 32; i++)
	{
		cout << i << "\t";
		cout << P[i].p00 << "\t";
		cout << P[i].p10 << "\t";
		cout << P[i].p01 << "\t";
		cout << P[i].p11 << endl;
	}
	cout << endl << endl;
	cout << "\tq00\tq10\tq01\tq11" << endl ;
	for (int i = 0; i < 32; i++)
	{
		cout << i << "\t";
		cout << Q[i].p00 << "\t";
		cout << Q[i].p10 << "\t";
		cout << Q[i].p01 << "\t";
		cout << Q[i].p11 << endl;
	}
	cout << endl << endl;
	cout.unsetf ( ios_base::fixed );  

	/*
	cout << "-----------------K-------------------" << endl;
	for (int i = 0; i < 32; i++)
	{	
		cout << dK[i] << "\t";
		if(!((i+1)%8)) cout << endl;
	}	
	cout << endl << endl;
	/**/
	//cout << "H* = " << dHx << endl;
	/*
	double s[] = {1, 1, 1, 1};
	SubInv(s);
	cout << s[0] << s[1] << s[2] << s[3] << endl << endl;
	
	for (int i = 0; i < 16; i++)
	{
		int temp = ciph.sub->subs_inv[i];
		cout << temp/2/2/2%2 << temp/2/2%2 << temp/2%2 << temp%2 << endl;
	}
	/**/

	_getch();
	return 0;
}