0

There is my context: We have Xamarin.Forms App that receive a certificate from the server in Base64 String with \n\r. I want to put this certificate in KeyStore so I'm doing this code :

        var ks = KeyStore.GetInstance("AndroidKeyStore");
        ks.Load(null);

        if (ks.ContainsAlias(RSAStrings.ServerCertificateName))
        {
            ks.DeleteEntry(RSAStrings.ServerCertificateName);
        }

        var cerBase64 = "-----BEGIN CERTIFICATE-----" + Environment.NewLine + cer + "-----END CERTIFICATE-----" + Environment.NewLine;
        var cerBytes = System.Text.UTF8Encoding.UTF8.GetBytes(cerBase64);
        var memStream = new MemoryStream(cerBytes);
        this.serverCertificate = CertificateFactory.GetInstance("X.509").GenerateCertificate(memStream);
        ks.SetCertificateEntry(RSAStrings.ServerCertificateName, this.serverCertificate);

When GenerateCertificate, I got error "inStream is empty", but my MemoryStream contains all bytes, I can see it.

Any help will be appreciated.

Thank you !

Film
  • 1
  • 1
  • Did you try to use `MemoryStream memStream = new MemoryStream(cerBytes);`,not use `ByteArrayInputStream` ? – Leo Zhu May 13 '21 at 06:45
  • Hello Leo, yes i start with MemoryStream memStream = new MemoryStream(certBytes); but it was not working – Film May 13 '21 at 20:12
  • It's weird,the codes seem to be correct.Could cerBytes be obtained correctly ? – Leo Zhu May 24 '21 at 02:29
  • Yes, I was able to see all bytes. – Film May 26 '21 at 15:26
  • Anyway, I create a workaround by saving the certificate in an encrypted file and by using X509Certificate2 instead of put it in the store. – Film May 26 '21 at 15:36

0 Answers0