잊지 않겠습니다.

'RSA'에 해당되는 글 1건

  1. 2010.01.05 0005. CSXmlDigitalSigniture
RSAServiceProvider를 이용해서 xml 파일에 서명 및 Decryption 하는 과정
RSAServiceProvider를 제공된 public key를 이용해서 얻어내는 것이 이 코드의 핵심.



private String publicOnlyKey;
private void btnSign_Click(object sender, EventArgs e)
{
    try
    {
        /////////////////////////////////////////////////////////////////
        // Create a new RSA signing key and export public key for 
        // verification.
        
        RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider();
        
        //Public Key exported
        //Public Key 뿐 아니라 Private Key도 얻어내는 것이 가능하다. 사용자에게 Key를 보내거나 저장할 때 이용 가능
        publicOnlyKey = rsaKey.ToXmlString(false);
        tbxRSAParameters.Text = publicOnlyKey;
        
        /////////////////////////////////////////////////////////////////
        // Sign the XML document.
        // 

        SignXml(xmlDoc, rsaKey);
        MessageBox.Show("XML file signed.");


        /////////////////////////////////////////////////////////////////
        // Save and display the signed document.
        // 

        xmlDoc.Save("test1.xml");
        tbxDigitalSignature.Text = xmlDoc.OuterXml;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

///XML파일을 RSA를 이용해서 서명. SignedXml class가 있는 것을 기억하고 있을것!
public static void SignXml(XmlDocument Doc, RSA Key)
{
    // Check arguments.
    if (Doc == null)
        throw new ArgumentException("Doc");
    if (Key == null)
        throw new ArgumentException("Key");

    try
    {
        // Create a SignedXml object to generate signature.
        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 an enveloped transformation to the reference
        XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
        reference.AddTransform(env);

        // Add the reference to the SignedXml object
        signedXml.AddReference(reference);

        // 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));
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void btnVerify_Click(object sender, EventArgs e)
{
    /////////////////////////////////////////////////////////////////////
    // Create a new RSA signing key and import public key for 
    // verification.
    //

    //NOTE:Public RSA Key를 이용해서 RSACryptoService Generate
    RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider();
    rsaKey.FromXmlString(publicOnlyKey);
    /////////////////////////////////////////////////////////////////////
    // Load the signed XML, and call VerifyXml to verify the signature of 
    // the signed XML.
    // 

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("test1.xml");

    bool result = VerifyXml(xmlDoc, rsaKey);

    if (result)
    {
        MessageBox.Show("The XML signature is valid.");
    }
    else
    {
        MessageBox.Show("The XML signature is not valid.");
    }
}

public static Boolean VerifyXml(XmlDocument Doc, RSA Key)
{
    // Check arguments.
    if (Doc == null)
        throw new ArgumentException("Doc");
    if (Key == null)
        throw new ArgumentException("Key");


    /////////////////////////////////////////////////////////////////////
    // Create a SignedXml object to verify the signature
    //

    SignedXml signedXml = new SignedXml(Doc);

    // Find Signature node and create a new XmlNodeList object
    XmlNodeList nodeList = Doc.GetElementsByTagName("Signature");

    // Throw an exception if no signature was found.
    if (nodeList.Count <= 0)
    {
        throw new CryptographicException(
            "Verification failed:" +
            " No Signature was found in the document.");
    }

    // This example only supports one signature for entire XML document
    if (nodeList.Count >= 2)
    {
        throw new CryptographicException(
            "Verification failed: More that one signature was found.");
    }

    // Load the first  node.  
    signedXml.LoadXml((XmlElement)nodeList[0]);

    // Check the signature and return the result.
    return signedXml.CheckSignature(Key);
}

private void btnChangeXML_Click(object sender, EventArgs e)
{
    // Modify the value of the Xml document for test. 

    XDocument xDoc = XDocument.Load("test1.xml");

    if (xDoc != null)
    {
        xDoc.Element("invoice").Element("items").
            Element("creditcard").Element("number").SetValue("19834210");

        xDoc.Save("test1.xml");

        tbxModifiedMessage.Text = xDoc.ToString();
    }
}
저작자 표시 비영리 변경 금지
신고
Posted by xyzlast Y2K


티스토리 툴바