Breakpoint located in string encryption cipher

0

I wrote a little string encryption algorithm, very simple.

This is it:

int chrCipher (int str)
{
    char a[] = "abcdefghijklmnopqrstuvwxyz";
    char b[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char *c;
    if ((c=strchr(a,str))!=NULL)
        return a[((c-a)+13)%26];
    if ((c=strchr(b,str))!=NULL)
        return b[((c-b)+13)%26];
    else return str;
}

void strCipher (char *dst, char *src)
{
    while(*src)
    {
        *src=chrCipher(*src); // BREAKPOINT HERE
        *++dst;
        *++src;
    }
}

When I run my program in MSVS I get a breakpoint at this section, and it says "unhandled exception at 0x0169b823 : 0xC0000006 acess violation writing 0x01395474"

What is the problem here, and how can I fix this?

c++
c
winapi
pointers
cryptography
asked on Stack Overflow Mar 12, 2014 by user3267146 • edited Mar 12, 2014 by pmg

1 Answer

2

I guess your problem is with a wrong update to src rather than dst

void strCipher (char *dst, char *src)
{
    while(*src)
    {
        *src=chrCipher(*src); // <<== did you mean *dst = chrCipher(*src); here??
        *++dst;
        *++src;
    }
}

Also the dereference in the lines that update the pointers aren't needed.

I would have added a const to the src parameter ...

void strCipher (char *dst, const char *src); // prototype
answered on Stack Overflow Mar 12, 2014 by pmg

User contributions licensed under CC BY-SA 3.0