Tach zusammen,
ich versuch grade für eine Anwendung einer Verschlüsselung/Lizensierung. Ich hab leider Gottes null peil von verschlüsselung und hab mir daher ein Stück Code runter geladen. Leider ist das AM Entscheidenen Code Abschnitt (beim prüfen der signatur) sau langsam. Das dauert zwischen 4 und 10! Sekunden. Gut mein Rechner ist jetzt nicht der beste aber sollte das nicht im millisekunden bereich liegen?
Ich häng mal den Code an vielleicht kann mir einer von euch den entscheidenen Tip geben!
//
// This example signs an XML file using an
// envelope signature. It then verifies the
// signed XML.
//
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
public class SignVerifyEnvelope
{
public static void Main(String[] args)
{
try
{
// Generate a signing key.
RSACryptoServiceProvider Key = new RSACryptoServiceProvider();
// Create an XML file to sign.
CreateSomeXml("Example.xml");
Console.WriteLine("New XML file created.");
// Sign the XML that was just created and save it in a
// new file.
SignXmlFile("Example.xml", "SignedExample.xml", Key);
Console.WriteLine("XML file signed.");
// Verify the signature of the signed XML.
Console.WriteLine("Verifying signature...");
bool result = VerifyXmlFile("SignedExample.xml");
// Display the results of the signature verification to \
// the console.
if (result)
{
Console.WriteLine("The XML signature is valid.");
}
else
{
Console.WriteLine("The XML signature is not valid.");
}
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
}
// Sign an XML file and save the signature in a new file.
public static void SignXmlFile(string FileName, string SignedFileName, RSA Key)
{
// Create a new XML document.
XmlDocument doc = new XmlDocument();
// Format the document to ignore white spaces.
doc.PreserveWhitespace = false;
// Load the passed XML file using it's name.
doc.Load(new XmlTextReader(FileName));
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(doc);
// Add the key to the SignedXml document.
signedXml.SigningKey = Key;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add a transformation to the reference.
Transform trns = new XmlDsigC14NTransform();
reference.AddTransform(trns);
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue((RSA)Key));
signedXml.KeyInfo = keyInfo;
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
if (doc.FirstChild is XmlDeclaration)
{
doc.RemoveChild(doc.FirstChild);
}
// Save the signed XML document to a file specified
// using the passed string.
XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false));
doc.WriteTo(xmltw);
xmltw.Close();
}
// Verify the signature of an XML file and return the result.
public static Boolean VerifyXmlFile(String Name)
{
// Create a new XML document.
XmlDocument xmlDocument = new XmlDocument();
// Format using white spaces.
xmlDocument.PreserveWhitespace = true;
// Load the passed XML file into the document.
xmlDocument.Load(Name);
// Create a new SignedXml object and pass it
// the XML document class.
SignedXml signedXml = new SignedXml(xmlDocument);
// Find the "Signature" node and create a new
// XmlNodeList object.
XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");
// Load the signature node.
signedXml.LoadXml((XmlElement)nodeList[0]);
// Check the signature and return the result.
return signedXml.CheckSignature();
}
// Create example data to sign.
public static void CreateSomeXml(string FileName)
{
// Create a new XmlDocument object.
XmlDocument document = new XmlDocument();
// Create a new XmlNode object.
XmlNode node = document.CreateNode(XmlNodeType.Element, "", "MyElement", "samples");
// Add some text to the node.
node.InnerText = "Example text to be signed.";
// Append the node to the document.
document.AppendChild(node);
// Save the XML document to the file name specified.
XmlTextWriter xmltw = new XmlTextWriter(FileName, new UTF8Encoding(false));
document.WriteTo(xmltw);
xmltw.Close();
}
}
Vielen Dank und viele Grüße!
Christoph
Leider ist das AM Entscheidenen Code Abschnitt (beim prüfen der signatur) sau langsam. Das dauert zwischen 4 und 10! Sekunden.
Hi! Du meinst bei signedXml.ComputeSignature();?
Gut mein Rechner ist jetzt nicht der beste aber sollte das nicht im millisekunden bereich liegen?
Nur bei signedXml.ComputeSignature(); dauert es ein Augenzwinkern, aber keine 4-10 Sekunden. Ich bin den Code im Debugger Einzelschritt durchgegangen. Im Releasemodus ist überhaupt kein Stocken zu spüren. Was hast Du für einen Rechner? Wie alt ist die Kiste? Grüsse - sarabande
Hey,
ich mein die Stelle:
// Check the signature and return the result.
return signedXml.CheckSignature();
Mein Rechner ist ein Notebook mit Intel Core 2 T7200 @ 2 Ghz mit 2 Gb ram und Windows Vista. Müsste eigentlich ausreichen.
Kannste ne Stopwatch drum packen und mir sagen was die zurückgibt? Das wär super. Vielleicht ist in meinem Rechner der Wurm drin.
Vg!
Mein Rechner ist ein Notebook mit Intel Core 2 T7200 @ 2 Ghz mit 2 Gb ram und Windows Vista. Müsste eigentlich ausreichen.
Hi! Ich habe einen Core2Duo und XP.
Kannste ne Stopwatch drum packen und mir sagen was die zurückgibt?
20 Millisekunden. Grüsse - sarabande
Hehe Krass,
das ist mal ne ordentliche zeitspanne zwischen deinem und meinem system. Vielen Dank für die Hilfe!
das ist mal ne ordentliche zeitspanne zwischen deinem und meinem system.
Hi! Und die Gründe können vielfältig sein. OS, FW etc. Einen habe ich im Netz gefunden, der sich auch darüber beschwert hat, aber ein Lösungansatz war leider nicht dabei.
http://www.eggheadcafe.com/software/aspnet/32479326/checksignature-slow.aspx
Grüsse - sarabande
Hast du schon mal probiert eine komplette Stino-Datei zu verschlüsseln/zu Signieren?
ich versuch grade für eine Anwendung einer Verschlüsselung/Lizensierung. Ich hab leider Gottes null peil von verschlüsselung und hab mir daher ein Stück Code
Das ist schlecht, du musst nicht unbedingt ein Mathematik-/InformatikStudium absolviert haben, aber bissel Grundkenntnisse brauchst du schon. Allein die Vor- und Nachteile der jewiligen Verschlüsselungsverfahren und Hashingverfahren sind schon wichtig.
Eventuell kommst du durch folgenden Link auf den entsprechenden Trichter:
Wiki: Geburtstagsangriff
Wo du recht hast hast du recht 😉 Ich werd mal versuchen mich einzulesen und meld mich dann nochmal.