Skip to content

Instantly share code, notes, and snippets.

@TomZhuPlanetart
Last active April 7, 2024 15:09
Show Gist options
  • Save TomZhuPlanetart/c8ec6c6bfe1b0f2a606d4b0f09116394 to your computer and use it in GitHub Desktop.
Save TomZhuPlanetart/c8ec6c6bfe1b0f2a606d4b0f09116394 to your computer and use it in GitHub Desktop.
Encrypt and Decrypt with openssl

Encrypt with AES

Let's encrypt something

echo hello world | openssl enc -aes-256-cbc -pass pass:hello -iter 10 -in - -out - | base64

The output looks like:

U2FsdGVkX19BU/HH1IvplMwhFBzzRZZwDgtldl2ROvA=

Now we decrypt it:

echo U2FsdGVkX19BU/HH1IvplMwhFBzzRZZwDgtldl2ROvA= | base64 -d \
  | openssl enc -d -aes-256-cbc -pass pass:hello -iter 10 -in - -out -

The output:

hello world

Encrypt With RSA

Encryption

note: RSA encryption has a limitation in the size of data that can be encrypted based on the size of the key.

echo -n helloworld | openssl rsautl -encrypt -pubin -inkey ~/.ssh/pub1.pem -in -  -out - | base64

Decryption:

echo JVmtAhQbR7VhrWLma/GqKwpaIzPJ+EXKB5N6EAaHGhS4EcHA8bxFA0v0N3jBm712bxCwXwX+qjOGBw+FCzul9fjw+eTzr/A8BxEF2XFwjUF8tQrq0a/lHC/CLRkbNEHQ+/DPP89V3UyKO4tyVIaKIfjs1auFi9lgy0dkoefqiatkGomgsRBaFuHrpA8hSSFjIoj2bhFYyuhNGikM91YlaE4fXBvLjIPSmVyuvqdMB0/eXCCC9Hherfv7blPc3Eb4ofXcSFobZ0IX1pdpEQwhh4pRIMmXVBxjYSKPGn5bBmIJUETW3FfXUdxbcaN0gQ+biybBH9dg3UEM0NGacI7KAA== \
  | base64 -d | openssl rsautl -decrypt  -inkey ~/.ssh/id_rsa -in -  -out -

References:

OPENSSL Manual

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment