我正在使用DKIM for JavaMail与DKIM签署外发邮件
我的私人DKIM密钥是使用opendkim-genkey -s default -d example.com
生成的,如下所示:
-----BEGIN RSA PRIVATE KEY-----
ABCCXQ...[long string]...SdQaZw9
-----END RSA PRIVATE KEY-----
DKIM for JavaMail库需要DER格式的私有DKIM密钥,如其自述文件中所述:
DKIM for JavaMail需要DER格式的私钥,你可以 转换PEM密钥 用openssl:
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
。
如何从我的RSA私钥轻松生成DKIM for JavaMail所需的RSAPrivateKey
?
答案 0 :(得分:3)
您的参考3(仅)是正确的;因为它说你的问题不只是转换 PEM到DER(正如@Jim所说的基本上只是base64到二进制)但转换 PEM包含openssl“传统”或“遗留”或“PKCS#1”格式的密钥数据 DER包含PKCS#8(特别是PKCS#8清除/未加密)格式密钥数据。
Alistair的回答指出http://juliusdavies.ca/commons-ssl/pkcs8.html 看起来可能有可能,但我没有详细检查。自PKCS#8起 RSA的clear(PrivateKeyInfo)只是围绕PKCS#1的简单ASN.1包装器, 以下(有点)快速和(非常)脏代码提供了最小的解决方案。 改变输入读取逻辑(和错误处理)以尝试并替换可用的base64解码器。
BufferedReader br = new BufferedReader (new FileReader (oldpem_file));
StringBuilder b64 = null;
String line;
while( (line = br.readLine()) != null )
if( line.equals("-----BEGIN RSA PRIVATE KEY-----") )
b64 = new StringBuilder ();
else if( line.equals("-----END RSA PRIVATE KEY-----" ) )
break;
else if( b64 != null ) b64.append(line);
br.close();
if( b64 == null || line == null )
throw new Exception ("didn't find RSA PRIVATE KEY block in input");
// b64 now contains the base64 "body" of the PEM-PKCS#1 file
byte[] oldder = Base64.decode (b64.toString().toCharArray());
// concatenate the mostly-fixed prefix plus the PKCS#1 data
final byte[] prefix = {0x30,(byte)0x82,0,0, 2,1,0, // SEQUENCE(lenTBD) and version INTEGER
0x30,0x0d, 6,9,0x2a,(byte)0x86,0x48,(byte)0x86,(byte)0xf7,0x0d,1,1,1, 5,0, // AlgID for rsaEncryption,NULL
4,(byte)0x82,0,0 }; // OCTETSTRING(lenTBD)
byte[] newder = new byte [prefix.length + oldder.length];
System.arraycopy (prefix,0, newder,0, prefix.length);
System.arraycopy (oldder,0, newder,prefix.length, oldder.length);
// and patch the (variable) lengths to be correct
int len = oldder.length, loc = prefix.length-2;
newder[loc] = (byte)(len>>8); newder[loc+1] = (byte)len;
len = newder.length-4; loc = 2;
newder[loc] = (byte)(len>>8); newder[loc+1] = (byte)len;
FileOutputStream fo = new FileOutputStream (newder_file);
fo.write (newder); fo.close();
System.out.println ("converted length " + newder.length);
除此之外:我假设您发布的数据中的ABCC已被编辑。任何有效和合理的 PKCS#1(清除)RSA密钥必须以字节0x30 0x82 x开头,其中x为2到9; 当转换为base64时,必须从MIIC开始到MIIJ。
答案 1 :(得分:0)
PEM格式只是Base64中编码的DER字节。您只需将PEM文件作为文本文件读取,并只取“----- BEGIN RSA PRIVATE KEY -----”和“----- END RSA PRIVATE KEY ----”之间的行。 - ”
PEM文件中允许注释,因此您可能需要从头开始跳过其他行,直到到达“----- BEGIN RSA私钥-----”。
以下是Ruby中的一些代码,只需通过Base64对文本进行解码即可将PEM转换为DER。您可以将其转换为Java:
require 'base64'
# Read the PEM as text
pem = File.read('key.pem')
# Break into individual lines
all_lines = pem.split("\n")
# Discard the first and last lines
b64_lines = all_lines[1..-2]
# Write the output file
File.open('key.der', 'w') do |f|
# For each line
b64_lines.each do |line|
# Write the Base64-decoded bytes
f.write Base64.decode64(line)
end
end
这是openssl显示我刚刚创建的RSA密钥:
$ openssl rsa -text -in key.pem
Private-Key: (2048 bit)
modulus:
00:aa:8d:15:05:db:a3:fa:bb:dc:8d:4f:d5:53:56:
d6:78:7f:41:6d:05:b9:92:a4:7c:28:b1:11:50:9e:
77:67:04:e9:77:22:62:db:1f:43:bc:f2:5e:fc:f3:
42:35:a2:01:9c:9c:c1:90:b7:2b:1c:c8:51:f2:27:
... (omitted)
这是openssl显示Ruby代码写的DER文件:
$ openssl rsa -text -inform DER -in key.der
Private-Key: (2048 bit)
modulus:
00:aa:8d:15:05:db:a3:fa:bb:dc:8d:4f:d5:53:56:
d6:78:7f:41:6d:05:b9:92:a4:7c:28:b1:11:50:9e:
77:67:04:e9:77:22:62:db:1f:43:bc:f2:5e:fc:f3:
42:35:a2:01:9c:9c:c1:90:b7:2b:1c:c8:51:f2:27:
...
这是PEM(中间部分省略):
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAqo0VBduj+rvcjU/VU1bWeH9BbQW5kqR8KLERUJ53ZwTpdyJi
2x9DvPJe/PNCNaIBnJzBkLcrHMhR8ici2aOpuPZi/EDxjRcNPF1Bj+ULvYNLMEDb
2ng9HhyyQtZMllYpkuewONSsTfrsOKEm3NXFfjAclVAGUHp6JpBCTfd32VbHGmJl
...
S4SwxwUV39H2Zq+X1pSIEssSUduyGvF7jJadHe5OiacD5112bkF+Tx/YThMcMebg
8IrO8DyBIBfgAcQPq2GSY99ImacZ++OLXcXdC9NYZ3U9T2SCJdf5
-----END RSA PRIVATE KEY-----