Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java-从字节数组获取密钥_Java_Public Key_Cryptoapi - Fatal编程技术网

java-从字节数组获取密钥

java-从字节数组获取密钥,java,public-key,cryptoapi,Java,Public Key,Cryptoapi,我有一个java程序,它用随机生成的密钥加密文件内容。 该密钥由RSA加密并保存到文本文件中 现在,我有一个java程序,给定文件和存储RSA密钥的密钥库,需要首先解密加密密钥,然后使用密钥解密文件 以下是我目前掌握的情况: // Fetch the other public key and decrypt the file encryption key java.security.cert.Certificate cert2 = keystore.getCertificate("keyForS

我有一个java程序,它用随机生成的密钥加密文件内容。 该密钥由RSA加密并保存到文本文件中

现在,我有一个java程序,给定文件和存储RSA密钥的密钥库,需要首先解密加密密钥,然后使用密钥解密文件

以下是我目前掌握的情况:

// Fetch the other public key and decrypt the file encryption key
java.security.cert.Certificate cert2 = keystore.getCertificate("keyForSeckeyDecrypt");
Key secKeyPublicKey = cert2.getPublicKey();
Cipher cipher = Cipher.getInstance(secKeyPublicKey.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, secKeyPublicKey);
keyFileFis = new FileInputStream(keyFile);
byte[] encryptedKey = new byte[128];
keyFileFis.read(encryptedKey);
byte[] realFileKey = cipher.doFinal(encryptedKey, 0, encryptedKey.length);
Key realKey = //  THE PROBLEM!!!;
keyFileFis.close();
简言之,我从密钥文本文件中获取加密密钥并对其进行解密,现在我将解密的密钥作为字节数组,如何使其再次成为密钥变量

我以这种方式生成了密钥:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
Key secKey = keyGen.generateKey();
cipher.init(Cipher.ENCRYPT_MODE, secKey);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
PrivateKey privateKey = kp.getPrivate();
Cipher keyCipher = Cipher.getInstance("RSA");
keyCipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encryptedKey = keyCipher.doFinal(secKey.getEncoded());
FileOutputStream keyStream = new FileOutputStream("key.txt");
keyStream.write(encryptedKey);
keyStream.close();
并以这种方式对其进行加密:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
Key secKey = keyGen.generateKey();
cipher.init(Cipher.ENCRYPT_MODE, secKey);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
PrivateKey privateKey = kp.getPrivate();
Cipher keyCipher = Cipher.getInstance("RSA");
keyCipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encryptedKey = keyCipher.doFinal(secKey.getEncoded());
FileOutputStream keyStream = new FileOutputStream("key.txt");
keyStream.write(encryptedKey);
keyStream.close();

我还没有尝试过,但是通过点击API可以找到你想要的

SecretKeySpec(byte[] key, String algorithm)
它可以用于从字节数组构造SecretKey,而无需经过(基于提供程序的)SecretKey工厂

此类仅适用于可以表示为字节数组且没有与之关联的密钥参数(例如DES或三重DES密钥)的原始密钥


如果我没弄错的话,这应该行得通

Key privateKey = keyStore.getKey("youralias", "password".toCharArray());
PublicKey publicKey = keyStore.getCertificate("youralias").getPublicKey();

KeyGenerator keyGen = KeyGenerator.getInstance("AES");
Key secKey = keyGen.generateKey();

Cipher keyCipher = Cipher.getInstance("RSA");
keyCipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encryptedKey = keyCipher.doFinal(secKey.getEncoded());

// Write & Read to/from file!

Cipher decryptCipher = Cipher.getInstance("RSA");
decryptCipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] decryptedKey = decryptCipher.doFinal(encryptedKey);

boolean equals = Arrays.equals(secKey.getEncoded(), new SecretKeySpec(decryptedKey, "AES").getEncoded());
System.out.println(equals?"Successfull!":"Failed!");

我已经尝试过了,但是当我尝试使用这个SecretKeySpec初始化密码时,我得到了一个异常“没有安装的提供程序支持这个密钥:javax.crypto.spec.SecretKeySpec”@MichBoy,这是重新创建密钥的正确方法。问题可能是您的钥匙大小不对。AES密钥应该是16、24或32字节,而不是128字节。若要使用长度超过128字节的密钥,您需要正确安装“无限强度”权限策略文件。您可以分享如何生成存储在文件中的密钥吗?算法?长度?只是为了确定!您可以使用“新生成的”
RSA
私钥加密您的
aes
密钥,并将其保存到文件中。但您希望使用密钥库中的公钥对其进行解密!这对我来说毫无意义。您应该使用密钥库中的私钥来让它工作。@Akdeniz,除了密钥库中的解密之外,密钥I是RSA公钥,因此我可以拥有RSA私钥,这样我就可以解密我的AES密钥。使用RSA加密AES密钥是本练习的要求之一,如果您有更好的方案来设计它,我很乐意听到:)感谢您的努力,有几点:a)加密和解密分为两个不同的类(即不同的程序),因此secKey在加密程序中,而不是在解密程序中。b) 我仍然没有解决我的问题,因为我需要一个密钥变量来解密原始文件。顺便说一句,由于某种原因,在尝试执行decryptCipher.doFinal(encryptedKey)部分时,我得到了“Data mush start with a zero”异常,为什么?请确保您能够准确读取写入该文件的内容。顺便说一下,此代码只是一个概念证明,您可以使用
SecretKeySpec
重新创建
AES
键。如果你对你的上下文有问题,请问另一个问题,清楚地指向你的问题。