Пример #1
0
        public static byte[] CrearPFX(byte[] bytesCER, byte[] bytesKEY, string password)
        {
            try
            {
                if (bytesCER == null || bytesKEY == null)
                {
                    throw new Exception("Empty cer and or key");
                }

                var certificate = new Mono.Security.X509.X509Certificate(bytesCER);

                char[] arrayOfChars = password.ToCharArray();
                AsymmetricKeyParameter privateKey = Org.BouncyCastle.Security.PrivateKeyFactory.DecryptKey(arrayOfChars, bytesKEY);

                RSA subjectKey = DotNetUtilitiesCustom.ToRSA((RsaPrivateCrtKeyParameters)privateKey);

                Mono.Security.X509.PKCS12 p12 = new Mono.Security.X509.PKCS12();
                p12.Password = password;

                ArrayList list = new ArrayList();
                // we use a fixed array to avoid endianess issues
                // (in case some tools requires the ID to be 1).
                list.Add(new byte[4] {
                    1, 0, 0, 0
                });
                Hashtable attributes = new Hashtable(1);
                attributes.Add(Mono.Security.X509.PKCS9.localKeyId, list);
                p12.AddCertificate(certificate, attributes);
                p12.AddPkcs8ShroudedKeyBag(subjectKey, attributes);
                return(p12.GetBytes());
            }
            catch (Exception ex)
            {
                throw new Exception("Los datos del Certificado CER KEY o Password son incorrectos. No es posible leer la llave privada.", ex);
            }
        }