2009-12-15 13 views
5

मैं एक एक्सएमएल फ़ाइल एन्क्रिप्ट/डिक्रिप्ट करने की कोशिश कर रहा हूं। मुझे एन्क्रिप्टिंग के लिए यह नमूना मिला लेकिन मुझे नहीं पता कि कैसे डिक्रिप्ट करना है? कोई उपाय? धन्यवाद!किसी XML फ़ाइल को एन्क्रिप्ट/डिक्रिप्ट कैसे करें?

 // Load this XML file 
     System.Xml.XmlDocument myDoc = new System.Xml.XmlDocument(); 
     myDoc.Load(@"c:\persons.xml"); 
     // Get a specified element to be encrypted 
     System.Xml.XmlElement element = myDoc.GetElementsByTagName("Persons")[0] as System.Xml.XmlElement; 

     // Create a new TripleDES key. 
     System.Security.Cryptography.TripleDESCryptoServiceProvider tDESkey = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); 

     // Form a Encrypted XML with the Key 
     System.Security.Cryptography.Xml.EncryptedXml encr = new System.Security.Cryptography.Xml.EncryptedXml(); 
     encr.AddKeyNameMapping("Deskey", tDESkey); 

     // Encrypt the element data 
     System.Security.Cryptography.Xml.EncryptedData ed = encr.Encrypt(element, "Deskey"); 

     // Replace the existing data with the encrypted data 
     System.Security.Cryptography.Xml.EncryptedXml.ReplaceElement(element, ed, false); 

     // saves the xml file with encrypted data 
     myDoc.Save(@"c:\encryptedpersons.xml"); 

लेकिन मुझे नहीं पता कि मैं इसे कैसे डिक्रिप्ट करूंगा? कोई विचार? धन्यवाद!

+2

जाँच करें: http://stackoverflow.com/questions/1086049/c-encrypt-an-xml-file – PRR

+0

क्या आप मदद नीचे दिए गए उत्तर के किसी भी? यदि आप ऐसा स्वीकार कर सकते हैं तो सवाल अब "अनुत्तरित" नहीं होगा? –

उत्तर

8

कुछ इस तरह:

public static class Encryption 
{ 
    private const string InitVector = "T=A4rAzu94ez-dra"; 
    private const int KeySize = 256; 
    private const int PasswordIterations = 1000; //2; 
    private const string SaltValue = "[email protected]=ner5sW&h59_Xe9P2za-eFr2fa&[email protected][email protected]"; 

    public static string Decrypt(string encryptedText, string passPhrase) 
    { 
     byte[] encryptedTextBytes = Convert.FromBase64String(encryptedText); 
     byte[] initVectorBytes = Encoding.UTF8.GetBytes(InitVector); 
     byte[] passwordBytes = Encoding.UTF8.GetBytes(passPhrase); 
     string plainText; 
     byte[] saltValueBytes = Encoding.UTF8.GetBytes(SaltValue); 

     Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passwordBytes, saltValueBytes, PasswordIterations); 
     byte[] keyBytes = password.GetBytes(KeySize/8); 

     RijndaelManaged rijndaelManaged = new RijndaelManaged { Mode = CipherMode.CBC }; 

     try 
     { 
      using (ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(keyBytes, initVectorBytes)) 
      { 
       using (MemoryStream memoryStream = new MemoryStream(encryptedTextBytes)) 
       { 
        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) 
        { 
         //TODO: Need to look into this more. Assuming encrypted text is longer than plain but there is probably a better way 
         byte[] plainTextBytes = new byte[encryptedTextBytes.Length]; 

         int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); 
         plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); 
        } 
       } 
      } 
     } 
     catch (CryptographicException) 
     { 
      plainText = string.Empty; // Assume the error is caused by an invalid password 
     } 

     return plainText; 
    } 

    public static string Encrypt(string plainText, string passPhrase) 
    { 
     string encryptedText; 
     byte[] initVectorBytes = Encoding.UTF8.GetBytes(InitVector); 
     byte[] passwordBytes = Encoding.UTF8.GetBytes(passPhrase); 
     byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); 
     byte[] saltValueBytes = Encoding.UTF8.GetBytes(SaltValue); 

     Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(passwordBytes, saltValueBytes, PasswordIterations); 
     byte[] keyBytes = password.GetBytes(KeySize/8); 

     RijndaelManaged rijndaelManaged = new RijndaelManaged {Mode = CipherMode.CBC}; 

     using (ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(keyBytes, initVectorBytes)) 
     { 
      using (MemoryStream memoryStream = new MemoryStream()) 
      { 
       using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) 
       { 
        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); 
        cryptoStream.FlushFinalBlock(); 

        byte[] cipherTextBytes = memoryStream.ToArray(); 
        encryptedText = Convert.ToBase64String(cipherTextBytes); 
       } 
      } 
     } 

     return encryptedText; 
    } 
} 

