java.security.InvalidKeyException: IOException : Unknown named curve: 1.2.156.10197.1.301

java.security.InvalidKeyException: IOException : Unknown named curve: 1.2.156.10197.1.301

public static PrivateKey privKeySM2FromBase64Str(String keystr) throws Exception {
		KeyFactory keyFactory = KeyFactory.getInstance("EC");
		return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(keystr)));
}

SM2国密加密在创建私钥时,运行上面代码报错:java.security.InvalidKeyException: IOException : Unknown named curve: 1.2.156.10197.1.301

解决步骤
1、引入jar包

			<dependency>
                <groupId>org.bouncycastlegroupId>
                <artifactId>bcprov-jdk15onartifactId>
                <version>1.68version>
            dependency>

2、添加下面的代码

static{
        Security.addProvider(new BouncyCastleProvider());
    }

如果上面的还解决不了检查以下pom文件,是不是在之前引入了bcprov-jdk16,会造成jar包冲突,删除此jar包即可

			<dependency>
                <groupId>org.bouncycastlegroupId>
                <artifactId>bcprov-jdk16artifactId>
                <version>${bcprov.version}version>
            dependency>

其他参考博客:
https://blog.csdn.net/weixin_43882265/article/details/132733835

你可能感兴趣的:(Java,java,开发语言)