我想用X.509證書和繼承的公鑰加密我的後期有效載荷。到目前爲止,我有這個Java代碼來執行加密使用X.509公共證書加密和解密
private String encrypt(String str) throws Exception {
ClassPathResource classPathResource = new ClassPathResource("testcert1.crt");
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate)certificateFactory.generateCertificate(classPathResource.getInputStream());
PublicKey pk = certificate.getPublicKey();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, pk);
return Base64.encodeBase64String(cipher.doFinal(str.getBytes()));
}
它返回base64編碼的字符串。從端點我總是得到結果,證書是無效的。
所以我想驗證我的加密字符串在控制檯上使用openssl
命令,但沒有這樣做。
我可以讀出與證書:openssl x509 -in testcert1.crt -text -noout
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 0 (0x0)
Signature Algorithm: md5WithRSAEncryption
Issuer: C=xxx, ST=xxx, L=xxx, O=xxx, OU=xxx, CN=xxx
Validity
Not Before: Jul 24 11:40:39 2013 GMT
Not After : Jul 24 11:40:39 2015 GMT
Subject: C=xxx, ST=xxx, L=xxx, O=xxx, OU=xxx, CN=xxx
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (4096 bit)
Modulus (4096 bit):
....
Exponent: 65537 (0x10001)
但我想不通的命令行,如果加密/使用該證書
'md5WithRSAEncryption'和4096位的密鑰是一個真正的* *不匹配。也許你應該考慮'sha256WithRSAEncryption'。 MD5的安全性還不到64位,而4096位的模數超過了128位的安全性。 MD5被破壞,並且不能提供隨時間的碰撞阻力。 – jww
好的,我會考慮這一點。我不是那個創造那個鑰匙的人,而是那個從第三方那裏得到鑰匙的消費者。 – MatthiasLaug
爲什麼?爲什麼不像其他人一樣使用HTTPS? – EJP