加密随机 6 位数字 -> 人类可读
Encrypt random 6 digit number -> human readable
我们有从 1 到 999999 的数字,我们需要使用简单的算法进行加密。加密输出应为人类可读字符(A-Z、0-9)且不超过 6 位数字。
是否可以使用简单的算法将数字 123456 加密为例如GH6ZT3 及以后解密 GH6ZT3 为 123456 ?
我只能找到使用 Base32 或 Base64 加密的示例,但加密输出远远大于 6 位数字:-(
如果你加密你想要一个人类 non-readable 方式来表示数字。
要完成你想做的事,你应该改变数字的底数。十六进制(以 16 为底)的东西应该会产生一些好的结果。尝试另一个基数,如 20.
看看:In Java how do you convert a decimal number to base 36?
或者,您可以选择一个 Classical Criptography, like in Ancient Rome。但是您将需要与要阅读的人更改密钥。一个键,就是任意一个字来组合数字。
如果您只是想混淆这些数字,那么结合模块化算法和 base 36 字符串可以很容易地完成。
这里有几个函数可以处理 0 到 (366 − 1 之间的任何数字:
// Convert an integer (from 0 to 2176782335) into a 6-digit string in base 36.
// The result is obfuscated by using modular arithmetic, i.e. multiply by the
// (arbitrarily selected) prime number 1708159939 modulo 36^6 (2176782336)
public static String obfuscate(Long n) {
Long x;
x = (n * 1708159939L) % 2176782336L;
return String.format("%6s",Long.toString(x, 36).toUpperCase()).replace(' ','0');
}
// Inverse of the above function. Converts a 6-character base 36 string into
// an integer value. The number 1553655019 is the modular inverse of 1708159939
// (i.e., 1708159939 * 1553655019 = 1 (mod 36^6)
public static Long deobfuscate(String s) {
return (Long.valueOf(s, 36) * 1553655019L) % 2176782336L;
}
但是请记住,经过混淆处理的字符串将包括每个不超过 6 个字母的单词。这包括所有 four-letter words.
我们有从 1 到 999999 的数字,我们需要使用简单的算法进行加密。加密输出应为人类可读字符(A-Z、0-9)且不超过 6 位数字。
是否可以使用简单的算法将数字 123456 加密为例如GH6ZT3 及以后解密 GH6ZT3 为 123456 ?
我只能找到使用 Base32 或 Base64 加密的示例,但加密输出远远大于 6 位数字:-(
如果你加密你想要一个人类 non-readable 方式来表示数字。
要完成你想做的事,你应该改变数字的底数。十六进制(以 16 为底)的东西应该会产生一些好的结果。尝试另一个基数,如 20.
看看:In Java how do you convert a decimal number to base 36?
或者,您可以选择一个 Classical Criptography, like in Ancient Rome。但是您将需要与要阅读的人更改密钥。一个键,就是任意一个字来组合数字。
如果您只是想混淆这些数字,那么结合模块化算法和 base 36 字符串可以很容易地完成。
这里有几个函数可以处理 0 到 (366 − 1 之间的任何数字:
// Convert an integer (from 0 to 2176782335) into a 6-digit string in base 36.
// The result is obfuscated by using modular arithmetic, i.e. multiply by the
// (arbitrarily selected) prime number 1708159939 modulo 36^6 (2176782336)
public static String obfuscate(Long n) {
Long x;
x = (n * 1708159939L) % 2176782336L;
return String.format("%6s",Long.toString(x, 36).toUpperCase()).replace(' ','0');
}
// Inverse of the above function. Converts a 6-character base 36 string into
// an integer value. The number 1553655019 is the modular inverse of 1708159939
// (i.e., 1708159939 * 1553655019 = 1 (mod 36^6)
public static Long deobfuscate(String s) {
return (Long.valueOf(s, 36) * 1553655019L) % 2176782336L;
}
但是请记住,经过混淆处理的字符串将包括每个不超过 6 个字母的单词。这包括所有 four-letter words.