How to decrypt a large AES encrypted data with openssl command? - encryption

I want to decrypt large AES encrypted data:
Encrypted data: 7dS560FiDJvUji7c+ky/9nQp+HwK3SEOkY4zefqlFuEyF2/vevrv8K4GJNr2zxD7+j28RK97LmaHjwY9F+/feO6T2DAQuJQsT3wp/fKCPyErwZggsE+ufzvEJZK/XObR8CdbBe8EGZC//JsuMo7MV6SSe267jp4U2iIz4xIFxUlFVy9QcjTr6AUJZdVogedreBgy3/q/isAqOvwoHuj74MV5gkfaXWvmqESjMF4s5wMsYmS1WuBWOR6rw+TSwuL52CiVC5arHuJJEKuodO58bJ+1Ip9uelUIOETswiZmZB6gpTkbos7ij0BpJregejT+9vsDO1tB7+QpByMqeeEbEiSp4N833Z29WmVmy+HpxdZvOPIJ2pyelVhRVPPs/V7VREs4v8/e3X1jR0saA1sSyKYNgIGZUWaQTJwBNi87IPkGiUtyhPnrrKh+BighshJi+Jd+s3+sTYM5CaADvaGhm3wx96rBQi2So+NmFcm5cQa4jRA4S8sLGHmniUO0iiEcxQ2FvlGt+0PBqdbCPewBgOHdjpBUYjs1oR4pV5VjbjTfuIh75G7TS6TZT/gMewJXOXPw88zsSgjXVjRK0obOXaJBKABJ8AqKbKULWHoeMuWW4OdiSZ7mNVt6N40LK/u12vpqWU7Wt5XbSeE3arJPjljEV7WhafKvIOHFx+cK0wB9LQnFUrx/FTLL5HDDEkhnVkm02vxXSFKDwP36VgQnPpvOnY5fbJUTyjnwJcO8I9/H1hYwzPRhos2eNAdHZ3aMysU8zSobwUVto9GnQrvVmVURFyEX6fwifkrf2DTfCsDNSsHt4DLCtsszABKTwTezUoBesNkndk/Qkcd13GEZjTHoyP2tLp85nYwbwI6P+yr1jeu9v3ocN5p0LWPXdwlyTo55WeYkGzlJLsM6d9sut1qlcyQ2AZ536+eYFAIURJkmSexwfbdRebwckvqURRO3a55WCbLxVARKDHnyfculr/ndwyZF7J0RX4G1KcwSgfRdnBpUCkpGeQRImv5Ry5RYXwp5XfKQDrGTCEhV3XtDBD4b2eTxw4KwgJUq9T6IQjNNyPyDwM5AFxS2Y+4wAA0PA2iADfVe3kZrOKYpZvVn0Q== (base64 encoded)
Key: uHe2MCmggLlugpGBiMVuXTck7OT8Nk8g (base64 encoded)
Cipher: AES-256-CBC
IV: LNP8U7pc6GjxzxAtgw4s3A== (base64 encoded)
I try to use this command for it:
openssl aes-256-cbc -d -in data.enc -out data.dec -K $key -iv $iv
In $key and $iv i decoded from encoded values this way
iv=$( cat response.json | jq .iv -r | openssl base64 -d | xxd -p | tr -d '\n' )
But i have crashed data like this
^GÌ<8a>û)F"PEi~^K±jÔ^AcWSM
23NDwSOqovXSFGNfy3WatkCreYRd7kcWSM";

Related

How to decode openssl enc'd data with salted password?

$ curl -s 'https://finance.yahoo.com/quote/MSFT/profile?p=MSFT' | awk -v ORS= 'match($0, /^ *root[.]App[.]main = (.*);$/, a) { print a[1] }' | jq -r .context.dispatcher.stores > /tmp/tmp.txt
$ file /tmp/tmp.txt
/tmp/tmp.txt: openssl enc'd data with salted password, base64 encoded
How can I decode the above data?

Decrpyting using pkcs#11 tool

