Java, How to implement a Shift Cipher (Caesar Cipher)

后端 未结 5 1669
走了就别回头了
走了就别回头了 2020-11-27 20:26

I want to implement a Caesar Cipher shift to increase each letter in a string by 3.

I am receiving this error:

possible loss of precision required         


        
5条回答
  •  执念已碎
    2020-11-27 21:04

    Two ways to implement a Caesar Cipher:

    Option 1: Change chars to ASCII numbers, then you can increase the value, then revert it back to the new character.

    Option 2: Use a Map map each letter to a digit like this.

    A - 0
    B - 1
    C - 2
    etc...
    

    With a map you don't have to re-calculate the shift every time. Then you can change to and from plaintext to encrypted by following map.

提交回复
热议问题