Encrypt in PHP and decrypt in Python or openssl command line -
a lot of data in database took on contains encrypted fields. method used encrypt data following php code:
<?php $text = "test 1234\ntest 2345\ntest 3456\ntest 4567"; $key = "0123456789abcdefghijklmnopqrstuv"; $enc = openssl_encrypt($text, "aes-256-cbc", $key); echo "raw: " . $text . "\n"; echo "key: " . $key . "\n"; echo "key (hex) " . bin2hex($key) . "\n"; echo $enc; echo "\n"; ?>
when run code following output including warning empty initialization vector (iv), have ignore because whole db data encrypted way (i know should not done way).
warning: openssl_encrypt(): using empty initialization vector (iv) potentially insecure , not recommended in /tmp/cp3_encdec/enc2.php on line 5 raw: test 1234 test 2345 test 3456 test 4567 key: 0123456789abcdefghijklmnopqrstuv key (hex) 303132333435363738396162636465666768696a6b6c6d6e6f70717273747576 upnxdo2k0gvy/+mw0yfr7utfsrndap8yyadxt352w3lpknoknmg+l3efkei0zeze
decrypt using php openssl_decrypt($encrypted, "aes-256-cbc", $key)
gives me full output. php manual not give lot of insight used encrypt in regards padding , iv when leaving values empty.
next tried decrypt on command line using openssl command:
echo "upnxdo2k0gvy/+mw0yfr7utfsrndap8yyadxt352w3lpknoknmg+l3efkei0zeze" | openssl aes-256-cbc -d -a -k 303132333435363738396162636465666768696a6b6c6d6e6f70717273747576 -iv 0
which works fine , returns initial input:
test 1234 test 2345 test 3456 test 4567
trying decrypt in python using following code results in wrong decryption:
import base64 crypto.cipher import aes pad = u'\0000' def decrypt(enc, key): decobj = aes.new(key, aes.mode_ecb) data = decobj.decrypt(base64.b64decode(enc)) data = data.rstrip(pad.encode()) print(str(data)) key = "0123456789abcdefghijklmnopqrstuv" decrypt("upnxdo2k0gvy/+mw0yfr7utfsrndap8yyadxt352w3lpknoknmg+l3efkei0zeze", key)
result, first 16 bytes readable not rest:
b'test 1234\ntest 2\x8b\xc7b|\xf9\xef\xa3\x1f\xd2\xcc\xd7#\xe7\x8b%\x8b\x981\x92\x87v4\xa8;h\xa9\xf8fw\x7frp'
modifying input contain more data break decryption using openssl command:
raw: [system] test:1234 [system] test:2345 [database] test:3456 [unknown] test:4567 key: 0123456789abcdefghijklmnopqrstuv key (hex) 303132333435363738396162636465666768696a6b6c6d6e6f70717273747576 9kwsggla1/g3f36kujj/ohnienidorzulwr8pxzhwjhul2xsdzlwln8jmptp9fcwgy42otq7rtm+/8ckpigfpwry/3nelvf8unedsvukrlc=
openssl command line:
echo "9kwsggla1/g3f36kujj/ohnienidorzulwr8pxzhwjhul2xsdzlwln8jmptp9fcwgy42otq7rtm+/8ckpigfpwry/3nelvf8unedsvukrlc=" | openssl aes-256-cbc -d -a -k 303132333435363738396162636465666768696a6b6c6d6e6f70717273747576 -iv 0 bad decrypt 15143:error:0606506d:digital envelope routines:evp_decryptfinal_ex:wrong final block length:/buildroot/library/caches/com.apple.xbs/sources/openssl098/openssl098-64/src/crypto/evp/evp_enc.c:323:
the same using python code above result in first 16 bytes readable, not rest:
b'[system] test:12\xc7\x91\xa6c\x11\xa3\xa4\x8cr\x12#\x84$\xf7\x0c\xd4ip!f6\xa8\xed0np\x1d\xc7\x174\xa5\xc5n\xe3\x00\x9f\x01\xa8\xc3\x18\xea\x158\xc0:\x9b\x9cx\xee\xf9x\xfc\x1a\xcf j\xca\xc5\xf4\xbf\x08\x16\x8f<'
again if using php openssl_decrypt works:
<?php $text = "9kwsggla1/g3f36kujj/ohnienidorzulwr8pxzhwjhul2xsdzlwln8jmptp9fcwgy42otq7rtm+/8ckpigfpwry/3nelvf8unedsvukrlc="; $key = "0123456789abcdefghijklmnopqrstuv"; $dec = openssl_decrypt($text, "aes-256-cbc", $key); echo $dec; echo "\n"; ?> [system] test:1234 [system] test:2345 [database] test:3456 [unknown] test:4567
someone has idea how php encrypts data, suppose padding problem not sure , open on topic.
i made modifications python code seems solve issue:
import base64 crypto.cipher import aes iv = 16 * '\x00' def decrypt(enc, key): decobj = aes.new(key, aes.mode_cbc, iv) data = decobj.decrypt(base64.b64decode(enc)) print(str(data.decode())) key = "0123456789abcdefghijklmnopqrstuv" decrypt("upnxdo2k0gvy/+mw0yfr7utfsrndap8yyadxt352w3lpknoknmg+l3efkei0zeze", key)
on command line did not found solution yet.
Comments
Post a Comment