I need to decrypt an XML file, but I've already passed the certificate password. I can do similar actions, like signing the file without the certificate driver asking for the password. Following:
SecureString senha = new SecureString();
foreach(char c in senhaCert.ToCharArray())
senha.AppendChar(c);
RSACryptoServiceProvider rsaKeyBase = new RSACryptoServiceProvider();
try {
rsaKeyBase = (RSACryptoServiceProvider) cert.PrivateKey;
} catch (Exception) {
return "E_acessar_chave_certificado";
}
CspParameters cspParams = new CspParameters();
cspParams.ProviderName = rsaKeyBase.CspKeyContainerInfo.ProviderName;
cspParams.ProviderType = rsaKeyBase.CspKeyContainerInfo.ProviderType;
cspParams.KeyNumber = rsaKeyBase.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2;
cspParams.KeyContainerName = rsaKeyBase.CspKeyContainerInfo.KeyContainerName;
cspParams.KeyPassword = senha;
cspParams.Flags = CspProviderFlags.NoPrompt | CspProviderFlags.UseDefaultKeyContainer;
// Instancia a nova chave de assinatura RSA e salva no contêiner.
try {
rsaKey = new RSACryptoServiceProvider(cspParams);
} catch (Exception) {
return "E_SenhaCert_Incorreta";
}
try {
rsaKey = (RSACryptoServiceProvider) cert.PrivateKey;
} catch (Exception) {
return "E_acessar_chave_certificado";
}
// Criando um documento Xml.
XmlDocument xmlDoc = new XmlDocument();
// Carregando um arquivo Xml dentro do objeto XmlDocument.
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(fileName);
// Check arguments.
if (xmlDoc == null) return "E_nao_carregou_xml";
if (rsaKey == null) return "E_key_invalida";
// Instancia o SignedXml.
SignedXml signedXml = new SignedXml(xmlDoc);
// Adiciona a chave RSA no documento SignedXml.
signedXml.SigningKey = rsaKey;
// Cria a referencia que deve ser assinada.
Reference reference = new Reference();
reference.Uri = "";
// Adiciona uma transformação envolvida com a referência.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Adiciona a referencia no objeto SignedXml.
signedXml.AddReference(reference);
// Instancia o KeyInfo.
KeyInfo keyInfo = new KeyInfo();
// Carrega o certificado dentro do objeto KeyInfoX509Data
// e adiciona no objeto KeyInfo.
keyInfo.AddClause(new KeyInfoX509Data(cert));
signedXml.KeyInfo = keyInfo;
// Grava a assinatura.
signedXml.ComputeSignature();
// Pega a representação do XML da assinatura e salva
// no objeto XmlElement.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Anexa o elemento no documento XML.
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
// Salvo o documento.
// Salvo o documento XML.
try {
xmlDoc.Save(fileName);
} catch {
return "E_salvar_xml";
}
To decrypt I use the following commands:
// Instancia a class XmlDocument
XmlDocument xmlDoc = new XmlDocument();
// Carrega o arquivo XML dentro do objeto XmlDocument.
xmlDoc.PreserveWhitespace = true;
try {
xmlDoc.Load(fileName);
} catch (Exception) {
return "E_nao_carregou_xml";
}
// Descriptografa o documento.
if (xmlDoc == null) return "E_xml_invalido";
// Instancia a class EncryptedXml.
EncryptedXml exml = new EncryptedXml(xmlDoc);
try {
exml.DecryptDocument();
} catch (Exception) {
return "E_DecryptDocument";
}
try {
xmlDoc.Save(Path.GetDirectoryName(fileName) + "\" + Path.GetFileNameWithoutExtension(fileName) + "_decrypted.xml");
} catch {
return "E_salvar_xml";
}
I wanted to decrypt without the driver's driver requesting the password.