简体   繁体   English

如何确保加密长度与给定的字符串相同

[英]How to ensure the encrypted length are same as the given string

Hi is it possible to encrypt the string with the certain length that i want? 嗨,有可能用我想要的一定长度来加密字符串吗? for example: i want to encrypt this string BI000001 to something like hex value A3D5F2ARD3 (random) fixed it at 10 length. 例如:我想将此字符串BI000001加密为十六进制值A3D5F2ARD3 (随机),将其长度固定为10。 Therefore when user enter this value A3D5F2ARD3 , system will based on this value and decrypt it to get back the value BI000001 . 因此,当用户输入该值A3D5F2ARD3 ,系统将基于该值并将其解密以获取值BI000001

is it possible to do this in java? 有可能在Java中做到这一点吗?

I tried a lot of method but all encrypted length are way too long. 我尝试了很多方法,但是所有加密长度都太长。

I am not aware of any JDK built-in Java encryption method which provides this feature. 我不知道任何提供此功能的JDK内置Java加密方法。 Then again, I am not an encryption expert, but I guess such a custom feature won't be built in the JDK. 再说一次,我不是加密专家,但是我想JDK不会内置这种自定义功能。 Maybe this discussion is also useful: https://crypto.stackexchange.com/questions/6098/is-there-a-length-preserving-encryption-scheme 也许此讨论也很有用: https : //crypto.stackexchange.com/questions/6098/is-there-a-length-preserving-encryption-scheme

Why do you want to preserve size of the string? 为什么要保留字符串的大小? maybe there is another solution for your problem. 也许您的问题还有另一种解决方案。

Typically you would use a block cipher such as AES to encrypt data. 通常,您将使用诸如AES之类的分组密码来加密数据。 Block ciphers (as their name suggest) work in blocks of data of a fixed size, for example AES works in blocks of 128 bits. 分组密码(顾名思义)以固定大小的数据块工作,例如AES以128位的块工作。 If a block cipher encounters input smaller than the block size it pads it , which is likely why you are seeing the ciphertext larger than the plaintext. 如果分组密码遇到小于分组大小的输入,它将填充它 ,这很可能就是您看到的密文大于明文的原因。

If you want to preserve the length then consider Format Preserving Encryption as mentioned in this question . 如果要保留长度,请考虑此问题中提到的“格式保留加密”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM