2017-08-03 11 views
1

INにはX509CertificateのJavaで[]バイトおよび背面のX509CertificateのC#

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); 
        } 
       } 

をsnipppet CustomMapCertificateWrapperは、x509certificateをバイト配列として格納する、byte [] 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) 
+0

エンディアンを無効にしようとしましたか(バイトを反転しないでください)? – Crypt32

+0

@ Crypt32私はそれを試して、動作しません( – Huseyn

答えて

0

の下にJavaのObjectOutputStreamが読み取られるように設計された出力を生成しますように、このコードスニペットに

//Do array reverse because of BigEndian difference between Java and c# languages 
Array.Reverse(customMapCertificateWrapper.value); 

         var certificate = new X509Certificate(customMapCertificateWrapper.value); 

を使用してこのコードは、私をexpcetionスローマイ.NETサービスがCustomMapCertificateWrapperこのオブジェクトを受け取り、私はC#側でのX509Certificateを生成しようJavaのObjectInputStreamによる。言語に依存しない標準的な結果は出ません。

移植性を考慮して、Certificate.getEncoded()メソッドを使用してJava X509Certificateをシリアル化する必要があります。出力は、またはX509Certificate2()コンストラクタへのbyte[]引数としてC#側で使用できます。

+0

ありがとうございます)getEncoded()を使用すると、C#X509Certificate(byte [])で使用できる必要なバイト[]が得られます。 – Huseyn

関連する問題