简体   繁体   English

java-从字节数组获取密钥

[英]java - get key from byte array

I have a java program that encrypts file content with a random-generated key. 我有一个Java程序,使用随机生成的密钥对文件内容进行加密。 That key is encrpyted with RSA and saved into a text file. 该密钥随RSA一起加密并保存到文本文件中。

Now, I have a java program that given the file and the keystore where the RSA key is stored, needs to first decrypt the encryped key and then with the key to decrypt the file. 现在,我有一个Java程序,给出了文件和存储RSA密钥的密钥库,首先需要解密加密的密钥,然后再使用密钥解密文件。

Here's what I have so far: 这是我到目前为止的内容:

// 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();

In short, I get the encrypted key from the key text file and decrypt it, now I have the decrypted key as a byte array, how would I make it a Key variable again? 简而言之,我从密钥文本文件中获取了加密密钥并对其进行解密,现在我将解密密钥作为字节数组使用了,如何再次将其设为Key变量?

I've generated the key this way: 我以这种方式生成了密钥:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
Key secKey = keyGen.generateKey();
cipher.init(Cipher.ENCRYPT_MODE, secKey);

And encrypted it this way: 并以这种方式加密:

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();

I haven't tried it but from clicking through the API SecretKeySpec could be what you are looking for. 我还没有尝试过,但是通过API SecretKeySpec单击可以找到您想要的。

SecretKeySpec(byte[] key, String algorithm)

It can be used to construct a SecretKey from a byte array, without having to go through a (provider-based) SecretKeyFactory. 它可以用于从字节数组构造SecretKey,而不必通过(基于提供程序的)SecretKeyFactory。

This class is only useful for raw secret keys that can be represented as a byte array and have no key parameters associated with them, eg, DES or Triple DES keys. 此类仅对可以表示为字节数组且没有与之关联的密钥参数(例如DES或Triple DES密钥)的原始秘密密钥有用。

If I get it right, this should work.. 如果我做对了,这应该可以工作。

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!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM