java pkcs12,如何使用来自Java PKCS#12密钥库的证书加密和解密文件?

Can anyone explain how to encrypt and decrypt files using certificates stored in a java 'PKCS#12` keystore?

解决方案

As mention Eugene Mayevski, your question is wrong and cannot be answered in its original form. But i'll try to clarify it for you a bit. PKCS#12 - cryptographic format for storing cerificates and private keys. When you encrypt or decrypt data, you use cipher implementation and content of PKCS#12 container.

Java has build-in support for work with PKCS#12 keystores, work with this containers doesn't much differ than standart JKS keystore.

For example, code to load JKS keystore

KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());

store.load(is, password.toCharArray());

and code to load PKCS#12 keystore

KeyStore store = KeyStore.getInstance("PKCS12");

store.load(is, password.toCharArray());

After that you have unlimited accsess to keystore content. You can get certificates and keys, stored in keystore, without that strange actions with import/export in Firefox.

Key key = store.getKey("alias_for_key", password.toCharArray());

Next thing, when you have keys and certificates, is encryption. For encryption. you need instance of Cipher class.

Cipher c = Cipher.getInstance(key.getAlgorithm());

c.init(Cipher.ENCRYPT_MODE, key);

Cipher ready to encrypt. If encryption data is relativily small, you can use update() method, other way is to create CipherOutputStream.

To decrypt, simply init cipher with different mode and, depends of encryption algorithm, key. For symmetric algorithm key will the same, for asymmetric algorithm for encryption uses public key, and for decryption private key.

In this article you can learn more about cryptography.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值