简体   繁体   中英

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. Therefore when user enter this value A3D5F2ARD3 , system will based on this value and decrypt it to get back the value BI000001 .

is it possible to do this in 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. Then again, I am not an encryption expert, but I guess such a custom feature won't be built in the JDK. Maybe this discussion is also useful: 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. Block ciphers (as their name suggest) work in blocks of data of a fixed size, for example AES works in blocks of 128 bits. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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