1
I need to decrypt an XML file, but already passing the certificate password. I can do similar actions, such as signing the file without the certificate driver requesting the password. Follow:
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 using 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 it without driver certificate request the password.
http://answall.com/a/30168/101
– Maniero
In which of these lines of execution the screen asking for the certificate password is displayed?
– Leonel Sanches da Silva
The line is exml.Decryptdocument(); I was able to do the following by signing some string before decrypting, because the password is only requested the first time the certificate is used for a program instance, so since I can pass the password to sign a string, I don’t need to pass the password to decrypt.. was a "little way" that worked.
– Wagner
@Wagner If you found the solution please post as a response.
– Genos