Mat*_*aug 5 java encryption shell ssl openssl
我想使用 X.509 证书和继承的公钥来加密我的帖子有效负载。到目前为止我有这个java代码来执行加密
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()));
}
Run Code Online (Sandbox Code Playgroud)
它返回 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)
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚使用该证书加密/解密文本文件的命令行
Guy*_*let -4
由于您使用的是非对称加密,如果您使用证书的公钥进行加密,则只能使用相应的私钥进行解密。确保您拥有该密钥并使用它进行解密。
归档时间: |
|
查看次数: |
16262 次 |
最近记录: |