Ejemplo n.º 1
0
int main() {
  unsigned long L = 1, R = 2;
  BLOWFISH_CTX ctx;

  Blowfish_Init (&ctx, (unsigned char*)"TESTKEY", 7);
  Blowfish_Encrypt(&ctx, &L, &R);
  printf("%08lX %08lX\n", L, R);
  if (L == 0xDF333FD2L && R == 0x30A71BB4L)
    printf("Test encryption OK.\n");
  else
    printf("Test encryption failed.\n");
  Blowfish_Decrypt(&ctx, &L, &R);
  if (L == 1 && R == 2)
    printf("Test decryption OK.\n");
  else
    printf("Test decryption failed.\n");

  return 0;
}
Ejemplo n.º 2
0
static void BlowFishEncode(int len,BYTE *source,int len_key,BYTE* init)
{
  BLOWFISH_CTX ctx;
  unsigned long lvalue,rvalue;
  int i;

  Blowfish_Init(&ctx,init,len_key);

  for (i=0; i < (len /8); i++)
    {
      lvalue = *source << 24 | *(source+1) << 16 | +*(source+2) << 8 | *(source+3);
      rvalue = *(source+4) << 24 | *(source+5) << 16 | +*(source+6) << 8 | *(source+7);

      Blowfish_Encrypt(&ctx,&lvalue,&rvalue);
      WORD_WRITE(source,lvalue >> 16);
      WORD_WRITE(source,lvalue & 0x0000ffff);
      WORD_WRITE(source,rvalue >> 16);
      WORD_WRITE(source,rvalue & 0x0000ffff);
    }
}
Ejemplo n.º 3
0
/*--------------------------------------------------------------------------------*/
void ABlowfish::Encrypt(const uint8_t *src, uint8_t *dst, uint32_t& iv1, uint32_t& iv2)
{
	BLOWFISH_CTX *ctx = (BLOWFISH_CTX *)GetContext();
	uint32_t l, r;

	// copy bytes into 2 32-bit variables
	memcpy(&l, src, sizeof(l)); src += sizeof(l);
	memcpy(&r, src, sizeof(r));

	// for little endian machines, swap bytes to make all data big-endian
	if (!MachineIsBigEndian()) {
		l = SwapBytes(l);
		r = SwapBytes(r);
	}

	// XOR in initialisation vectors (for CBC mode)
	l ^= iv1;
	r ^= iv2;

	// encrypt data
	Blowfish_Encrypt(ctx, &l, &r);

	// return new initialisation vectors (for CBC mode)
	iv1 = l;
	iv2 = r;

	// for little endian machines, swap bytes back to make all data little-endian
	if (!MachineIsBigEndian()) {
		l = SwapBytes(l);
		r = SwapBytes(r);
	}

	// copy bytes to destination
	memcpy(dst, &l, sizeof(l)); dst += sizeof(l);
	memcpy(dst, &r, sizeof(r));

	l = r = 0;	// clear local variables to prevent security leaks
}
Ejemplo n.º 4
0
	static forceinline void Encrypt(_BLOWFISH_CTX *ctx,unsigned long *IV_L,unsigned long *IV_R,unsigned long *left,unsigned long *right)
	{
		(void) IV_L; (void) IV_R;
		Blowfish_Encrypt(ctx,left,right);
	};
