我正在使用REST API监视Oracle Cloud Compute VM。
String privateKeyFilename = "/.oci/oci_api_key.pem";
PrivateKey privateKey = loadPrivateKey(privateKeyFilename);
RequestSigner signer = new RequestSigner(apiKey, privateKey);
loadPrivateKey(privateKeyFilename)方法如下。
private static PrivateKey loadPrivateKey(String privateKeyFilename) {
try (InputStream privateKeyStream = Files.newInputStream(Paths.get(SystemUtils.getUserHome().toString() + privateKeyFilename))) {
return PEM.readPrivateKey(privateKeyStream);
} catch (InvalidKeySpecException e) {
throw new RuntimeException("Invalid format for private key");
} catch (IOException e) {
throw new RuntimeException("Failed to load private key");
}
}
oci_api_key.pem文件(示例)如下所示。
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,93A2E22E154E2EBFE18D170E9E9D1772
xCpRUz2HCa9sd0inFN7BtFO/ZgQDlcieOriynSDGYBYXMO1JWpHtjbiEvt9FbdGk
INKzMQKeDbmG1PqW0Pzflla2IIpyZKEL85s/HpT/EM2qDkKZ15JostR2W1il+u1V
xCpRUz2HCa9sd0inFN7BtFO/ZgQDlcieOriynSDGYBYXMO1JWpHtjbiEvt9FbdGk
SampleSampleSampleSampleSampleSampleSampleSampleSampleSampleSamp
eB04BHrY9RTk2Oe6Bj5j9y0oCOcF0ScTSLRoA2z2PYTY0lacAiv7lq2fWq5/iVb1
KbNqIL7oMBn0oyFr2t9/STiHXU/F5gMbxqCN+A0F+S/Cdua5U5P1icnPF2f/RL32
cUrJM1soChcI1eJIDBlVsvOLOGEM761f5WYXIyMcM0fXi2nLpihrlh9yVU6El0Vq
+vPUlqLfxjlzOZgAjzSjsFSv0MBoSxeFM3zkGQs/OTkqHBVudJ1imoNAQXWRC50k
+OfItUAQmgIpvhQ3hCOXqnMXdgzVphjPS7J39nLSJRKXEKno3t4ahMkaHB6pFRun
hpNdlY3B+9h1iPh4Zxbr+3tIIDadQwV4Ic9JdtS+iUOQ4t/0zFcnMZ33lL81v+Fj
VOpMycKYkJVLVYyrq7gIxDjADR6BGWIJnuTeVyc8NQIBTUkrvYBlJkq5ro3p2jVg
Fh0Er0H1YxfkRTQR4uXWCv5GPIE+uMDHSb+JAvwYSsHQ0Vp+b3VKdkG4p7wgJeNS
7SODuznK2z8YcSiHz/SX7EKbx8kNCOODu/Tw4cav8GS9iUH5j7BRrePMFdQVv+bz
f9l1W429oro2Fxe/RdHzm7zliquiPE+/Wiw+sBIZfJFmwQS69aQOQeDawuHP6xBi
HOwF5Jqr15yTOxwRxmyGtOvj5M5x/uoPa4217fbfmyzc+XNxII4e6r95z3MGCZs0
hVIX4dAu37+i3cnJodWuqOn/it44OiHZbLM3pWGXNWb2BYCub/AUWxOQRfURGDDR
weU25EHFn3Xp5al4T8oHXaZBjJCBlVQ57A5YFX6CBsYRarVF7PJYycvRHa2eEM/y
ZTzDJjZZIaUu0rNPuNrTW7ZSH7mwq4ekCSRRbfyM05RkGWAhNiuXu2YWy7E07bMQ
5IIOdKXzx9FonECowWkAV1vDeesbVjxQTMdO82/dq5FjaErPTNji7RwS3izw9kiX
gcd00iltXKd6rUI8s1EwrFvEpURDcybVTi1D6PAZf25RjEF6MZAeNXKj7qPg3EDs
P4Rmi8y2pn93QHQfJxWI5aLmHP8ZCHrj8XRZFYrijOqM5y2DJBxTqIijd/S9197b
-----END RSA PRIVATE KEY-----
我收到以下异常
Exception in thread "main" java.lang.IllegalArgumentException: java.io.IOException: Invalid DER: length field too big (186)
at org.tomitribe.auth.signatures.RSA.privateKeyFromPKCS1(RSA.java:63)
at org.tomitribe.auth.signatures.PEM.readPrivateKey(PEM.java:71)
at com.zoho.listeners.Signing.loadPrivateKey(Signing.java:132)
at com.zoho.listeners.Signing.main(Signing.java:86)
Caused by: java.io.IOException: Invalid DER: length field too big (186)
at org.tomitribe.auth.signatures.RSA$DerParser.getLength(RSA.java:402)
at org.tomitribe.auth.signatures.RSA$DerParser.read(RSA.java:358)
at org.tomitribe.auth.signatures.RSA.newRSAPrivateCrtKeySpec(RSA.java:133)
at org.tomitribe.auth.signatures.RSA.privateKeyFromPKCS1(RSA.java:59)
... 3 more
有输入吗?
答案 0 :(得分:3)
看起来它期望使用DER编码证书而不是PEM证书。以下excerpt from this article应该提供一些背景知识:
我们首先要了解的是每种文件扩展名是什么。 关于DER,PEM,CRT和CER是什么,存在很多困惑,并且许多人错误地表示它们都是可互换的。尽管在某些情况下可以互换某些最佳实践,但最好的做法是确定证书的编码方式,然后正确标记它。正确标记的证书将更易于操作。
[...]
建议使用类似openssl genrsa -out ~/.oci/oci_api_key.pem 2048
而不创建-aes128
的密钥作为可能的解决方案here。