尝试加载证书时出现以下问题:

> java.io.IOException: DerInputStream.getLength(): lengthTag=66, too big.
|   at sun.security.util.DerInputStream.getLength(DerInputStream.java:561)
|   at sun.security.util.DerValue.init(DerValue.java:365)
|   at sun.security.util.DerValue.<init>(DerValue.java:320)
|   at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1914)
|   at java.security.KeyStore.load(KeyStore.java:1445)
|   at com.sequenceiq.cloudbreak.cloud.wap.client.WapClient.getFactory(WapClient.java:61)
|   at com.sequenceiq.cloudbreak.cloud.wap.client.WapClient.checkConnect(WapClient.java:80)
|   at com.sequenceiq.cloudbreak.cloud.wap.WapCredentialConnector.verify(WapCredentialConnector.java:53)
|   at com.sequenceiq.cloudbreak.cloud.handler.CredentialVerificationHandler.accept(CredentialVerificationHandler.java:43)
|   at com.sequenceiq.cloudbreak.cloud.handler.CredentialVerificationHandler.accept(CredentialVerificationHandler.java:20)
|   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
|   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
|   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
|   at java.lang.reflect.Method.invoke(Method.java:498)
|   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
|   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
|   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
|   at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
|   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
|   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
|   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
|   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
|   at com.sun.proxy.$Proxy201.accept(Unknown Source)
|   at reactor.bus.EventBus$3.accept(EventBus.java:317)
|   at reactor.bus.EventBus$3.accept(EventBus.java:310)
|   at reactor.bus.routing.ConsumerFilteringRouter.route(ConsumerFilteringRouter.java:72)
|2016-06-07 19:56:59,465 [reactorDispatcher-17] accept:56 [34mINFO [0;39m c.s.c.c.h.CredentialVerificationHandler - [owner:c90f9d2e-587f-4af6-a3a8-f1d321caa3a1] [type:springLog] [id:null] [name:debug2] Credential verification successfully finished
|   at reactor.bus.routing.TraceableDelegatingRouter.route(TraceableDelegatingRouter.java:51)
|   at reactor.bus.EventBus.accept(EventBus.java:591)
|   at reactor.bus.EventBus.accept(EventBus.java:63)
|   at reactor.core.dispatch.AbstractLifecycleDispatcher.route(AbstractLifecycleDispatcher.java:160)
web01#3|2016-06-07 19:56:59,466 [http-nio-9091-exec-4] init:51 [34mINFO [0;39m c.s.c.s.s.c.a.ServiceProviderCredentialAdapter - [owner:c90f9d2e-587f-4af6-a3a8-f1d321caa3a1] [type:springLog] [id:] [name:] Result: CloudPlatformResult{status=OK, statusReason='null', errorDetails=null, request=CloudPlatformRequest{cloudContext=CloudContext{id=null, name='debug2', platform='StringType{value='WAP'}', owner='c90f9d2e-587f-4af6-a3a8-f1d321caa3a1'}, cloudCredential=com.sequenceiq.cloudbreak.cloud.model.CloudCredential@4bd64260}}
|   at reactor.core.dispatch.MultiThreadDispatcher$MultiThreadTask.run(MultiThreadDispatcher.java:74)
|   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
|   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
|   at java.lang.Thread.run(Thread.java:745)


这是代码示例:

          keyInput = new FileInputStream(pKeyFile);
          keyStore.load(keyInput, pKeyPassword.toCharArray());
          keyInput.close();


请注意,在加载之前,我将服务器收到的证书写在一个新文件中:

        File file = new File(name);
        try(FileWriter fw = new FileWriter(file)){
            fw.write(certificate);
        }catch(IOException e){
            LOGGER.debug("Writer issue",e);
        }


编写文件后,我已经尝试读取该文件,并且它可以工作。因此,我很确定这不是InputStream问题。

证书是一个pkcs12文件。
可能是因为我无法加载刚刚在新文件中写入的证书?

最佳答案

PKCS#12以DER格式编码,而DER格式是binary。

您正在使用的是FileReader(来自javadoc)


编写字符文件的便捷类。


继承自OutputStreamWriter


OutputStreamWriter是从字符流到字节流的桥梁:使用指定的字符集将写入其中的字符编码为字节。


因此


您在将二进制文件转换为char时遇到编码问题
您收到的文件不是真正的二进制格式。可能在Base64中


在这两种情况下,都使用FileOutputStream.writeFiles.write来存储文件,并且如果您的“ certificate”变量是字符串,请首先将其转换为二进制

File file = new File(name);
FileOutputStream fout = new FileOutputStream(file);
fout.write (data);

Files.write(Paths.get("name"), data);

10-05 23:52