I'm trying too encrypt and decrypt using pkcs#11-tool
echo 'Hello word !' | openssl pkeyutl -encrypt -pubin \
-inkey pub.pem \
-pkeyopt rsa_padding_mode:oaep \
-pkeyopt rsa_oaep_md:sha512 \
-pkeyopt rsa_mgf1_md:sha512 \
-out encrypted.data
This work find but trying to decrypt it fail
pkcs11-tool --module /usr/local/lib/softhsm/libsofthsm2.so --decrypt \
--mechanism RSA-PKCS-OAEP \
--input-file encrypted.data \
--label keyp \
--hash-algorithm SHA512 --pin Jojo
I'm having this error :
Using slot 0 with a present token (0x607f2e7f)
Using decrypt algorithm RSA-PKCS-OAEP
OAEP parameters: hashAlg=SHA512, mgf=MGF1-SHA512, source_type=0, source_ptr=(nil), source_len=0
error: PKCS11 function C_DecryptInit failed: rv = CKR_ARGUMENTS_BAD (0x7)
Aborting.
I'm following this tutorial : https://docs.nitrokey.com/nethsm/guides/pkcs11_with_pkcs11-tool and can't what wrong.

How to decrypt AES encrypted data with openssl command? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I want to decrypt AES encrypted data with openssl command.
Encrypted data: GD5YV2naJZ/x3mQnfictWQ== (base64 encoded)
Key: uHe2MCmggLlugpGBiMVuXTck7OT8Nk8g
Cipher: AES-256-CBC
IV: LNP8U7pc6GjxzxAtgw4s3A== (base64 encoded)
Follow these steps:
$ echo GD5YV2naJZ/x3mQnfictWQ== | openssl base64 -d > data.enc
$ iv=$( echo LNP8U7pc6GjxzxAtgw4s3A== | openssl base64 -d | xxd -p | tr -d '\n' )
$ echo $iv
2cd3fc53ba5ce868f1cf102d830e2cdc
$ key=$( echo uHe2MCmggLlugpGBiMVuXTck7OT8Nk8g | xxd -p | tr -d '\n' )
$ echo $key
754865324d436d67674c6c7567704742694d56755854636b374f54384e6b38670a
$ openssl aes-256-cbc -d -in data.enc -K $key -iv $iv
s:4:"Test";
With -base64 option, the decrypt command can directly use base64 encoded data as the input:
$ echo GD5YV2naJZ/x3mQnfictWQ== | openssl aes-256-cbc -d -base64 -K $key -iv $iv
s:4:"Test";
Please follow the encryption and decryption of the string according to its documentation and follow the step as well:
to decrypt a string use the following:
Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Facades\Crypt;
try {
$decrypted = Crypt::decryptString($encryptedValue);
} catch (DecryptException $e) {
//
}
for encryption use:
use Illuminate\Support\Facades\Crypt;
Crypt::encryptString($request->token)

How do I decrypt the ciphertext given the private key with RSA?

