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:
- How to Solve JUnit Debugging initializationerror ERROR
- How to Solve Spring integrate Seata startup error
- [How to Fix]java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x98\x82\xF0\x9F…’
- Ali easyexcel error: org.apache.poi.ss.usermodel.font.setbold (z) V
- [Solved] fasterxml ToStringSerializerBase Error: Caused by: java.lang.NoClassDefFoundError…
- [Solved] Jedis connect and operate Redis error: Failed to create socket和connect timed out
- [Solved] Jxls error: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath
- Code case of XXL job executor
- Error querying database.Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource.
- [Solved] The Bean Validation API is on the classpath but no implementation could be found
- Error resolving template [index], template might not exist
- Caused by: java.lang.IllegalStateException (How to Fix)
- [Solved] Spring Cloud Use Ribbon Error: No instances available for XXX
- spring cloud 2020 gateway Error 503 [How to Solve]
- Error resolution: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/Datatype
- Jjwt error: ERROR 9856 — [nio-8083-exec-2] o.a.c.c. [. [. [. [/]. [dispatch server]
- [Solved] Openfegn Remote Call Error: java.lang.NullPointerException: null
- [Solved] Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
- java: java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x590
- Springboot uses Oracle database to report property ‘sqlsessionfactory’ or ‘sqlsessiontemplate’ are required