I'm using this little class which returns me a pfx file in byte array.
Server side:
byte[] serverCertificatebyte;
var date = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day);
serverCertificatebyte = Certificate.CreateSelfSignCertificatePfx("CN=RustBuster" + RandomString(5),
date,
date.AddDays(7));
Then I send It to the client (length: 1654):
tcpClient.GetStream().Write(serverCertificatebyte , 0, serverCertificatebyte .Length);
Once the client read It, I would like to convert It to a certificate class: (Length is also 1654 here)
I try to do a new X509Certificate2(data); and I get the error down below. This works on server side. What's the matter?
I also tried It with new X509Certificate2(data, string.Empty); and got the same error
Error System.Security.Cryptography.CryptographicException: Unable to decode certificate. ---> System.Security.Cryptography.CryptographicException: Input data cannot be coded as a valid certificate. ---> System.ArgumentOutOfRangeException: Cannot be negative.
Parameter name: length
at System.String.Substring (Int32 startIndex, Int32 length) [0x00000] in :0
at Mono.Security.X509.X509Certificate.PEM (System.String type, System.Byte[] data) [0x00000] in :0
at Mono.Security.X509.X509Certificate..ctor (System.Byte[] data) [0x00000] in :0