Ejemplo n.º 5
0
static void Blowfish_Init(_BLOWFISH_CTX *ctx, const unsigned char *key, unsigned int keyLen)
{
	static const unsigned long dummy=0;
	unsigned int i;
	unsigned long *p,*s;

	ASSERT(keyLen>=4);
	ASSERT(keyLen<=56);//448 bit max!

	if (keyLen<4) { //SOS!
		key=(const unsigned char*)&dummy;
		keyLen=4;
	};
	if (keyLen>56) { //SOS!
		keyLen=56;
	};

	{
		p=ctx->P;
		const unsigned long *p0=ORIG_P;
		unsigned int j=0;
		if (ISLONGALIGN((size_t)key)&&ISLONGALIGN(keyLen)) {
			unsigned long *pulkey=(unsigned long*)key;
			unsigned long *pulmaxkey=(unsigned long*)(key+keyLen);
			for (i=0;i<(16+2);i++) {
				*p++ = *p0++ ^ SWAP(*pulkey++);
				if (pulkey>=pulmaxkey) pulkey=(unsigned long*)key;
			};
		}
		else {
			unsigned long data=0;
			for (i=0;i<(16+2);i++) {
				unsigned int k;
				for (k=0;k<4;k++) {
					SETBYTE(data,3-k,key[j]);if (++j >= keyLen) j = 0;
				};
				*p++ = *p0++ ^ data;
			};
		};
	};

	memcpy(ctx->S,ORIG_S,sizeof(ORIG_S));

	{
		unsigned long datal, datar;
		datal=0;datar=0;

		p=ctx->P;
		for (i=0;i<((16+2)/2);i++) {
			Blowfish_Encrypt(ctx, &datal, &datar);
			*p++ = datal;
			*p++ = datar;
		};

		s=ctx->S;
		for (i=0;i<((4*256)/2);i++) {
			Blowfish_Encrypt(ctx, &datal, &datar);
			*s++ = datal;
			*s++ = datar;
		};
	};
}
Ejemplo n.º 6
0
void CSocket::DoPrint(const wstring &wscTextIn)
{
	wstring wscText(wscTextIn);
	for(uint i = 0; (i < wscText.length()); i++)
	{
		if(wscText[i] == '\n')
		{
			wscText.replace(i, 1, L"\r\n");
			i++;
		}
	}

	int iSndBuf = 300000; // fix: set send-buffer to this size (bytes) 
    int size = sizeof(int); 
    setsockopt(this->s, SOL_SOCKET, SO_SNDBUF, (const char*)&iSndBuf, size);

	if(bEncrypted)
	{
		int iLen;
		char *data;
		const char* tempData;
		if(bUnicode)
		{
			uint iRem = (uint)(wscText.length() % 4);
			if(iRem) //Data to be encrypted is not a multiple of 8 bytes, add 0x00s to compensate
			{
				iRem = 4 - iRem;
				wscText.resize(wscText.length()+iRem, L'\x00');
			}
			tempData = (const char*)wscText.data();
			iLen = (uint)(wscText.length()*2);
			data = (char*)malloc(iLen);
			memcpy(data, tempData, iLen);
		}
		else
		{
			uint iRem = (uint)(wscText.length() % 8);
			if(iRem)
			{
				iRem = 8 - iRem;
				string scText = wstos(wscText);
				scText.resize(scText.length()+iRem,'\x00');
				tempData = scText.data();
				iLen = (int)scText.length();
				data = (char*)malloc(iLen);
				memcpy(data, tempData, iLen);
			}
			else
			{
				string scText = wstos(wscText);
				tempData = scText.data();
				iLen = (int)wscText.length();
				data = (char*)malloc(iLen);
				memcpy(data, tempData, iLen);
			}
		}

		SwapBytes(data, iLen);
		if(Blowfish_Encrypt(bfc, data, iLen))
		{
			SwapBytes(data, iLen);
			send(this->s, data, iLen, 0);
			free(data);
		}
	}
	else
	{
		if(bUnicode)
			send(this->s, (const char*)wscText.c_str(), (int)wscText.length()*2, 0);
		else
			send(this->s, wstos(wscText).c_str(), (int)wscText.length(), 0);
	}

}
Ejemplo n.º 7
0
int
encryption (char *key, unsigned char *plaintext_string, unsigned char *ciphertext_string, unsigned char *crypt64, int * ciphertext_len)
   {
	   BLOWFISH_CTX ctx;
	   int n;
	   int keylen = strlen(key);
	   int plaintext_len = strlen(plaintext_string);
	   unsigned long message_left;
	   unsigned long message_right;
	   int block_len;
           char * ciphertext_string_ori = ciphertext_string;

      	   *ciphertext_len = 0;

	   Blowfish_Init(&ctx, key, keylen);

	   while (plaintext_len)
	   {
		     message_left = message_right = 0UL;

		   /* crack the message string into a 64-bit block (ok, really two 32-bit blocks); pad with zeros if necessary */
		     for (block_len = 0; block_len < 4; block_len++)
		     {
			       message_left = message_left << 8;
			       if (plaintext_len)
			       {
				   message_left += *plaintext_string++;
				   plaintext_len--;
			       }
			       else message_left += 0;
		     }
		     for (block_len = 0; block_len < 4; block_len++)
		     {
			       message_right = message_right << 8;
			       if (plaintext_len)
			       {
				   message_right += *plaintext_string++;
				   plaintext_len--;
			       }
			       else message_right += 0;
		     }
          

		    /* encrypt and print the results */
		     Blowfish_Encrypt(&ctx, &message_left, &message_right);

		   /* save the results for decryption below */
		     *ciphertext_string++ = (unsigned char)(message_left >> 24);
		     *ciphertext_string++ = (unsigned char)(message_left >> 16);
		     *ciphertext_string++ = (unsigned char)(message_left >> 8);
		     *ciphertext_string++ = (unsigned char)message_left;
		     *ciphertext_string++ = (unsigned char)(message_right >> 24);
		     *ciphertext_string++ = (unsigned char)(message_right >> 16);
		     *ciphertext_string++ = (unsigned char)(message_right >> 8);
		     *ciphertext_string++ = (unsigned char)message_right;
		     *ciphertext_len += 8;		    
	    }

 	    encode_base64(*ciphertext_len, ciphertext_string_ori, crypt64 );

            return 0;

}