Thread: CBC Implementation issues...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    CBC Implementation issues...

    I've having a lot of trouble implementing this. When my algorithm was in ECB mode it worked fine but i cant get the CBC to go. Anyone see what my error is?
    Code:
    void encryptDEA(string &ptext, string key, string &ctext)
    {
    	//CBC Mode
    	string prevBlock = "";
    	appendString(ptext);
    	string block;
    	int r = ptext.size()/16;
    	//ENCRYPTION
    	for(int i=r; i>0; i--)
    	{
    		divyBlocks(ptext, block, r);
    		xor(block, prevBlock);
    		encrypt(block, key);
    		prevBlock = block;
    		ctext += block;
    	}
    }
    
    void decryptDEA(string ctext, string key, string &ptext)
    {
    	//CBC Mode
    	string block;
    	int j;
    	string prevBlock = "";
    	int r = ctext.size()/16;
    	//DECRYPTION
    	j = r;
    	for(int i=0; i<j; i++)
    	{
    		dDivyBlocks(ctext, block, r);
    		xor(block, prevBlock);
    		decrypt(block, key);
    		prevBlock = block;
    		ptext += block;
    	}
    }
    the divyBlocks() function divides the plain-text into 128-bit blocks, the dDivyBlocks() function does the same but starts with the last block instead of the first.


    Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Any Takers?

    Let's see, i'll explain my functions:

    divyBlocks() -> Divides up the text into 128-bit blocks and puts each block into the string (block) that gets passed to the function, dDivyBlocks() does the same thing except it starts with the LAST 128-bit block and works backwards.

    xor() - > pretty self explainitory

    and of course encrypt() and decrypt() do one round of encryption on one block segment.

    Help would be appreciated! If anyone knows what CBC is, and if you dont it's this.

    Basically, in ECB (electronic codebook mode) the encryption just encodes a block, and then encodes another, and so on and strings them together at the end. Not very secure, suppose two blocks were the same? Then the security of the cipher is compromised greatly.

    So there's also CBC mode, (Code Block Chaining mode) in which the first block is encrypted, then the encrypted block is xored with the next block to be encrypted then the result is encrypted, then that block is xored with the next and then encrypted, and so on and so forth.


    I think the problem lies in my logic on the decryption side of this. Any help? Please =)
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 08:05 PM