خوارزميات

السلام عاوز كود جاهز لي خوازميه blowfish...لانوا جربت كذا كود وغلط ...علمآ انا شغال بالNot Baines

يرجى الدخول لحسابك أو تسجيل حساب لتستطيع إضافة تعليق
حساب جديد دخول

التعليقات

هذا الكود المقدم من ويكبيديا

uint32_t P[18];
uint32_t S[4][256];

uint32_t f (uint32_t x) {
   uint32_t h = S[0][x >> 24] + S[1][x >> 16 & 0xff];
   return ( h ^ S[2][x >> 8 & 0xff] ) + S[3][x & 0xff];
}

void blowfish_encrypt(uint32_t *L, uint32_t *R) {
    for (short r = 0; r < 16; r++) {
		*L = *L ^ P[r];
		*R = f(*L) ^ *R;
		swap(L, R);
	}
	swap(L, R);
	*R = *R ^ P[16];
	*L = *L ^ P[17];
}

void blowfish_decrypt(uint32_t *L, uint32_t *R) {
	for (short r = 17; r > 1; r--) {
		*L = *L ^ P[r];
		*R = f(*L) ^ *R;
		swap(L, R);
	}
	swap(L, R);
	*R = *R ^ P[1];
	*L = *L ^ P[0];
}

  // ...
  // initializing the P-array and S-boxes with values derived from pi; omitted in the example (you can find them below)
  // ...
  
{
	/* initialize P box w/ key*/
	uint32_t k;
	for (short i = 0, p = 0; i < 18; i++) {
		k = 0x00;
		for (short j = 0; j < 4; j++) {
			k = (k << 8) | (uint8_t) key[p];
			p = (p + 1) % key_len;
		}
		P[i] ^= k;
	}
   
	/* blowfish key expansion (521 iterations) */
	uint32_t l = 0x00, r = 0x00;
	for (short i = 0; i < 18; i+=2) {
		blowfish_encrypt(&l, &r);
		P[i] = l; 
		P[i+1] = r;
	}
	for (short i = 0; i < 4; i++) {
		for (short j = 0; j < 256; j+=2) {
			blowfish_encrypt(&l, &r);
			S[i][j] = l;
			S[i][j+1] = r;
		}
	}
}