通过 32 个字符密钥加密解密 12 位文本
我正在寻找一种通过 32 个字符长的密钥加密和解密 12 位文本的方法。 密码必须具有固定长度(32 或更少)。可能吗?
提前致谢
I am looking for a way to encrypt and decrypt 12 digits text by 32 characters long key. The cipher must be of fixed length (32 or less). Is it possible?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然。使用良好的分组密码(例如 AES),您可以选择将文本加密为块(输出将是一个 32 字符块)并且您将拥有 256 位加密或使用加密的随机数对文本进行异或(输出将是 12 字节密文)并且您将拥有 96 位加密。
只需谷歌搜索 AES 和 C# 就应该能找到一个现成的实现。请务必使用正确的 nonce (在某些情况下也称为 初始化向量)。
要根据您的目的使用哈希(请参阅对此答案的评论),请按以下步骤操作:
哈希:
计算
HASH = hash(FROM_DATE + TO_DATE + SECRET)
。输出
FROM_DATE + TO_DATE + HASH
。+
表示连接,SECRET
只有您知道。如果仅使用大写字母和数字,则长度应至少 25 个字符。
验证:
将字符串拆分为
FROM_DATE + TO_DATE
和HASH
。验证
HASH = hash(FROM_DATE + TO_DATE + SECRET)
SHA-256 应该可以很好地实现这一点。
Of course. With a good block cipher (like AES), you can choose between encrypting text as a block (the output will be a 32 character block) and you'll have 256 bit encryption or XORing the text with an encrypted nonce (the output will be a 12 byte ciphertext) and you'll have 96 bit encryption.
Just googling for AES and C# should come up with a ready-to-use implementation. Be sure to use a proper nonce (in some contexts also called initialization vector).
To use a hash for your purposes (see comments on this answer), proceed as follows:
Hashing:
Compute
HASH = hash(FROM_DATE + TO_DATE + SECRET)
.Output
FROM_DATE + TO_DATE + HASH
.+
denotes concenation andSECRET
is only known to you.If using only capitals and numbers, it should be at least 25 characters long.
Verification:
Split string into
FROM_DATE + TO_DATE
andHASH
.Verify that
HASH = hash(FROM_DATE + TO_DATE + SECRET)
SHA-256 should work quite well for this.