2
$\begingroup$

I'm new to cryptography and I'm trying implement a file encryption feature into a Python program but I'm running into a big issue...what is needed for someone else to decrypt the file?! Obviously, I can decrypt the file on my end. I'm not worried about that.

Basically, I need to encrypt a file and send it to another person who then needs to be able to decrypt the file using their system. I thought the other person would just need to know the form of encryption used (AES, RSA, etc) and the respective key to decrypt the file. But I guess that's not the case.

So, what is required to get this scenario to work? I know I'm not the first person to want to send an encrypted file to someone else and have them decrypt it.

This is what I need to know:

  • Can a file encrypted using Python be decrypted using, say...C# or another programming language?
  • Exactly what information about the encryption process is required for the person receiving the encrypted file to successfully decrypt the file?

Your help will be greatly appreciated. I don't know what I'd do without a community like this.

$\endgroup$

closed as too broad by Artjom B., e-sushi Oct 17 '15 at 2:39

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ The language doesn't matter. What you need to know: Format, Algorithms, keys. Format includes stuff like authentication tags and IVs / Nonces... $\endgroup$ – SEJPM Oct 14 '15 at 18:37
  • $\begingroup$ @SEJPM I didn't think the language mattered. I only mentioned that to point out that the system I will be using to encrypt the file is entirely different from the system the other person will be using to decrypt the file. I asked a similar question on here earlier, and although I was getting some good information from the responses. I felt like the true questions were not being answered. That could also be due to my limited knowledge on the subject. $\endgroup$ – CM-Dev Oct 14 '15 at 18:51
  • $\begingroup$ @SEJPM Could you please elaborate further in an answer? Maybe use an example pertaining to my scenario, please? I'm hoping to use AES-256 just so you are aware. $\endgroup$ – CM-Dev Oct 14 '15 at 18:53
  • $\begingroup$ Q1: Yes, but probably needs some small adjustments. Q2: This is very broad, because it depends on the cryptosystem that you're using. There are many different asymmetric cryptosystems out there that use different primitives. I answered a similar question on Stack Overflow for the symmetric case. $\endgroup$ – Artjom B. Oct 16 '15 at 18:39
3
$\begingroup$

As mentioned in the comments what the receiver needs to know is effectively:
Formats, Algorithms and Keys.

Now this may be a bit of an unsatisfying answer to most people, so I'll go into more detail.

Formats

The sender and the receiver have to agree on a format on how to actually read the data and how to interpret it. If I give anyone a chunk of Word document data without any format specification he will have a really hard time figuring out what field means what. So in your scenario you have to think of some specification on how to transmit your files, this may be as simple as the following, but must be done in order to ensure compatibility.

Example: RSA encrypted file

Offset        | Size | Type | Name |Description
0             | 4    | bytes| Magic| A constant magic value to identify this packet
4             | 4    | bytes| Ver  | A little-endian (LE) 32-bit integer denoting the format's version
68            | 64   | bytes| Cert | A SHA-512 hash of the certificate used to encrypt the key
72            | 4    | int  | KeyLe| A LE 4-byte integer holding the size of the ciphertext
76            | 8    | int  | Size | Size of the actual encrypted data
84            | 12   | byte | Nonce| A random nonce used for GCM
96            | KeyLe| int  | KEM  | An encrypted random number
96+KeyLe      | Size | byte | Data | The encrypted file
96+KeyLe+Size | 16   | byte | Tag  | The GCM tag on the data

So far for the format, now for the

Algorithms

Only knowing how to read in the data doesn't help you alot. You also need to know how to interpret it. This is where the algorithms come into play. For the above example you need to know that f.ex. Header Version 1 would be to use AES-256-GCM to encrypt the data using the nonce and outputting / verifying the appended tag along with authenticating the header as associated data (AD). Furthermore you'd have to know that this version requires you to use HKDF (salt=empty, no additional info) to derive the key from the RSA decryption of the Key value using the certificate with the hash matching the Cert entry. You'd also need to know that you select a key by randomly choosing a number smaller than the RSA modulus and by encrypting this number with RSA (=Key) and using the HKDF of the secret integer as the AES key.

Keys

The keys aren't all that interesting at this point. You'd need to somehow trust the authenticity of the public key (using certificates?). But that's basically it with key management with this example. Of course you also have to consider things like key revocation, key expiration and key erasure but those are different problems...


To finally answer your first question whether the programming language matters. It doesn't if both systems use the same Formats, Algorithms and the right keys, because you wouldn't be able to distinguish the results of a C# from a Python from a C++ implementation then.

$\endgroup$
  • $\begingroup$ This is awesome! I appreciate the thorough explanation. I haven't finished reading through everything quite yet because I'm running errands right now. But I will as soon as I can. $\endgroup$ – CM-Dev Oct 14 '15 at 22:38
  • $\begingroup$ Also, when you said "...you have to think of some specification on how to transmit your files..." it made me realize that I may have misunderstood what you're referring to. Is your response discussing encrypting the file transmission? The reason I bring this up is because I don't need to know how to encrypt the transmission. I'm actually already using HTTPS to send the file. What I'm interested in doing is encrypting the file itself, then send the encrypted file over HTTPS and then have the receiver be able to decrypt the file once they've received it. $\endgroup$ – CM-Dev Oct 14 '15 at 22:38
  • $\begingroup$ Did you think I was talking about encrypting the file transmission or were you still referring to file encryption? $\endgroup$ – CM-Dev Oct 14 '15 at 22:39
  • $\begingroup$ Is that the case or did I just misinterpret what you said? $\endgroup$ – CM-Dev Oct 15 '15 at 15:53
  • $\begingroup$ @CM-Dev please be patient and give me at least 24 hours to respond to comments :) The response is discussing the encryption of the actual file ("how to prepare the file for transmission?" = "how [=in what format] to transmit files"). $\endgroup$ – SEJPM Oct 15 '15 at 16:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.