To get a private key, you need to decrypt the encrypted data. I can’t figure it out. When I get the privatekey, I report an error algid parse error, not a sequence
KeyFactory.getInstance("RSA").generatePrivate(
new PKCS8EncodedKeySpec(Encodes.decodeBase64("priKey")))
The reason is that the private key string is not in PKCs #8’s format and cannot be transferred without using a third-party jar
One solution is to use OpenSSL to convert the private key string into pkcs#8 format
The second is to use the third-party library. I use the third-party library bouncy castle, which is more convenient and fast. I don’t bother to install OpenSSL
pom:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.59</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15on</artifactId>
<version>1.59</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.59</version>
</dependency>
Restore private/public key to PEM file
public static PrivateKey get10027504355PrivateKey() throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream("config/10027504355ssl.pem")));
PEMParser pemParser = new PEMParser(br);
PEMKeyPair pemKeyPair = (PEMKeyPair)pemParser.readObject();
pemParser.close();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
KeyPair keyPair = converter.getKeyPair(pemKeyPair);
PublicKey publicKey=keyPair.getPublic();
return keyPair.getPrivate();
}
Add bouncycastleprovider when the application starts and initialize it once
package com.mktpay.admin.init;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.security.Security;
/**
* @ClassName Runner
* @Author yupanpan
* @Date 2021/10/11 15:03
*/
@Component
public class EDncryptRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
//Customize other ways to initialize encryption and decryption algorithms
Security.addProvider(new BouncyCastleProvider());
}
}
Read More:
- Java learning unreported exception java.io.IOException ; must be caught or declared to be thrown
- I/O error while reading input message; nested exception is java.io.IOException: Stream closed
- Three ways of thread sequence alternate execution in Java lock free programming
- [Solved] Sqoop Error: ERROR tool.ImportTool: Import failed: java.io.IOException
- [Solved] ClientAbortException: java.io.IOException: Connection reset by peer
- [Solved] javax.crypto.BadPaddingException: Decryption error
- [Solved] java.security.AccessControlException: Access Denied Error
- keytool error java.io.IOException:keystore was tampered with,or password was incorre
- [Solved] java.sql.SQLException: Table ‘xxx.hibernate_sequence’ doesn’t exist
- keytool Error: java.io.IOException: Keystore was tampered with, or password was incorrect
- [Solved] IDEA error: sun.security.pkcs not found
- Run hadoop fs -put Command Error: java.io.IOException: Got error, status message , ack with firstBadLink
- [Solved] SpringBoot Date Convert Error: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime`
- [Solved] Hadoop Error: Exception in thread “main“ java.io.IOException: Error opening job jar: /usr/local/hadoop-2.
- [Solved] JSON parse error: Cannot deserialize instance of `java.util.ArrayList<..> out of START_OBJECT token;
- [Solved] MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.Runtime
- [Solved] canal Startup Error: error while reading from client socket java.io.IOException: Received error packet:
- [Solved] java.io.IOException: Got error, status=ERROR, status message, ack with firstBadLink as
- Spring Security Upgrade to Version 5.5.7, 5.6.4 or Above to Startup Error (Version incompatibility)
- [Solved] Ceres Compile error: ‘integer_sequence’ is not a member of ‘std‘