Thread: Substitution problem...

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

    Substitution problem...

    Okay, so i'm trying to wrap up this REAL BASIC ENCRYPTION ALGORITHM i've been writing and i've come to the last stumbling block, and this one really has me... I wrote two functions to substitute a random character in for the one its given. It's accomplished by taking the input character, and replacing it with a random one using a 256 integer array. For some reason it chooses to only correctly substitute and re-substitute certain characters correctly. Here's the code:
    Code:
    void eSubstitution(string &text)
    {
    	int subTable[256] = {62, 251, 202, 73, 80, 6, 60, 109, 236, 112, 55, 196, 191, 246, 155, 175, 122, 11
    , 253, 206, 124, 208, 5, 8, 197, 39, 99, 199, 232, 182, 165, 188, 224, 167, 140, 76, 68, 152, 100, 31, 223, 185, 
    4, 213, 229, 201, 16, 24, 105, 226, 57, 164, 33, 71, 51, 1, 128, 89, 235, 54, 187, 49, 176, 166, 30, 28, 183, 26, 
    120, 193, 244, 215, 90, 40, 217, 216, 129, 177, 153, 65, 195, 163, 118, 91, 138, 139, 242, 150, 21, 103, 83, 170,
     210, 81, 15, 207, 45, 132, 9, 43, 29, 46, 108, 146, 86, 245, 254, 3, 36, 173, 130, 106, 144, 66, 87, 230, 70, 151, 
    111, 156, 190, 243, 158, 148, 171, 143, 137, 178, 48, 84, 162, 157, 110, 200, 233, 198, 161, 225, 227, 74, 98, 248, 
    186, 159, 82, 50, 127, 2, 172, 123, 52, 25, 160, 219, 131, 77, 115, 141, 63, 79, 69, 220, 37, 102, 101, 247, 239, 42, 
    184, 116, 238, 78, 142, 189, 212, 32, 85, 104, 134, 114, 180, 174, 240, 211, 194, 23, 205, 125, 12, 17, 14, 0, 94, 
    107, 20, 41, 218, 209, 179, 97, 47, 67, 75, 59, 22, 35, 255, 13, 192, 169, 237, 119, 214, 117, 250, 38, 64, 228, 44, 
    168, 147, 113, 35, 33, 58, 121, 252, 149, 95, 10, 222, 19, 27, 154, 34, 145, 56, 136, 204, 53, 234, 231, 61, 93, 72, 
    181, 126, 92, 221, 96, 203, 241, 88, 249, 7, 18};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		text.at(i) = ((char)subTable[((int)text.at(i))]);
    	}
    }
    
    void dSubstitution(string &text)
    {
    	int subTable[256] = {62, 251, 202, 73, 80, 6, 60, 109, 236, 112, 55, 196, 191, 246, 155, 
    175, 122, 11, 253, 206, 124, 208, 5, 8, 197, 39, 99, 199, 232, 182, 165, 188, 224, 167, 140, 76, 68,
     152, 100, 31, 223, 185, 4, 213, 229, 201, 16, 24, 105, 226, 57, 164, 33, 71, 51, 1, 128, 89, 235, 54, 
    187, 49, 176, 166, 30, 28, 183, 26, 120, 193, 244, 215, 90, 40, 217, 216, 129, 177, 153, 65, 195, 163, 
    118, 91, 138, 139, 242, 150, 21, 103, 83, 170, 210, 81, 15, 207, 45, 132, 9, 43, 29, 46, 108, 146, 86,
     245, 254, 3, 36, 173, 130, 106, 144, 66, 87, 230, 70, 151, 111, 156, 190, 243, 158, 148, 171, 143, 137,
     178, 48, 84, 162, 157, 110, 200, 233, 198, 161, 225, 227, 74, 98, 248, 186, 159, 82, 50, 127, 2, 172, 
    123, 52, 25, 160, 219, 131, 77, 115, 141, 63, 79, 69, 220, 37, 102, 101, 247, 239, 42, 184, 116, 238, 
    78, 142, 189, 212, 32, 85, 104, 134, 114, 180, 174, 240, 211, 194, 23, 205, 125, 12, 17, 14, 0, 94, 107, 
    20, 41, 218, 209, 179, 97, 47, 67, 75, 59, 22, 35, 255, 13, 192, 169, 237, 119, 214, 117, 250, 38, 64, 228,
     44, 168, 147, 113, 35, 33, 58, 121, 252, 149, 95, 10, 222, 19, 27, 154, 34, 145, 56, 136, 204, 53, 234, 
    231, 61, 93, 72, 181, 126, 92, 221, 96, 203, 241, 88, 249, 7, 18};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		for(int j=0; j<256; j++)
    		{
    			if(((int)text.at(i)) == subTable[j])
    			{
    				text.at(i) = ((char)j);
    				break;
    			}
    		}
    	}
    }
    Thanks for any help you can give!
    Last edited by Junior89; 06-27-2007 at 12:32 AM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe wrap the initialiser for your arrays onto multiple lines, so we don't have to keep sideways scrolling to read anything...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Well they're basically just the numbers 0-255 in random order but okay...
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, given the limits of your i for loop, which stops at 4, I'm not surprised at what you are describing. Wouldn't you want to loop over the entire input string? Wouldn't you use text.length() instead of a hard-coded value of 4? I'd also maybe make your subTable arrays static/const.

    Give an example of the input/output you are receiving.
    "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

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Since subTable is the same is both functions, you should probably declare it in the calling function and pass it to those functions, or run the risk of having different arrays -- not to mention how unreadable that is.

    Actually, you might want a reverse lookup table for dSubstitution. Then you wouldn't have to loop through every index to find a number. What I mean is this. If your first array was
    Code:
    char lookup[] = {0, 2, 3, 1};
    char c = lookup[(int)(c - '0')];
    (to encode '0'-'4') then your second would be
    Code:
    char rlookup[] = {0, 3, 1, 2};
    char c = rlookup[(int)c] + '0';
    Basically, for rlookup[0], I put the index in lookup[] in which 0 occurs: 0. Then rlookup[1] = index in lookup[] of wherever 1 is: 3. (lookup[3] = 1. rlookup[1] = 3.) You could generate this with a program -- please don't do it by hand. In fact, your function could generate it from your first lookup table automatically.

    Also, these expressions are overly complex. Too many parentheses can be a bad thing.
    Code:
    text.at(i) = ((char)subTable[((int)text.at(i))]);
    if(((int)text.at(i)) == subTable[j])
    text.at(i) = ((char)j);
    How's this instead?
    Code:
    text.at(i) = (char)subTable[(int)text.at(i)];
    if((int)text.at(i) == subTable[j])
    text.at(i) = (char)j;
    You could even add spaces if you wanted to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    The hard-coded 4, is perfect for a string with 4 chars in it The substitution works on a 32-bit chunk so.... that's right.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    But it's not that much more difficult to use
    Code:
    for(string::size_type x = 0; x < text.length(); x ++)
    Another thought of mine is that you could generate those random numbers at run-time. If you used the same random number generator you'd be all right. For example, you could use Prelude's Mersenne Twister. Just a thought.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Okay well i did what you first suggested and created a reverse lookup table. It works on a,b,c, but not the letter d. Those were the only ones i've tested as of yet but i do not understand it. This function is not that difficult nor is the concept yet why is the reality of it and the actual code not working right?

    Code:
    void eSubstitution(string &text)
    {
    	int subTable[256] = {27, 144, 241, 223, 244, 24, 1, 60, 149, 154, 50, 174, 70, 132, 
    167, 86, 38, 188, 3, 233, 128, 130, 39, 118, 175, 22, 46, 84, 191, 42, 19, 113, 153, 225, 227, 
    110, 114, 254, 25, 230, 181, 185, 207, 2, 226, 243, 150, 236, 187, 14, 77, 116, 249, 137, 182, 
    133, 125, 17, 152, 197, 143, 105, 208, 57, 168, 119, 36, 224, 120, 189, 146, 90, 166, 101, 210
    , 201, 11, 82, 196, 23, 56, 43, 44, 159, 12, 157, 123, 5, 156, 96, 97, 160, 242, 9, 171, 121, 220
    , 95, 85, 41, 212, 237, 69, 139, 20, 228, 194, 59, 253, 195, 203, 161, 7, 200, 192, 169, 115, 21
    , 204, 91, 30, 75, 164, 252, 172, 117, 88, 13, 89, 247, 32, 108, 206, 178, 65, 81, 198, 145, 147
    , 170, 190, 209, 54, 67, 0, 179, 79, 6, 26, 4, 64, 124, 98, 62, 29, 193, 127, 104, 45, 140, 74, 
    218, 93, 134, 176, 232, 28, 202, 221, 234, 49, 35, 235, 155, 68, 217, 102, 33, 142, 251, 58, 
    231, 131, 135, 63, 163, 205, 87, 112, 165, 222, 245, 40, 72, 141, 246, 186, 92, 238, 255, 173, 
    37, 111, 240, 15, 76, 126, 248, 199, 122, 148, 213, 16, 48, 34, 78, 103, 47, 158, 219, 52, 73, 
    18, 106, 94, 183, 99, 53, 214, 83, 177, 107, 100, 211, 61, 66, 51, 138, 239, 31, 80, 8, 250, 71, 
    55, 109, 184, 229, 215, 10, 162, 151, 216, 180, 129, 136};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		text.at(i) = ((char)subTable[((int)text.at(i))]);
    	}
    }
    
    void dSubstitution(string &text)
    {
    	int subTable[256] = {144, 6, 43, 18, 149, 87, 147, 112, 241, 93, 249, 76, 84, 127, 
    49, 204, 212, 57, 222, 30, 104, 117, 25, 79, 5, 38, 148, 0, 166, 154, 120, 239, 130, 177, 214,
     171, 66, 201, 16, 22, 192, 99, 29, 81, 82, 158, 26, 217, 213, 170, 10, 236, 220, 227, 142, 244
    , 80, 63, 180, 107, 7, 234, 153, 184, 150, 134, 235, 143, 174, 102, 12, 243, 193, 221, 160, 121
    , 205, 50, 215, 146, 240, 135, 77, 229, 27, 98, 15, 187, 126, 128, 71, 119, 197, 162, 224, 97, 
    89, 90, 152, 226, 232, 73, 176, 216, 157, 61, 223, 231, 131, 245, 35, 202, 188, 31, 36, 116, 51
    , 125, 23, 65, 68, 95, 209, 86, 151, 56, 206, 156, 20, 254, 21, 182, 13, 55, 163, 183, 255, 53, 
    237, 103, 159, 194, 178, 60, 1, 137, 70, 138, 210, 8, 46, 251, 58, 32, 9, 173, 88, 85, 218, 83, 
    91, 111, 250, 185, 122, 189, 72, 14, 64, 115, 139, 94, 124, 200, 11, 24, 164, 230, 133, 145, 
    253, 40, 54, 225, 246, 41, 196, 48, 17, 69, 140, 28, 114, 155, 106, 109, 78, 59, 136, 208, 113, 
    75, 167, 110, 118, 186, 132, 42, 62, 141, 74, 233, 100, 211, 228, 248, 252, 175, 161, 219, 96, 
    168, 190, 3, 67, 33, 44, 34, 105, 247, 39, 181, 165, 19, 169, 172, 47, 101, 198, 238, 203, 2, 92
    , 45, 4, 191, 195, 129, 207, 52, 242, 179, 123, 108, 37, 199};
    	//substitute
    	for(int i=0; i<4; i++)
    	{
    		text.at(i) = ((char)subTable[((int)text.at(i))]);
    	}
    }
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You must have gotten the numbers wrong. It's the only explanation. How did you generate that table? (I'm assuming you automatically generated it.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I dont think i generated the reverse lookup correctly, it seems like some of them are 1 off what they should be perhaps, i dont know. Can anyone find anything?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  11. #11
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Yeah... i didn't save the function but i suppose i should write it again.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    i think i know what i did hold on lemme give it another shot
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I generated the table myself. It's the same as yours, I think.
    Code:
    #include <stdio.h>
    
    int lookup[] = {27, 144, 241, 223, 244, 24, 1, 60, 149, 154, 50, 174, 70, 132, 
    167, 86, 38, 188, 3, 233, 128, 130, 39, 118, 175, 22, 46, 84, 191, 42, 19, 113, 153, 225, 227, 
    110, 114, 254, 25, 230, 181, 185, 207, 2, 226, 243, 150, 236, 187, 14, 77, 116, 249, 137, 182, 
    133, 125, 17, 152, 197, 143, 105, 208, 57, 168, 119, 36, 224, 120, 189, 146, 90, 166, 101, 210
    , 201, 11, 82, 196, 23, 56, 43, 44, 159, 12, 157, 123, 5, 156, 96, 97, 160, 242, 9, 171, 121, 220
    , 95, 85, 41, 212, 237, 69, 139, 20, 228, 194, 59, 253, 195, 203, 161, 7, 200, 192, 169, 115, 21
    , 204, 91, 30, 75, 164, 252, 172, 117, 88, 13, 89, 247, 32, 108, 206, 178, 65, 81, 198, 145, 147
    , 170, 190, 209, 54, 67, 0, 179, 79, 6, 26, 4, 64, 124, 98, 62, 29, 193, 127, 104, 45, 140, 74, 
    218, 93, 134, 176, 232, 28, 202, 221, 234, 49, 35, 235, 155, 68, 217, 102, 33, 142, 251, 58, 
    231, 131, 135, 63, 163, 205, 87, 112, 165, 222, 245, 40, 72, 141, 246, 186, 92, 238, 255, 173, 
    37, 111, 240, 15, 76, 126, 248, 199, 122, 148, 213, 16, 48, 34, 78, 103, 47, 158, 219, 52, 73, 
    18, 106, 94, 183, 99, 53, 214, 83, 177, 107, 100, 211, 61, 66, 51, 138, 239, 31, 80, 8, 250, 71, 
    55, 109, 184, 229, 215, 10, 162, 151, 216, 180, 129, 136};
    
    int main() {
        int x, y;
        
        for(x = 0; x < sizeof(lookup)/sizeof(*lookup); x ++) {
            for(y = 0; y < sizeof(lookup)/sizeof(*lookup); y ++) {
                if(lookup[y] == x) {
                    printf("&#37;i, ", y);
                    break;
                }
            }
        }
        
        return 0;
    }
    The output is: 144, 6, 43, 18, 149, 87, 147, 112, 241, ...

    Unless speed is an issue you can just generate that on-the-fly in your program.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Well if we got the same table why isn't it all working right?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Give me a minute, I'm working on it . . .

    The logic must be messed up. If you call dSubstitution() and then eSubstitution(), the last three characters work, at least with my test.
    Last edited by dwks; 06-27-2007 at 03:47 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM