组合多个加密密钥以生成加密密钥

时间:2018-05-03 18:00:40

标签: java encryption cryptography

我们有一个拥有多个SecretKeys的系统(例如,一个用于用户,一个用于我们)。我们希望以一种需要所有这些密钥才能解密的方式加密数据。

我认为正确的方法是使用某种密钥派生函数将源SecretKeys合并在一起,并将结果用作加密密钥。

鉴于所有源密钥都是使用以下代码加密生成的SecretKeys:

    import sys, os, time,re,json
    from subprocess import Popen, PIPE, STDOUT

    cmd = 'defaults read com.apple.screencapture location' # default env variable that stores the screenshot dir in MacOS 
    #opens a stream and retrieves output
    pipe = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) 
    # reads the output from the stdout and then strips the '\n' at the end to form a valid path
    path = pipe.stdout.read().rstrip() 

使用源密钥的加密哈希是否安全,或者是否可能以某种方式引入漏洞?像这样:

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

然后在调用Cipher.init()时使用生成的mergedSecretKey。

这种方法有任何问题或风险吗?

1 个答案:

答案 0 :(得分:1)

使用封装加密。最好不要组合哈希或进行双重加密,尤其是后者可能导致中间相遇攻击。