Thread: xor Encryption

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    xor Encryption

    I am trying to work out the encryption function to the following decryption function.

    Code:
    char* buf;
    	unsigned int nKey = buf[3] << 24;
    	nKey |= buf[2] << 16;
    	nKey |= buf[1] << 8;
    	nKey |= buf[0];
    	unsigned int nIndex = 4;
    
    	for(unsigned int nBitNr = 0; nBitNr < 32; ++nBitNr, nIndex++)
    	{
    		int nDecryptBit = (nKey & (1 << nBitNr)) >> nBitNr;
    		int chEncryptedByte = buf[nIndex];
    		int chDecryptedByte = chEncryptedByte;
    
    		chDecryptedByte = chDecryptedByte ^ nDecryptBit;
    		chDecryptedByte = chDecryptedByte & 1;
    		chDecryptedByte = chDecryptedByte ^ chEncryptedByte;
    		buf[nIndex] = chDecryptedByte;
    		if(nBitNr == 31)
    			break;
    	}
    	printf("File Decrypted is %s\n",buf);
    I see that the decrpt key is the first for bytes of Buf and that only 32 bytes are decrypted.

    From searching these forums I now know that
    a = b ^ c goes to c = b ^ a

    would a = b & 1 go to a = b & 0?

    also what is this doing?
    Code:
    int nDecryptBit = (nKey & (1 << nBitNr)) >> nBitNr;
    Last edited by cruxis; 11-05-2004 at 04:42 PM.

Popular pages Recent additions subscribe to a feed