我有一个Java,它接收来自其他X509Certificate的service.On Java,使用这个代码片段将X509Certificate序列化为字节数组
for (X509Certificate certificate : certs) {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
try (ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(certificate);
wrapper = new CustomMapCertificateWrapper();
wrapper.setCustomValue(bos.toByteArray());
response.getCustomMapCertificateWrapper().add(wrapper);
}
}这里的CustomMapCertificateWrapper是一个具有byte[]值的类,名为字段,它将x509certificate存储为字节数组。我的.NET服务接收到这个对象CustomMapCertificateWrapper,我尝试使用这个代码片段在C#端生成X509Certificate。
//Do array reverse because of BigEndian difference between Java and c# languages
Array.Reverse(customMapCertificateWrapper.value);
var certificate = new X509Certificate(customMapCertificateWrapper.value);这段代码会使我得到如下的补偿
System.Security.Cryptography.CryptographicException: Cannot find the requested object.
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertBlobType(Byte[] rawData)
at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData)发布于 2017-08-04 02:34:19
Java的ObjectOutputStream生成可供ObjectInputStream读取的输出。它不会产生一个标准的,语言独立的结果。
为了便于移植,您应该使用X509Certificate方法序列化Certificate.getEncoded()。然后,可以在C#端将输出用作X509Certificate()或X509Certificate2()构造函数的byte[]参数。
https://stackoverflow.com/questions/45487070
复制相似问题