gpt4 book ai didi

java - 使用 X.509 公共(public)证书进行加密和解密

转载 作者:行者123 更新时间:2023-11-29 09:32:19 25 4
gpt4 key购买 nike

我想用 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()));
}

返回 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)

但我无法弄清楚使用该证书加密/解密文本文件的命令行

最佳答案

您可以通过以下命令使用 openssl 验证您的加密字符串:

echo -n 'string to encrypt' | openssl rsautl -encrypt -certin -inkey testcert1.crt | base64

关于java - 使用 X.509 公共(public)证书进行加密和解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21786821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com