2010-03-30 161 views
5

它晚了,我累了,而且很可能是相當密集....保護與RSA密鑰的許可證密鑰

我寫,我需要確保,因此只能在機器上運行的應用程序,我爲...生成密鑰。 我現在所做的是獲取BIOS序列號並從中生成一個哈希值,然後使用XML RSA私鑰對其進行加密。然後我簽名XML以確保它不被篡改。 我想打包公鑰解密和驗證簽名,但每次我嘗試執行代碼作爲不同的用戶比生成簽名我簽署失敗的用戶。

我的大部分代碼都是從我發現的示例代碼中修改的,因爲我不像我想要的那樣熟悉RSA加密。下面是我使用的代碼和我認爲我需要使用的代碼來獲得這個工作的權利...

任何反饋將不勝感激,因爲我現在很迷茫 我正在使用的原始代碼是這樣的,這個代碼工作正常,只要用戶啓動程序是最初簽署該文件的同一個...

CspParameters cspParams = new CspParameters(); 
      cspParams.KeyContainerName = "XML_DSIG_RSA_KEY"; 
      cspParams.Flags = CspProviderFlags.UseMachineKeyStore; 

      // Create a new RSA signing key and save it in the container. 
      RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams) 
      { 
       PersistKeyInCsp = true, 
      }; 

這段代碼是什麼,我相信我應該做的,但它的失敗驗證無論我做什麼簽名,無論它是相同的用戶還是不同的...

RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(); 
      //Load the private key from xml file 
      XmlDocument xmlPrivateKey = new XmlDocument(); 
      xmlPrivateKey.Load("KeyPriv.xml"); 
      rsaKey.FromXmlString(xmlPrivateKey.InnerXml); 

我相信這與密鑰容器名稱有關(請原諒我是真正的笨蛋)我確信這是導致它在第一種情況下工作並阻止其發生的行在第二種情況下的工作....

cspParams.KeyContainerName = "XML_DSIG_RSA_KEY"; 

有沒有辦法讓我簽字/生成應用程序許可證時,用私鑰加密的XML,然後在應用程序目錄和使用拖放公鑰驗證/解密代碼?如果我可以讓簽名部分正常工作,我可以放棄加密部分。我使用它作爲備份來混淆我所關注的許可證代碼的來源。

這是否有任何意義? 我是一個傻瓜嗎?

感謝所有幫助任何人都可以給我這個..

回答

5

我用這個方法來註冊使用存儲在一個XML文件,我再嵌入到應用程序.dll文件作爲資源私鑰的XML文檔。我認爲你可能正在努力訪問密鑰庫的權限,這也會造成將代碼轉移到其他服務器等問題。

以下是將私鑰作爲嵌入資源並簽署文檔的代碼: (標誌是這個方法位於類的名稱,Licensing.Private.Private.xml是默認的命名空間+文件夾+資源的文件名)的組合

public static void SignDocument(XmlDocument xmldoc) 
{ 
    //Get the XML content from the embedded XML privatekey. 
    Stream s = null; 
    string xmlkey = string.Empty; 
    try 
    { 
     s = typeof(Sign).Assembly.GetManifestResourceStream("Licensing.Private.Private.xml"); 

     // Read-in the XML content. 
     StreamReader reader = new StreamReader(s); 
     xmlkey = reader.ReadToEnd(); 
     reader.Close(); 
    } 
    catch (Exception e) 
    { 
     throw new Exception("Error: could not import key:",e); 
    } 

    // Create an RSA crypto service provider from the embedded 
    // XML document resource (the private key). 
    RSACryptoServiceProvider csp = new RSACryptoServiceProvider(); 
    csp.FromXmlString(xmlkey); 
    //Creating the XML signing object. 
    SignedXml sxml = new SignedXml(xmldoc); 
    sxml.SigningKey = csp; 

    //Set the canonicalization method for the document. 
    sxml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationUrl; // No comments. 

    //Create an empty reference (not enveloped) for the XPath transformation. 
    Reference r = new Reference(""); 

    //Create the XPath transform and add it to the reference list. 
    r.AddTransform(new XmlDsigEnvelopedSignatureTransform(false)); 

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

    //Compute the signature. 
    sxml.ComputeSignature(); 

    // Get the signature XML and add it to the document element. 
    XmlElement sig = sxml.GetXml(); 
    xmldoc.DocumentElement.AppendChild(sig); 
} 

使用下面的代碼生成私有.xml和public.xml鍵。顯然,保持private.xml文件的安全。

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 
File.WriteAllText(@"C:\privateKey.xml", rsa.ToXmlString(true)); // Private Key 
File.WriteAllText(@"C:\publicKey.xml", rsa.ToXmlString(false)); // Public Key 
+0

太棒了!謝謝您的幫助!這不僅解決了我的許可問題,而且幫助我管理應用中的其他資源。 – 2010-04-03 16:37:16

+0

只是注意到我沒有在最後的try catch塊中放置StreamReader ... – 2010-04-03 17:59:30

0

猜測,問題是不同的用戶不能訪問爲第一個用戶存儲的密鑰(請注意:我不是加密專家)。