संपादित करें:

सानी Huttunen ने बताया कि मेरी स्थिर कार्यान्वयन के ऊपर एक गंभीर प्रदर्शन मुद्दा अगर आप एक ही पासवर्ड का उपयोग कर डेटा के कई टुकड़े को एनक्रिप्ट कर दिया जाएगा । आप यहाँ इसके बारे में अधिक पढ़ सकते हैं: http://jmpstart.wordpress.com/2009/09/29/proper-use-of-rfc2898derivebytes/

संपादित करें: एक गैर स्थिर कार्यान्वयन है कि और अधिक कुशल यदि आप एक ही पासवर्ड (~ 32ms मूल ~ 1ms नया) का उपयोग कर कई encryptions/decryptions प्रदर्शन करने की जरूरत है।

public class SimpleEncryption 
{ 
    #region Constructor 
    public SimpleEncryption(string password) 
    { 
     byte[] passwordBytes = Encoding.UTF8.GetBytes(password); 
     byte[] saltValueBytes = Encoding.UTF8.GetBytes(SaltValue); 

     _DeriveBytes = new Rfc2898DeriveBytes(passwordBytes, saltValueBytes, PasswordIterations); 
     _InitVectorBytes = Encoding.UTF8.GetBytes(InitVector); 
     _KeyBytes = _DeriveBytes.GetBytes(32); 
    } 
    #endregion 

    #region Private Fields 
    private readonly Rfc2898DeriveBytes _DeriveBytes; 
    private readonly byte[] _InitVectorBytes; 
    private readonly byte[] _KeyBytes; 
    #endregion 

    private const string InitVector = "T=A4rAzu94ez-dra"; 
    private const int PasswordIterations = 1000; //2; 
    private const string SaltValue = "[email protected]=ner5sW&h59_Xe9P2za-eFr2fa&[email protected][email protected]"; 

    public string Decrypt(string encryptedText) 
    { 
     byte[] encryptedTextBytes = Convert.FromBase64String(encryptedText); 
     string plainText; 

     RijndaelManaged rijndaelManaged = new RijndaelManaged { Mode = CipherMode.CBC }; 

     try 
     { 
      using (ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(_KeyBytes, _InitVectorBytes)) 
      { 
       using (MemoryStream memoryStream = new MemoryStream(encryptedTextBytes)) 
       { 
        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) 
        { 
         //TODO: Need to look into this more. Assuming encrypted text is longer than plain but there is probably a better way 
         byte[] plainTextBytes = new byte[encryptedTextBytes.Length]; 

         int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); 
         plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); 
        } 
       } 
      } 
     } 
     catch (CryptographicException exception) 
     { 
      plainText = string.Empty; // Assume the error is caused by an invalid password 
     } 

     return plainText; 
    } 

    public string Encrypt(string plainText) 
    { 
     string encryptedText; 
     byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); 

     RijndaelManaged rijndaelManaged = new RijndaelManaged {Mode = CipherMode.CBC}; 

     using (ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(_KeyBytes, _InitVectorBytes)) 
     { 
      using (MemoryStream memoryStream = new MemoryStream()) 
      { 
       using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) 
       { 
        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); 
        cryptoStream.FlushFinalBlock(); 

        byte[] cipherTextBytes = memoryStream.ToArray(); 
        encryptedText = Convert.ToBase64String(cipherTextBytes); 
       } 
      } 
     } 

     return encryptedText; 
    } 
} 
+1

मानक एक्सएमएल एन्क्रिप्शन पर इस घर से उगाए गए समाधान का कोई लाभ/नुकसान? – dtb

+0

एक फायदा यह होगा कि आप किसी भी स्ट्रिंग को एन्क्रिप्ट/डिक्रिप्ट करने के लिए इसका उपयोग कर सकते हैं। ईमानदारी से मैं उपरोक्त विधियों के बारे में नहीं जानता। मेरे उदाहरण के लिए पासवर्ड या पासफ्रेज़ की आवश्यकता होती है जो पहली नज़र में मैंने उपर्युक्त उदाहरण में नहीं देखा था। इसके अलावा उपर्युक्त उदाहरण केवल एक्सएमएल तत्वों को एन्क्रिप्ट करना प्रतीत होता है, न कि संपूर्ण एक्सएमएल (केवल उपयोग किया जा सकता है) –

+2

आपके समाधान में एक प्रदर्शन त्रुटि है। आप इसके बारे में यहां पढ़ सकते हैं: http://jmpstart.wordpress.com/2009/09/29/proper-use-of-rfc2898derivebytes/ –

4

complete example on MSDN है, हालांकि आरएसए का उपयोग करता है और ट्रिपलडेस नहीं है।

संबंधित मुद्दे