将数字ID转换为简短的不同字母数字代码的算法 [英] Algorithm to turn numeric IDs in to short, different alphanumeric codes

查看:484
本文介绍了将数字ID转换为简短的不同字母数字代码的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库中的ID,我希望它们简短易懂,而且可以通过眼睛区分(即,两个接近的数字看起来不同).

I have IDs from a database, and I want them to be short and easily differentiatable by eye (i.e., two close numbers look different).

赞:

13892359163211-> ALO2WE7
13992351216421-> 52NBEK3

13892359163211 -> ALO2WE7
13992351216421 -> 52NBEK3

或类似的算法.有点像哈希,但它需要可逆吗?像AES这样的加密算法几乎是理想的选择,除了它的输出太长之外. (和过度杀伤力).

or similar, algorithmically. So kind of like a hash, except it needs to be reversible? An encryption algorithm like AES is almost ideal, except that its outputs are way too long. (and overkill).

我正在使用Python(3),尽管我认为这并不重要

I'm using Python (3), although I don't think that should really matter

推荐答案

新答案,其中接近"数字看起来不同

New answer with 'close' numbers looking different

您可以使用RSA加密(然后解密)您的号码.这绝对是矫kill过正-但这是示例: 安装https://github.com/sybrenstuvel/python-rsa(pip install rsa)

You could use RSA to encrypt (and later decrypt) your numbers. This is definitely overkill - but ... here is the example: Install https://github.com/sybrenstuvel/python-rsa (pip install rsa)

import rsa
import rsa.core
# (pubkey, privkey) = rsa.newkeys(64) # Generate key pair
pubkey = rsa.PublicKey(n=9645943279888986023, e=65537)
privkey = rsa.PrivateKey(n=9645943279888986023, e=65537, d=7507666207464026273, p=9255782423, q=1042153201)

print("1st", rsa.core.encrypt_int(13892359163211, pubkey.e, pubkey.n))
print("2nd", rsa.core.encrypt_int(13992351216421, pubkey.e, pubkey.n))
print("1st", hex(rsa.core.encrypt_int(13892359163211, pubkey.e, pubkey.n))[2:])
print("2nd", hex(rsa.core.encrypt_int(13992351216421, pubkey.e, pubkey.n))[2:])

# If you want to compare a couple of numbers that are similar
for i in range (13892359163211, 13892359163251):
  encrypted = rsa.core.encrypt_int(i, pubkey.e, pubkey.n)
  # decrypted = rsa.core.decrypt_int(encrypted, privkey.d, privkey.n)
  print (i, hex(encrypted)[2:], encrypted)

请不要不要加密大于pubkey.n的数字.这是与RSA相关的限制.通过生成具有更高n的不同密钥对,可以避免此问题.如果您希望所有生成的数字都具有相同的长度,请在它们前面加上前导零.您也可以考虑将它们大写以提高可读性.为了使显示的字符串更短,请考虑使用下面我的旧答案中提到的base62编码.

Please not that you cannot encrypt numbers bigger than pubkey.n. This is a RSA related limitation. By generating a different keypair with a higher n you can circumvent this issue. If you would like all generated numbers to have the same length, prefix them with leading zeroes. You could also consider making them uppercase for better readability. To make the displayed strings shorter consider using the base62 encoding mentioned in my old answer below.

输出

1st 5427392181794576250
2nd 7543432434424555966
1st 4b51f86f0c99177a
2nd 68afa7d5110929be

input          hex(encrypted)   encrypted
13892359163211 4b51f86f0c99177a 5427392181794576250
13892359163212 2039f9a3f5cf5d46 2322161565485194566
13892359163213 173997b57918a6c3 1673535542221383363
13892359163214 36644663653bbb4  244958435527080884
13892359163215 c2eeec0c054e633  877901489011746355
...

旧答案 与显示数字短一些有关,而没有意识到它们看起来应该大不相同

您希望将数字的基数从10更改为更大的数字以使用更少的字符.请参阅 https://stackoverflow.com/a/1119769 以62为基数(a-zA-Z0-9)的示例.

You want to change the base of your number from 10 to something bigger to use less characters. See https://stackoverflow.com/a/1119769 for an example with base 62 (a-zA-Z0-9).

或者基数为16(0-9A-F,十六进制)的快捷键.

Or quick and dirty for base 16, (0-9A-F, hexadecimal).

hex(13892359163211)[2:] # -> 'ca291220d4b'

这篇关于将数字ID转换为简短的不同字母数字代码的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