java 证书的加密与解密

证书,加密的相关知识可以参考阮一峰的密码学笔记数字签名是什么 

 

代码中使用的demo.p12使用jdk自带的keytool证书生成,私钥密码为123456

    

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.util.Enumeration;

import javax.crypto.Cipher;

public class ReadP12Cert {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		final String KEYSTORE_FILE = "demo.p12";
		final String KEYSTORE_PASSWORD = "123456";
		try {
			KeyStore ks = KeyStore.getInstance("PKCS12");
			FileInputStream fis = new FileInputStream(KEYSTORE_FILE);
			char[] nPassword = null;
			if ((KEYSTORE_PASSWORD == null)|| KEYSTORE_PASSWORD.trim().equals("")) {
				nPassword = null;
			} else {
				nPassword = KEYSTORE_PASSWORD.toCharArray();
			}
			ks.load(fis, nPassword);
			fis.close();
			System.out.println("keystore type = " + ks.getType());
			Enumeration enuml = ks.aliases();
			String keyAlias = null;
			if (enuml.hasMoreElements()) {
				keyAlias = (String) enuml.nextElement();
				System.out.println("alias=[" + keyAlias + "]");
			}
			System.out.println("is key entry = " + ks.isKeyEntry(keyAlias));
			PrivateKey prikey = (PrivateKey) ks.getKey(keyAlias, nPassword);
			Certificate cert = ks.getCertificate(keyAlias);
			PublicKey pubkey = cert.getPublicKey();

			byte[] msg = "This is a demo!".getBytes("UTF8"); // 待加解密的消息
			Cipher c1 = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // 定义算法:RSA
			c1.init(Cipher.ENCRYPT_MODE, pubkey);
			byte[] msg1 = c1.doFinal(msg); // 加密后的数据
			System.out.println("加密后的数据----"+new String(msg1, "UTF8"));
			Cipher c2 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			c2.init(Cipher.DECRYPT_MODE, prikey);
			byte[] msg2 = c2.doFinal(msg1); // 解密后的数据
			// 打印解密字符串
			System.out.println("解密后的数据----"+new String(msg2, "UTF8")); // 将解密数据转为字符串
			System.out.println(prikey.toString());
			System.out.println(pubkey.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java Cryptography Extension (JCE)提供的API来实现加解密。首先需要获取加密证书,然后使用证书中的公钥进行加密,使用私钥进行解密。以下是一个简单的示例代码: ``` import java.io.FileInputStream; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import java.security.cert.Certificate; import javax.crypto.Cipher; public class EncryptionExample { public static void main(String[] args) throws Exception { // Load the keystore containing the certificate and private key KeyStore ks = KeyStore.getInstance("JKS"); FileInputStream fis = new FileInputStream("keystore.jks"); ks.load(fis, "password".toCharArray()); // Get the certificate and private key String alias = "myalias"; Certificate cert = ks.getCertificate(alias); PrivateKey privateKey = (PrivateKey) ks.getKey(alias, "password".toCharArray()); // Get the public key from the certificate PublicKey publicKey = cert.getPublicKey(); // Encrypt the message using the public key Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encryptedMessage = cipher.doFinal("Hello, world!".getBytes()); // Decrypt the message using the private key cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decryptedMessage = cipher.doFinal(encryptedMessage); System.out.println(new String(decryptedMessage)); } } ``` 注意,这个示例代码中使用的是RSA算法,如果你需要使用其它算法,需要相应地修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值