将数字ID转换为简短的不同字母数字代码的算法

编程入门 行业动态 更新时间:2024-10-25 18:35:04
本文介绍了将数字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过正-但这是示例: 安装github/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 github/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更改为更大的数字以使用更少的字符.请参阅 stackoverflow/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 stackoverflow/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转换为简短的不同字母数字代码的算法

本文发布于:2023-11-30 09:58:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   转换为   简短   算法   字母

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!