Decrypting the CipherValue in a PSKC file

0

This is the opposite of what I was doing in this question - https://crypto.stackexchange.com/questions/21021/encryption-and-hmac-in-a-pskc-file

The spec is there at https://tools.ietf.org/rfc/rfc6030.txt

The Data/seed is encrypted with AES-128-CBC & and a Key and an IV. The IV is prepended to the encrypted data and the whole thing is base-64 encode and stored in the field CipherValue.

I have (this is from a sample pskc file I have, not from the RFC).

CipherValue/EncryptedText = v5dgOiUq1Hgja0g1SI9VpcHu81DLdcDqT8C3+k8wlQtk5KL1KErwojxLPSNO8lZt
Encryption Key = E9C1E401122EE194D620465A775D9C97
Algo = AES-128-CBC

I need to get the Decrypted data from this

This is what I am doing

I have CipherValue stored in a file named cv.

openssl base64 -d -A < cv > cv.bin

I get a 48 byte cv.bin

I spilt into 2 parts

iv.bin = 16 bytes
data.bin = 32 bytes

I convert iv.bin into hex

xxd -p < iv.bin
bf97603a252ad478236b4835488f55a5

So my

iv = bf97603a252ad478236b4835488f55a5

I convert the key into hex

I have a file call k.b64 which contains E9C1E401122EE194D620465A775D9C97

openssl base64 -d -A < k.b64 > k.bin
xxd -p < k.bin
13d0b5138d35d76d84135f780fadb4e3ae40efbe43f42f7b

key = 13d0b5138d35d76d84135f780fadb4e3ae40efbe43f42f7b

Now I decrypt

openssl enc -aes-128-cbc -d  -K 13d0b5138d35d76d84135f780fadb4e3ae40efbe43f42f7b -iv bf97603a252ad478236b4835488f55a5  < data.bin

I get the following error

▓\=Bt═┐|)éïh│■ìcbad decrypt
62008:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:.
\crypto\evp\evp_enc.c:539:

What am I doing wrong here?

user93353

Posted 2014-12-30T07:20:30.547

Reputation: 354

Answers

1

(Answer from comments reorganized to logical order.)

The encryption key value in this example E9C1E401122EE194D620465A775D9C97 is 32 chars that are valid hex digits, which are inherently also valid base64 chars. Normally one should know the encoding of data from the process that created (or where applicable sent) it.

This value is very unlikely to be base64 because a base64 encoding of more than a few bytes, unless deliberately crafted, will almost always contain some chars that are not hex digits. And it is impossible to be base64 because 32 chars of base64 not using base64 padding (=) encodes a 24-byte value, but we know the value is an AES-128 key which is 16 bytes.

Thus this key is hex encoded, and giving it to openssl enc -K which expects hex decrypts correctly.

dave_thompson_085

Posted 2014-12-30T07:20:30.547

Reputation: 1 962