代换密码用不同的字母长度字母、长度、不同、密码

2023-09-11 00:15:51 作者:吹乱心事

我想实现一个简单的替换密码来掩盖私人IDS中的URL。

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

我知道我的ID将看起来像(大写ASCII字母,数字和下划线组合),他们将是相当长的,因为它们是由密钥。我想用一个较长的字母,以缩短所产生的codeS(我想使用大小写ASCII字母,数字和别的)。所以,我进来的字母是

I know how my IDs will look like (combination of uppercase ASCII letters, digits and underscore), and they will be rather long, as they are composed keys. I would like to use a longer alphabet to shorten the resulting codes (I'd like to use upper- and lowercase ASCII letters, digits and nothing else). So my incoming alphabet would be

[A-Z0-9_] (37 chars)

和我即将离任的字母是

[A-Za-z0-9] (62 chars)

所以,几乎50%的 COM pression 的COM pression合理数额将是可用的。

so a compression of almost 50% reasonable amount of compression would be available.

比方说,我的网址是这样的:

Let's say my URLs look like this:

/my/page/GFZHFFFZFZTFZTF_24_F34

和我希望他们看起来像这样:

and I want them to look like this instead:

/my/page/Ft32zfegZFV5

显然,这两个数组将被洗牌带来一些随机排列。

Obviously both arrays would be shuffled to bring some random order in.

此不必是安全的。如果有人数字出来:很好,但我不希望这个计划是显而易见的。

This does not have to be secure. If someone figures it out: fine, but I don't want the scheme to be obvious.

我的期望的解决方案是将字符串转换为重新基数37 presentation一个整数,基数转换为62并使用第二个字母写出来的数字。有什么样code可用,做类似的事情? 的Integer.parseInt() 有一些类似的逻辑,但它是硬codeD使用标准的数字行为。

My desired solution would be to convert the string to an integer representation of radix 37, convert the radix to 62 and use the second alphabet to write out that number. is there any sample code available that does something similar? Integer.parseInt() has some similar logic, but it is hard-coded to use standard digit behavior.

任何想法?

我使用Java来实现这一点,但其他语言code或伪code是当然也是有帮助的。

I am using Java to implement this but code or pseudo-code in any other language is of course also helpful.

推荐答案

莫名 Character.MAX_RADIX 只有36,但你可以写你自己的基本转换程序。下面的实现是不高的表现,但它应该是一个很好的起点:

Inexplicably Character.MAX_RADIX is only 36, but you can always write your own base conversion routine. The following implementation isn't high-performance, but it should be a good starting point:

import java.math.BigInteger;
public class BaseConvert {
    static BigInteger fromString(String s, int base, String symbols) {
        BigInteger num = BigInteger.ZERO;
        BigInteger biBase = BigInteger.valueOf(base);
        for (char ch : s.toCharArray()) {
            num = num.multiply(biBase)
                     .add(BigInteger.valueOf(symbols.indexOf(ch)));
        }
        return num;
    }
    static String toString(BigInteger num, int base, String symbols) {
        StringBuilder sb = new StringBuilder();
        BigInteger biBase = BigInteger.valueOf(base);
        while (!num.equals(BigInteger.ZERO)) {
            sb.append(symbols.charAt(num.mod(biBase).intValue()));
            num = num.divide(biBase);
        }
        return sb.reverse().toString();
    }
    static String span(char from, char to) {
        StringBuilder sb = new StringBuilder();
        for (char ch = from; ch <= to; ch++) {
            sb.append(ch);
        }
        return sb.toString();
    }
}

然后你就可以有一个的main()测试工具这样的:

public static void main(String[] args) {
    final String SYMBOLS_AZ09_ = span('A','Z') + span('0','9') + "_";
    final String SYMBOLS_09AZ = span('0','9') + span('A','Z');
    final String SYMBOLS_AZaz09 = span('A','Z') + span('a','z') + span('0','9');

    BigInteger n = fromString("GFZHFFFZFZTFZTF_24_F34", 37, SYMBOLS_AZ09_);

    // let's convert back to base 37 first...
    System.out.println(toString(n, 37, SYMBOLS_AZ09_));
    // prints "GFZHFFFZFZTFZTF_24_F34"

    // now let's see what it looks like in base 62...       
    System.out.println(toString(n, 62, SYMBOLS_AZaz09));
    // prints "ctJvrR5kII1vdHKvjA4"

    // now let's test with something we're more familiar with...
    System.out.println(fromString("CAFEBABE", 16, SYMBOLS_09AZ));
    // prints "3405691582"

    n = BigInteger.valueOf(3405691582L);
    System.out.println(toString(n, 16, SYMBOLS_09AZ));
    // prints "CAFEBABE"        
}

若干意见

的BigInteger 可能是最简单的,如果数量能超过 您可以洗牌字符中的符号字符串,只要坚持一个秘密排列

Some observations

BigInteger is probably easiest if the numbers can exceed long You can shuffle the char in the symbol String, just stick to one "secret" permutation

您不能普遍预期基62字符串一半左右,短则基36字符串。这里的为Long.MAX_VALUE 在基地10,20,30:

You can't generally expect the base 62 string to be around half as short as the base 36 string. Here's Long.MAX_VALUE in base 10, 20, and 30:

    System.out.format("%s%n%s%n%s%n",
        Long.toString(Long.MAX_VALUE, 10), // "9223372036854775807"
        Long.toString(Long.MAX_VALUE, 20), // "5cbfjia3fh26ja7"
        Long.toString(Long.MAX_VALUE, 30)  // "hajppbc1fc207"
    );