I have this private key and I am trying to decrypt the ciphertext:
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAkfmJTVkwbRXOtWN76j2MAmbwCMReCkV3hWG+23zEmivwVEUtlUNvmFkSx9fFSRgGLMW+TPleLybU629fYDTXSPmIzLM3faxIyzyI2LJdRAaQMWo1FUcVI8ME5/BX9T5Ebu6D2dA8zJzJ68dGkHmhSxhOC97vLQfjj3tNC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TYr/ztjt9FIg00QiSF5tR/6ssGAMaGaDnpZqmUekLB7BqWqQSOxIM0CrRw4dRRNbvviTbir0k+dVrfXCO8G2hyPnYIjl0MR6JptdnznVDVRhAbQo7ysRMLg84cEnvrnX0HPcWhdhIKcQvWDKHb9Uoec/Dz4KhFN+L/AoGATnaM4pl/tv735KcNn+qpgyB2Qd01qasRahecchPZqujH0+roS3llDJFN09NG9BzmAQFi2yH7Asx5KmIHqyWiYqyMXGUcsauT3u+xsDYWxObqAW7q7sMCKlfRtsnEWli3FOWNufO0TBwGeL9xVjL3nXXIsD9J5/iq1cH/RuIVoX4=
-----END RSA PRIVATE KEY-----
The ciphertext:
M8zhAvxu/lkEcH42Eoe/cPjL6WJwPo1FZOK8Ta4SmigoHl9omd5Dol84vY3GfyzIZ0hHHp5gO3A4MjktImsLnT3Xx/myUearG/Ii24C+6irFapMqghziuMDYpeh7rs8G8ggE/rkjZaAAPSb/FgEDQkknL2qr4kxwyQybsnfmDDUqJ3q3lf0gRBDJ/2yr+aQyUcw3nK/zewbZjwlUeSyTvs76HhFtUDTyAlhB37961j9qfu+cxX34PtJlHWlF7k0WAuxQ4w7aCwkuQIvBOk/FUK3Ta1TrGEXg9iczPj6QacLI2DSjEPbODHsje82Fw+1ylcD1tF2mZ7GnnqtUw/9YoQ==
I tried to use these commands but without sucess:
openssl rsautl -decrypt -in ciphertext -out plaintext -inkey private_key
In this case I get the fallowing error:
rsa routines:RSA_EAY_PRIVATE_DECRYPT:data greater than mod len:rsa_eay.c:518
openssl enc -in ciphertext -out binarytext -d -a
openssl rsautl -decrypt -in binarytext -out plaintext -inkey private_key
If I try these 2 commands I get this error:
rsa routines: RA_padding_check_PKCS1_type_2:pkcs decoding error
rsa routines: RSA_EAY_PRIVATE_DECRYPT:padding check failed

AES-CBC incorrect usage

I am trying to do AES-CBC cipher and decipher via openssl, however, I am not able to get the correct output. Please advise me. Thank you.
cipher
clr;
MSG_CIPHERED_HEX="920e5af8b78702c778a919f7969a1f8cba578f11693673035213daf02500c50a"
IV="00000000000000000000000000000000"
KEY="00000000000000000000000000000000"
echo -n "$MSG_CIPHERED_HEX" | xxd -r -p | openssl enc -aes-128-cbc -K $KEY -iv $IV | xxd -p | tr -d '\n'
decipher
clr;
MSG_CIPHERED_HEX="734563526574204d6553734167452030300a0e0e0e0e0e0e0e0e0e0e0e0e0e0e"
echo $MSG_CIPHERED_HEX
MSG_ASCII=echo $MSG_HEX | xxd -p -r | tr -d '\n'; echo $MSG_ASCII > tmp.ciphered.file.ascii;
openssl enc -d -aes-128-cbc -K $KEY -iv $IV -in tmp.ciphered.file.ascii -out out.txt
cat out.txt | xxd -p # | tr -d '\n'
rm -rf tmp.ciphered.file.ascii out.txt
There seemed to be data formatting issue:
$ #decipher
MSG_CIPHERED_HEX="920e5af8b78702c778a919f7969a1f8cba578f11693673035213daf02500c50a" IV="00000000000000000000000000000000" KEY="00000000000000000000000000000000" echo -n "$MSG_CIPHERED_HEX" | xxd -r -p | openssl enc -d -aes-128-cbc -K $KEY -iv $IV | xxd -p | tr -d '\n'
Ans:
734563526574204d6553734167452030300a (truncated)
$ #cipher
MSG_CIPHERED_HEX="920e5af8b78702c778a919f7969a1f8cba578f11693673035213daf02500c50a" IV="00000000000000000000000000000000" KEY="00000000000000000000000000000000" echo -n "$MSG_CIPHERED_HEX" | xxd -r -p | openssl enc -aes-128-cbc -K $KEY -iv $IV | xxd -p | tr -d '\n'
Ans:
236999001256bd4131dffa3417c29bfc597a43f6bde387ba0e42da86e67cfff42890e4f6e84c0e70753a9db754df996e
The e0e0e0e0e0e0e0e0e0e0e0e0e0e is 14 bytes of padding. If you specify padding on decryption it will be automatically removed.
See PKCS7 padding.

Resources