java - Difference between these two "methods" for simple character encryption -


i have trouble understanding simple method encrypting characters in string. here's method:

encryptedchar = (char) (’a’ + (originalchar -’a’ + offset) % 26); 

i don't understand need 'a' - 'a' since cancel out. reason behind it?

why shouldn't use following method?

encryptedchar = (char) ((originalchar + offset) % 26); 

shouldn't work same?

encryptedchar = (char) ('a' + (originalchar -'a' + offset) % 26); 

the 2 'a' don't cancel each other, since second 1 inside expression operand of modulus operator.

  • 'a' + (originalchar -'a' + offset) % 26 - here each letter mapped different letter.

  • ((originalchar + offset) % 26) - here each letter mapped character int value between 0 , 25.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -