Example #1
0
int validar_clave(char* clave){
	int det, det_1, det_2, inv_det;
	char matrix[10];

	if(strlen(clave) != 9)
		return 1;

	normalizar(clave, matrix);

	// calculamos el determinante
	det_1 = matrix[0]*matrix[4]*matrix[8] + matrix[1]*matrix[5]*matrix[6] + matrix[2]*matrix[3]*matrix[7];
	det_2 = matrix[2]*matrix[4]*matrix[6] + matrix[5]*matrix[7]*matrix[0] + matrix[8]*matrix[1]*matrix[3];

	det = det_1 - det_2;

	if(det == 0)
		return 1;

	if(det < 0)
		det = (det % 41)+41;
	else
		det = det % 41;

	inv_det = euclides(0, 1, 0, 41, 0, 1, det);
	if(inv_det == 0){
		return 1;
	}

	return 0;
}
Example #2
0
int euclides(int x, int y)
{
	int resto = x%y;

	if(resto == 0)
	{
		return y;
	}
	else
	{
		return euclides(y, resto);
	}

}
Example #3
0
int main(int argc, char const *argv[])
{
	
	int i, j, k;

	int num_instancia, f1, f2, resto;

	scanf("%d\n", &num_instancia);

	for (i = 0; i < num_instancia; i++)
	{
		scanf("%d %d\n", &f1, &f2);

		printf("%d\n", euclides(f1, f2));

	}

	return 0;
}
Example #4
0
int decrypt(char* clave, char* palabra, char* out){
	bool is_present = 0;//falso
	int pos = 0;

	// matrix 3x3
	int adj[9];
	int inv[9] = {0};
	int det, det_1, det_2, inv_det;
	int result2[3];
	int word_code[6];
	int i;
	int matrix[9] = {0};


	for (i = 0; i < 9; i++) {
		is_present = 0;
		pos  = 0;
		while(!is_present && pos < 41){
			if(alfabeto[pos] == clave[i]){
				matrix[i] = pos;
				is_present = 1;
			}
			pos += 1;
		}
	}

	// adj(matrix)
	adj[0] = (matrix[4]*matrix[8] - matrix[5]*matrix[7]) % 41; // 0
	adj[1] = (matrix[1]*matrix[8] - matrix[2]*matrix[7]) % 41; // 3
	adj[2] = (matrix[1]*matrix[5] - matrix[2]*matrix[4]) % 41; // 6

	adj[3] = (matrix[3]*matrix[8] - matrix[5]*matrix[6]) % 41; // 1
	adj[4] = (matrix[0]*matrix[8] - matrix[2]*matrix[6]) % 41; // 4
	adj[5] = (matrix[0]*matrix[5] - matrix[2]*matrix[3]) % 41; // 7

	adj[6] = (matrix[3]*matrix[7] - matrix[4]*matrix[6]) % 41; // 2
	adj[7] = (matrix[0]*matrix[7] - matrix[1]*matrix[6]) % 41; // 5
	adj[8] = (matrix[0]*matrix[4] - matrix[1]*matrix[3]) % 41; // 8

	// calculamos el determinante
	det_1 = matrix[0]*matrix[4]*matrix[8] + matrix[1]*matrix[5]*matrix[6] + matrix[2]*matrix[3]*matrix[7];
	det_2 = matrix[2]*matrix[4]*matrix[6] + matrix[5]*matrix[7]*matrix[0] + matrix[8]*matrix[1]*matrix[3];

	det = det_1 - det_2;

	if(det < 0)
		det = (det % 41)+41;
	else
		det = det % 41;

	inv_det = euclides(0, 1, 0, 41, 0, 1, det);
	if(inv_det == 0){
		return -1;
	}

	for (i = 0; i < 9; i++) {
		if(i%2 == 0)
			inv[i] = ( adj[i] * inv_det ) % 41;
		else{
			inv[i] = ( (-1) * adj[i] * inv_det ) % 41;
		}

	}

	int size = strlen(palabra);
	out[size] = 0;
	while(size > 2){
		for (i = 0; i < 3; i++) {
			is_present = 0;
			pos  = 0;
			while(!is_present && pos < 41){
				if(alfabeto[pos] == palabra[i]){
					word_code[i] = pos;
					is_present = 1;
				}
				pos += 1;
			}
		}

		_decrypt(inv, word_code, result2);

		for (i = 0; i < 3; i++)
			*(out++) =  alfabeto[result2[i]];

		palabra += 3;
		size -= 3;
	}

	if(size){
		word_code[0] = 0;
		word_code[1] = 0;
		word_code[2] = 0;
		for (i = 0; i < size; i++) {
			is_present = 0;
			pos  = 0;
			while(!is_present && pos < 41){
				if(alfabeto[pos] == palabra[i]){
					word_code[i] = pos;
					is_present = 1;
				}
				pos += 1;
			}
		}

		if(size == 2){
			result2[1] = (word_code[1] * matrix[0] - matrix[3] * word_code[0]) / (matrix[0] * matrix[4] - matrix[1] * matrix[3]);
			result2[0] =  (word_code[0] - matrix[1] * result2[1]) / matrix[0];
		}else{
			result2[0] = word_code[0] / matrix[0];
		}
		//_decrypt(inv, word_code, result2);
		for (i = 0; i < size; i++)
			*(out++) =  alfabeto[result2[i]];
	}

	return 0;
}