Simple Shift errors...

This is a discussion on Simple Shift errors... within the C++ Programming forums, part of the General Programming Boards category; Hey everyone... I'm writing a very SImple and very BASIC encryption algorithm. It consists of 3 parts, dividing up the ...

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

    Simple Shift errors...

    Hey everyone... I'm writing a very SImple and very BASIC encryption algorithm.

    It consists of 3 parts, dividing up the plaintext, dividing up the key, and then xoring, substituting and permutating (if thats a word ha) these individual pieces.


    I havent begun the substitution yet but i think that should be easy, nor have i done the xoring but again that's simple as well.

    What i am having trouble with is the permutation function.

    It's EXTREMELY simple, it simply shifts the string 3 chars to the left on encryption and 3 chars to the right on decryption.

    Here's the functions, for some reason the first few characters are getting messed up and i dont know why:
    Code:
    void ePermutation(string &block)
    {
    	char temp = block.at(0);
    	char temp2;
    	for(int i=0; i<3; i++)
    	{
    		//shift everything one to the left
    		for(int j=15; j>0; j--)
    		{
    			temp2 = block.at(j);
    			block.at(j) = temp;
    			temp = temp2;
    		}
    	}
    }
    
    void dPermutation(string &block)
    {
    	char temp = block.at(15);
    	char temp2;
    	for(int i=0; i<3; i++)
    	{
    		for(int j=0; j<16; j++)
    		{
    			temp2 = block.at(j);
    			block.at(j) = temp;
    			temp = temp2;
    		}
    	}
    }
    Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    ePermutation
    changes chars block.at(15) .. block.at(1)

    dPermutation
    changes chars block.at(0) .. block.at(15)

    looks suspisios
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    ah thanks, ill try to fix that
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    ok well i fixed it so that in ePermutation the second for loop went like this:
    Code:
    for(int j=15; j>=0; j--)
    Still not working
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Nevermind... fixed it, i wasn't resetting the initial temp char each time the second loop looped in the first loop. I moved a bit of code and now it works fine. Thanks for your help!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    If you want to rotate a string there's a handy rotate function in the algorithm header:
    Code:
    #include <string>
    #include <algorithm>
    #include <iostream>
    
    ...
    
    std::string str("Hello World!");
    std::cout << "Before rotate : " << str << std::endl;
    
    std::rotate(str.begin(),str.begin()+3,str.end());
    std::cout << "Rotate 3 left : " << str << std::endl;
    
    std::rotate(str.begin(),str.end()-3,str.end());
    std::cout << "Rotate 3 right: " << str << std::endl;
    Should output:
    Code:
    Before rotate : Hello World!
    Rotate 3 left : lo World!Hel
    Rotate 3 right: Hello World!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ceasar Shift program
    By trevordunstan in forum C Programming
    Replies: 11
    Last Post: 09-11-2008, 10:40 PM
  2. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  3. Caeser Cipher
    By L_U_K_E in forum C++ Programming
    Replies: 35
    Last Post: 06-30-2006, 08:15 AM
  4. Simulating Shift Key
    By PrimeTime00 in forum Windows Programming
    Replies: 7
    Last Post: 10-15-2004, 06:53 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 10:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21