证书加密解密

开始以为可以对文件进行加密,使用后发现不支持文件加密,其中证书类型是浏览器支持的XX.p12证书,现在正在研究Itext使用证书签名加密pdf文件,找了很多资料,这方面的资料实在是太少了。

/**
* 证书加密解密(不能对文件加密,这个只能对一小段字符加密解密)
* @param certPath 证书路径
* @param password 证书密码
*/
private static void encrypAndDecryption(String certPath,String certPassWord,String encryptData){
//此类表示密钥和证书的存储设施
KeyStore keyStore;
String alias = "";
//String testEncrypt = "certificate encrypt decryption";
System.out.println("加密前: " + encryptData);
try {
FileInputStream is = new FileInputStream(certPath);
//得到KeyStore实例
keyStore = KeyStore.getInstance("PKCS12");
//从指定的输入流中加载此 KeyStore。
keyStore.load(is, certPassWord.toCharArray());
is.close();
//获取keyStore别名
alias = (String)keyStore.aliases().nextElement();
Certificate cert = keyStore.getCertificate(alias);
//根据给定别名获取相关的私钥
PrivateKey priKey = (PrivateKey)keyStore.getKey(alias, certPassWord.toCharArray());
//获取证书的公钥
PublicKey pubKey = cert.getPublicKey();
//获取Cipher的实例 getInstance(算法/模式/填充)或getInstance("算法")
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//公钥初始化Cipher 公钥加密
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
//加密
byte [] encodeEncryp = cipher.doFinal(encryptData.getBytes());
System.out.println("加密后: " + new String(encodeEncryp));
//encodeEncryp = cipher.doFinal(getFileContentToByte(encrypFile));
//私钥初始化Cipher 私钥解密
cipher.init(Cipher.DECRYPT_MODE, priKey);
//解密
byte [] encodeDecryption = cipher.doFinal(encodeEncryp);

String content = new String(encodeDecryption);
System.out.println("解密后: " + content + "length: " + content.length());
} catch (Exception e) {
e.printStackTrace();
}
}

下面有个测试用的certificate.
在Python中,使用证书来进行数据加密和解密通常涉及到使用安全套接字层(SSL)或其更高级的形式,称为传输层安全(TLS)。这两个协议都是基于公开的加密标准,比如RSA、AES等,并使用X.509证书来验证身份。 **加密和解密的基本步骤**: 1. **导入库**: 首先,你需要导入`ssl`库和相关的加密模块,如`cryptography`。 ```python from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import rsa, padding ``` 2. **生成密钥对**: 创建一个RSA公钥和私钥对。 ```python private_key = rsa.generate_private_key( public_exponent=65537, key_size=2048 ) public_key = private_key.public_key() ``` 3. **证书操作**: 对于证书,你可以保存私钥到PEM格式,公开部分可以作为公共证书加载。 ```python private_pem = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ).decode('utf-8') with open("private.pem", "w") as f: f.write(private_pem) public_cert = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ).decode('utf-8') ``` 4. **加密和解密**: 使用`public_key.encrypt()`进行加密,`private_key.decrypt()`进行解密。 ```python message = b"Hello, World!" cipher_text = public_key.encrypt( message, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) # 解密需要私钥 original_message = private_key.decrypt(cipher_text) ``` **
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值