#define CYCLE_LEN 3

void crypting(char *s, char *password, int decrypt)
{
	int l = strlen(s), pos;
	int pwdlen = strlen(password);
	char *xpassword = malloc(pwdlen + CYCLE_LEN);
	int i, j;

	j = 1;
	/* xpassword is password with bytes mangled periodically */
	for (i = 0; i < pwdlen; i++) {
		xpassword[i] = password[i] + (j + i) * password[i];
		j++; if (j > CYCLE_LEN) j = 1;
	}
	/* fill the rest of xpassword with its start */
	for (i = 0; i < CYCLE_LEN; i++) {
		xpassword[l + i] = xpassword[i];
	}

	j = 2; i = 0;
	for (pos = 0; pos < l; pos++) {
		int mangle;

		mangle = xpassword[i] - xpassword[i + j] + (j * i);
		s[pos] = (decrypt ? -1 : 1) * mangle;

		i++; if (i >= pwdlen) i = 0;
		j++; if (j > CYCLE_LEN) j = 1;
	}
}
