# Combining multiple cryptographic keys to generate an encryption key

We have a system that has several SecretKeys (for example, one for a user, and one for us). We want to encrypt data in a way that requires all of those keys to be available in order to decrypt.

I think that the correct way to do this is to use a key derivation function of some sort to merge the source SecretKeys together, and use the result a the encryption key.

Given that all of the source keys are cryptographically generated SecretKeys using the following (apologies in advance if this isn't the right :

KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(256);
return generator.generateKey();


is it safe to just use a cryptographic hash of the source keys, or is that potentially introducing a vulnerability somehow? Like this:

SecretKey secretKey1 = ...
SecretKey secretKey2 = ...
SecretKey secretKey3 = ...

MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(secretKey1.getEncoded());
md.update(secretKey2.getEncoded());
byte[] digest = md.digest(secretKey3.getEncoded());

SecretKey mergedSecretKey = new SecretKeySpec(digest, "AES");


The database we are working with doesn't support native encryption, and we are only encrypting a very small number of fields.

Related (but no helpful responses): Using multiple cryptographic keys for encryption?

• en.wikipedia.org/wiki/Secret_sharing – Luis Casillas May 4 '18 at 0:37
• Good background. I'm still uncertain about whether using SHA is a cryptographically sound way of driving a symmetric key from two or three other keys... – Kevin Day May 4 '18 at 3:54
• I agree with @LuisCasillas, you want to look into Secret Sharing. I would suggest looking into Shamir's scheme – Haris Nadeem May 4 '18 at 5:11

I assumed your SecretKeys are symmetric shared secret (uniform full entropy) keys. (I don't know why you would want to combine shared secret keys to make a shared secret key and why the process that made the input keys can't be used.)