public void Dispose() { PrivateKey?.Dispose(); PrivateKey = null; PublicKey?.Dispose(); PublicKey = null !; }
/// <summary> /// Performs the actual job of disposing the object. /// </summary> /// <param name="disposing"> /// Passes the information whether this method is called by <see cref="Dispose"/> (explicitly or /// implicitly at the end of a <c>using</c> statement), or by the <see cref="M:~SymmetricCipher"/>. /// </param> /// <remarks> /// If the method is called with <paramref name="disposing"/><c>==true</c>, i.e. from <see cref="Dispose"/>, it will try to release all managed resources /// (usually aggregated objects which implement <see cref="IDisposable"/> as well) and then it will release all unmanaged resources if any. /// If the parameter is <c>false</c> then the method will only try to release the unmanaged resources. /// </remarks> protected override void Dispose(bool disposing) { if (disposing) { if (PrivateKey != null) { PrivateKey.Dispose(); } if (PublicKey != null) { PublicKey.Dispose(); } } base.Dispose(disposing); }
/// <summary> /// Releases the asymmetric keys. By doing so the instance looses its <see cref="IKeyManagement" /> behavior but the memory footprint becomes much lighter. /// The asymmetric keys can be dropped only if the underlying symmetric algorithm instance is already initialized and /// the property <see cref="SymmetricKeyCipherBase.ShouldEncryptIV" /> is <see langword="false" />. /// </summary> /// <exception cref="InvalidOperationException"> /// If the underlying symmetric algorithm instance is not initialized yet or the property <see cref="SymmetricKeyCipherBase.ShouldEncryptIV" /> is <see langword="false" />. /// </exception> /// See also <seealso cref="CloneLightCipher"/>. public virtual ICipherAsync ReleaseCertificate() { if (ShouldEncryptIV) { throw new InvalidOperationException("This object cannot reset its asymmetric keys because the property" + nameof(ShouldEncryptIV) + " is true."); } if (PublicKey == null) { return(this); } InitializeSymmetricKey(); PublicKey.Dispose(); PublicKey = null; PrivateKey?.Dispose(); PrivateKey = null; KeyStorage = null; return(this); }
public CfdiResult SellarXml(String rutaCert, String rutaKey, String contrasena, String rutaArchivoXmlOrigen) { CfdiResult result = AgregarCertificado(rutaCert); if (!result.Correcto) { return(result); } PrivateKey privKey = new PrivateKey(); XDocument xDoc = XDocument.Load(rutaArchivoXmlOrigen); xDoc.Root.SetAttributeValue("Certificado", this.Certificado); xDoc.Root.SetAttributeValue("NoCertificado", this.NoCertificado); xDoc.Save(rutaArchivoXmlOrigen); String cadenaOriginal = GetCadenaOriginal(this.RutaXSLTCadenaOriginal, rutaArchivoXmlOrigen); if (!privKey.LoadPkcs8EncryptedFile(rutaKey, contrasena)) { return(new CfdiResult(false, privKey.LastErrorText)); } String privKeyXml = privKey.GetXml(); Rsa rsa = new Rsa(); String hashAlg = "SHA-256"; if (!rsa.UnlockComponent("RSAT34MB34N_7F1CD986683M")) { return(new CfdiResult(false, rsa.LastErrorText)); } if (!rsa.ImportPrivateKey(privKeyXml)) { return(new CfdiResult(false, rsa.LastErrorText)); } rsa.Charset = "UTF-8"; rsa.EncodingMode = "base64"; rsa.LittleEndian = false; Cert cert = new Cert(); if (!cert.LoadFromFile(rutaCert)) { return(new CfdiResult(false, cert.LastErrorText)); } String selloBase64 = rsa.SignStringENC(cadenaOriginal, hashAlg); PublicKey publicKey = cert.ExportPublicKey(); Rsa rsa2 = new Rsa(); if (!rsa2.ImportPublicKey(publicKey.GetXml())) { return(new CfdiResult(false, rsa2.LastErrorText)); } rsa2.Charset = "utf-8"; rsa2.EncodingMode = "base64"; rsa2.LittleEndian = false; if (!rsa2.VerifyStringENC(cadenaOriginal, hashAlg, selloBase64)) { return(new CfdiResult(false, rsa2.LastErrorText)); } cert.Dispose(); privKey.Dispose(); publicKey.Dispose(); this.Sello = selloBase64; xDoc.Root.SetAttributeValue("Sello", selloBase64); //String xml = xDoc.ToString(); //File.WriteAllText(rutaArchivoXmlOrigen, xml); xDoc.Save(rutaArchivoXmlOrigen); //SerializarObjeto(rutaArchivoXmlOrigen); return(new CfdiResult()); }