Encrypt a string using openssl command line

后端 未结 4 1877
迷失自我
迷失自我 2020-12-07 22:36

I have a 16 byte character that I would like to encrypt using openssl into a 16 byte encrypted string.

This encrypted string ( in human readable format ) then nee

4条回答
  •  自闭症患者
    2020-12-07 23:39

    I have a 16 byte character that I would like to encrypt using openssl into a 16 byte encrypted string [in human readable format]

    I believe you are looking for Format Preserving Encryption. I think the caveat is you have to start with a 16-byte human readable string. Phillip Rogaway has a paper on the technologies: Synopsis of Format-Preserving Encryption. There's a lot to the paper, and it can't fit into a single paragraph on Stack Overflow.

    If you can start with a shorter string and use a streaming mode like OCB, OFB or CTR, then you can Base64 encode the final string so that the result is 16-bytes and human readable. Base64 expands at a rate of 3 → 4 (3 un-encoded expands to 4 encoded), so you'd need a shorter string of length 12 characters to achieve 16 human readable characters.

    As far as I know, there are no command line tools that do it natively. You may be able to use OpenSSL on the command line with AES/CTR and pipe it through base64 command. The following gets close, but it starts with 11 characters (and not 12):

    $ echo 12345678901 | openssl enc -e -base64 -aes-128-ctr -nopad -nosalt -k secret_password
    cSTzU8+UPQQwpRAq
    

    Also, you really need to understand te -k option (and -K for that matter), and how it derives a key so you can do it outside of the OpenSSL command (if needed).

提交回复
热议问题