我正在使用DKIM for JavaMail使用 DKIM签署传出邮件。
我的私人 DKIM 密钥生成opendkim-genkey -s default -d example.com
如下:
-----BEGIN RSA PRIVATE KEY-----
ABCCXQ...[long string]...SdQaZw9
-----END RSA PRIVATE KEY-----
JavaMail 库的 DKIM 需要 DER 格式的私有 DKIM 密钥,如其自述文件中所述:
DKIM for JavaMail 需要 DER 格式的私钥,您可以使用 openssl 转换 PEM 密钥:
openssl pkcs8 -topk8 -nocrypt -in private.key.pem -out private.key.der -outform der
我正在寻找一种方法来避免使用 openssl 将我的密钥转换为 DER 格式。相反,我想直接在 Java 中进行转换。
我尝试了不同的建议(1、2、3),但到目前为止没有任何效果。
DKIM for Java 像这样处理 DER 文件:
File privKeyFile = new File(privkeyFilename);
// read private key DER file
DataInputStream dis = new DataInputStream(new FileInputStream(privKeyFile));
byte[] privKeyBytes = new byte[(int) privKeyFile.length()];
dis.read(privKeyBytes);
dis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
// decode private key
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privKeyBytes);
RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec);
所以最后我需要的是RSAPrivateKey
.
如何RSAPrivateKey
从我的 RSA 私钥轻松生成 DKIM for JavaMail 所需的内容?