开发者

Using keystore with javax.crypto for file encryption/decryption

I was advised to look here: http://exampledepot.com/egs/javax.crypto/DesFile.html for the source code of encryption/decryption using import javax.crypto. I have generated my key via keytool and now I don't know how to pass my generated keys into that application for encryption and decryption.

My situation is, that I have a XML file stored online (it stores configuration details) and before I parse it with a XML parser I have to decrypt it. First of all I should of course encrypt it, bef开发者_运维问答ore it goes online.

Question is: How to pass my generated keys into code visible in link in first row?

Thanks


How to load a KeyStore is documented in the JavaDoc of the KeyStore class:

import java.io.FileInputStream;
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
// get user password and file input stream
char[] password = getPassword();
try (FileInputStream fis = new FileInputStream("C:/mykeystore.jks")) {
    ks.load(fis, password);
}

Once you have loaded the key store you can load the key:

Key myKey = ks.getKey("mykeyalias", password);

The key alias is the one you have specified using keytool. Using the myKey you can initialize the Cipher instance or use for example a CipherOutputStream / CipherInputStream

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