I would like to know how to limit the ASCII range of encryption from 32 - 126.
For our task we are supposed to convert string to char and encrypt/decrypt each individual char.
I'm currently using this for encryption
int value = (value-32+shift)%95+32 //value is the current ascii value of a
//character
//the first shift is given by the user with regards
//to how many shifts he wants to do to the right
and this for decryption
int value = (value-32-shift)%95+32
my encryption is working perfectly (when I quote out the decryption function) but my decryption is not working how it is supposed to.
extra note: We only have to do a right shift when encoding, a single string is given for our entire program to encrypt and decrypt ("This is C++")
Give shift: 3
Wklv#lv#F..
DECODE
Wklv#lv#F.. //must be 'THIS is C++'
ENCODE //shift=15
This is C++ //must be 'cwx#/x#/R::'
DECODE
EYZdpZdp4{{ //must be 'THIS is C++'
ENCODE //shift=66
cwx#/x#/R:: //must be "8LMWbMWb'mm"
DECODE
This is C++
ENCODE //shift=94
cwx#/x#/R:: //must be 'SGHR~hr~B**'
DECODE
This is C++
Note: in process of adding more code description
Your problem is explained in Modulo operator with negative values. I'm not sure this is an exact duplicate. The problem is that decoding a cipher character like '!' with a shift of more than one (say "3")
int value = (value-32-shift)%95+32
= ('!'-32-3)%95+32
= (33-32-3)%95+32
= (-2)%95 + 32
= -2 + 32
= 30
Oops. You need to use:
int value = (value-32+(95-shift))%95+32