-4

I am doing a decryption program. I've been looking and trying to figure this out, but I don't see anything. So when I decrypt such as 9adwrqxvni0348&4#9 it comes out fine, but when I have an offset of 11 or more it decrypts all but the last character. No matter what the offset past 11 is the last character is that same. I just now stuck all letters in, and they work. It is just the last number character that does not work past 11.

for (int count = 0; count < length; count++)
{
    if (msg[count] >= 'a' && msg[count] <= 'z')//Letter wraping
    {
        dmsg += ((msg[count] - 'a' - offset + 26) % 26) + 'a';
    }
    else if (msg[count] >= '0' && msg[count] <= '9')//Number wraping
    {
        dmsg += (abs(msg[count] - '0' - offset + 10) % 10) + '0';
    }       
}
Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

-1

It looks to me that when the offset becomes greater than 10, adding 10 doesn't create the appropriate positive value on any digit that is equal to or less than the difference between the offset and 10. One work around is to mod 10 the offset:

dmsg += ( abs( msg[count] - '0' - ( offset % 10 ) + 10 ) % 10 ) + '0';
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • @JakePurdom - You're very welcome. One thing to keep in mind. The same thing will probably occur with letters, if the offset is greater than 26. – tinstaafl Mar 15 '17 at 21:25
  • @JakePurdom - If you've found an answer that answers your question, please remember to mark it so. Thanks. – tinstaafl Mar 15 '17 at 23:01