4

Suppose that I want to encrypt a text with the TripleDES algorithm in CBC mode, I have two choices.

1- An online tool, Like this

2- OpenSSL tool.

In the Online tool, I enter the Text, the KEY, and the IV.

In the OpenSSL, I enter the below command :

OpenSSL> des-ede3-cbc -in MyText.txt -K <KEY> -iv <IV>


but what happens when I use the below command:

OpenSSL> des-ede3-cbc -in Mytext.txt

First question: I want to know, is the KEY made up of the password that I entered in the two next lines? Based on what algorithm will it create the key?

If someone has only a password, can they decrypt my encrypted text?

Second question: What is Salt parameter!? what is the difference between an encryption command that uses -nosalt and one that doesn't?

Third question: When using -base64, is the output in base64 form? When I don't use this parameter, what is the format of the output?


Update: Forth question: what do we need to decrypt an encrypted text? only password? both of IV and KEY? or all of them?

Fifth question:

I enter "1" as the password in openssl commandline. and then you this online tool and create MD5 hash of "1". As you see in the below picture, the first two bye of 3DES KEY is equal to MD5 hashing of the password that I entered. I want to know what is the last byte in the key? how it create?

And If someone have only Password,how can he decrypt my Encrypted text?

enter image description here

3
  • 1
    Abraham, stop editing your question to remove sub-questions. You're making a mess of this. I rolled back your Question to make the sub-question order consistent, and I rolled back your edit to @mtak's answer to make it consistent as well.
    – Spiff
    Commented Jun 21, 2014 at 6:35
  • @Spiff why?!! some parts of question and answer are excess and useless! for example, the fifth question is almost the same first question with more detail. the second question is not a good question! and so on ... why did you do that?! :/ Commented Jun 21, 2014 at 6:44
  • 1
    Abraham, because this site doesn't work well when you do that. It's really intended for one question per Question-posting. @mtak had already answered your previous subquestions 1, 2, and 3, and then when you took away subquestion 1, his subanswer 1 didn't line up with the new subquestion 1 (which was previously subquestion 2).
    – Spiff
    Commented Jun 21, 2014 at 6:59

1 Answer 1

3
  1. The Key and IV are derived from the password you specify, using an OpenSSL-specific algorithm that the OpenSSL team is not proud of. They keep it around for backward compatibility's sake, but they recommend that you use better password-based key derivation functions, such as PKCS's PBKDF2.

OpenSSL's bespoke key derivation algorithm is in the function EVP_BytesToKey(3).

Key:

-K key the actual key to use: this must be represented as a string comprised only of hex digits.

IV:

In cryptography, an initialization vector (IV) or starting variable (SV)[1] is a fixed-size input to a cryptographic primitive that is typically required to be random or pseudorandom.

So the IV is additional input used to encrypt the file. It is not the key (I guess it's just terminology).

2 A salt is an additional (prefix) to the key you specify. (see Wikipedia. It makes it impossible to use rainbow tables or precalculated hash tables on your key. The salt is usually stored unencrypted.

3 The output will be binary and will quite likely contain non-printable characters. Your terminal emulator will try to render those byte values as printable characters in its default character encoding and typeface, but they will probably look like "garbage text" and not be safe for copy/paste, FTP or email.

4 To decrypt an encrypted text, you need the Key and IV. If you don't have one or both of those, and the ones you're missing were derived from the Password, then if you have the Password, you can re-derive they missing Key and/or IV from the Password. You don't need the Salt, because you already have it; it's pre-pended to the beginning of the encrypted text. The Salt isn't really a secret, it's just a way to foil pre-computed hash tables and rainbow tables.

5 As defined in EVP_BytesToKey(3), if you use a password of "1" and --nosalt, the first 16 Bytes of your Key will be:

md5( D_0 || password || salt)

(note that in this context, || means concatenation, not logical or)

which is equivalent to

md5 ( `null` || "1" || `null`)

which is equivalent to

md5("1")

Which turns out to be

0xc4ca4238a0b923820dcc509a6f75849b

This value is what the man page calls D_1.

The remaining needed bytes of the Key and IV are generated like this:

md5( D_1 || password || salt)

which is equivalent to

md5( 0xC4CA4238A0B923820DCC509A6F75849B || "1" || `null` )

which is equivalent to

md5( 0xC4CA4238A0B923820DCC509A6F75849B31 )

(note that the ASCII "1" becomes 0x31 concatenated at the end of the D_1 value)

which turns out to be:

0x7976c7161415c830816dd4068a1d9a52

which is what that man page calls D_2.

The Key only needs 8 more bytes than D_1 already proved, so it takes the first 8 bytes of D_2 and becomes:

Key: C4CA4238A0B923820DCC509A6F75849B7976c7161415c830

The IV only needs 8 bytes, and since there are 8 unused bytes from D_2, they become the IV:

IV: 816dd4068a1d9a52

Here's a command-line to generate D_1, the first 16 bytes of the Key (given your example of password "1" and --nosalt):

echo -n "1" | openssl md5

Here's a command-line to generate D_2, the remaining 8 bytes of the Key, plus all 8 bytes of the IV (again, given your example inputs):

echo -n "$(echo -n "1" | openssl md5 -binary)1" | md5

This works by taking the output of D_1 (making sure to keep it in binary rather than translating it into ASCII-encoded hex digits), appending "1" (0x31) to it, and taking the md5 of that.

6
  • 1
    but I think the password is not the key! because when I use -p parameter in my command, to show IV and KEY in output, the key value is different from the password that I entered in previous line. [in this command : OpenSSL> des-ede3-cbc -in Mytext.txt] Commented Jun 17, 2014 at 8:21
  • if the format of output is binary, why I see some unreadable characters? (heart,some smileys and so on)? Commented Jun 17, 2014 at 8:24
  • 1
    It's because your terminal can't interpret those characters (it's binary, not text) and tries to display them using your default character set.
    – mtak
    Commented Jun 17, 2014 at 8:32
  • @mtak Your answers (at least to original sub-questions 2 and 3) were great. Please forgive me for jumping into your Answer rather than writing my own; I wanted to make sure you got the rep for your Answer, rather than competing with you and possible stealing the credit.
    – Spiff
    Commented Jun 21, 2014 at 7:03
  • 1
    dear @Spiff and dear mtak , thank you both,. very much :) Commented Jun 21, 2014 at 7:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .