substitution cipher with different alphabet length

前端 未结 3 2079
-上瘾入骨i
-上瘾入骨i 2020-12-20 18:33

I would like to implement a simple substitution cipher to mask private ids in URLs.

I know how my IDs will look like (combination of uppercase ASCII letters, digits

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 19:03

    I now have a working solution which you can find here:

    http://pastebin.com/Mctnidng

    The problem was that a) I was losing precision in long codes through this part:

    value = value.add(//
        BigInteger.valueOf((long) Math.pow(alphabet.length, i)) // error here
            .multiply(
                BigInteger.valueOf(ArrayUtils.indexOf(alphabet, c))));
    

    (long just wasn't long enough)

    and b) whenever I had a text that started with the character at offset 0 in the alphabet, this would be dropped, so I needed to add a length character (a single character will do fine here, as my codes will never be as long as the alphabet)

提交回复
热议问题