cryptography - How to encrypt a message with AES encryption? -
this question has answer here:
i read article pirate bay's secret aes encrypted message being solved. (this old news)
the solver used command in linux terminal decrypt message.
echo "jyo7wnzc8xht47qkwohfdvj6sc2qh+x5tbct+uetocijcjqnp/2f1viebr+ty0cz" | openssl aes-128-cbc -k $(printf wearetpb | sha256sum | head -c 32 | tr '[:lower:]' '[:upper:]') -nosalt -nopad -iv 0 -base64 -d -p
the decrpyted message link: https://www.youtube.com/watch?v=-yeg9dgrhha
i want encrpyt own custom message same way pirate bay did. assume need change encrypted text custom message , change command encrypt, rather decrypt. how do this?
it should simple as:
echo -n "https://stackoverflow.com/q/39197703/5128464" | openssl aes-128-cbc -k $(printf wearetpb | sha256sum | head -c 32 | tr '[:lower:]' '[:upper:]') -nosalt -iv 0 -e -base64
giving ciphertext:
l3iwqzree8r55mqmu13hn2s+mvks46flt6rvp3yijqvcpwazjlrnzph1fyx1qdkk
which can decrypted (using same technique, key, etc.):
echo "l3iwqzree8r55mqmu13hn2s+mvks46flt6rvp3yijqvcpwazjlrnzph1fyx1qdkk" | openssl aes-128-cbc -k $(printf wearetpb | sha256sum | head -c 32 | tr '[:lower:]' '[:upper:]') -nosalt -nopad -iv 0 -base64 -d
giving plaintext:
https://stackoverflow.com/q/39197703/5128464
but if @ original decrypted text (i.e. dump actual message bytes):
echo "jyo7wnzc8xht47qkwohfdvj6sc2qh+x5tbct+uetocijcjqnp/2f1viebr+ty0cz" | openssl aes-128-cbc -k $(printf wearetpb | sha256sum | head -c 32 | tr '[:lower:]' '[:upper:]') -nosalt -nopad -iv 0 -base64 -d | xxd -g1
you (please note 5 '00' bytes @ end of output):
0000000: 68 74 74 70 73 3a 2f 2f 77 77 77 2e 79 6f 75 74 https://www.yout 0000010: 75 62 65 2e 63 6f 6d 2f 77 61 74 63 68 3f 76 3d ube.com/watch?v= 0000020: 2d 59 45 47 39 44 67 52 48 68 41 00 00 00 00 00 -yeg9dgrhha.....
and if repeat same previous result:
echo "l3iwqzree8r55mqmu13hn2s+mvks46flt6rvp3yijqvcpwazjlrnzph1fyx1qdkk" | openssl aes-128-cbc -k $(printf wearetpb | sha256sum | head -c 32 | tr '[:lower:]' '[:upper:]') -nosalt -nopad -iv 0 -base64 -d | xxd -g1
you different ending (this time there 5 '05' bytes):
0000000: 68 74 74 70 3a 2f 2f 73 74 61 63 6b 6f 76 65 72 http://stackover 0000010: 66 6c 6f 77 2e 63 6f 6d 2f 71 2f 33 39 31 39 37 flow.com/q/39197 0000020: 37 30 33 2f 35 31 32 38 34 36 34 05 05 05 05 05 703/5128464.....
those bytes called padding bytes , must added aes cipher in cbc mode can not process messages length not n*128 bits (i.e. can not divided 16-byte blocks)
the padding used in original plaintext (i.e. 00 00 00 00 00
) zero padding, second 1 (04 04 04 04
) pkcs7 padding.
unfortunately 0 padding not directly supported in openssl command line utilities, have add manually (note added 0 bytes , additional -nopad
option):
echo -ne "https://stackoverflow.com/q/39197703/5128464\0\0\0\0\0" | openssl aes-128-cbc -k $(printf wearetpb | sha256sum | head -c 32 | tr '[:lower:]' '[:upper:]') -nosalt -nopad -iv 0 -e -base64
giving:
l3iwqzree8r55mqmu13hn2s+mvks46flt6rvp3yijquybigplgobxjxjmu/bxocr
you can check padding of new plaintext with:
echo "l3iwqzree8r55mqmu13hn2s+mvks46flt6rvp3yijquybigplgobxjxjmu/bxocr" | openssl aes-128-cbc -k $(printf wearetpb | sha256sum | head -c 32 | tr '[:lower:]' '[:upper:]') -nosalt -nopad -iv 0 -base64 -d | xxd -g1
to see padding correct now:
0000000: 68 74 74 70 3a 2f 2f 73 74 61 63 6b 6f 76 65 72 http://stackover 0000010: 66 6c 6f 77 2e 63 6f 6d 2f 71 2f 33 39 31 39 37 flow.com/q/39197 0000020: 37 30 33 2f 35 31 32 38 34 36 34 00 00 00 00 00 703/5128464.....
if wanted generate padding in shell, see e.g. here (under edit3).
good luck!
Comments
Post a Comment