Verschlüsselung

theplake

Erfahrenes Mitglied
RijndaelManaged Verschlüsselung

Hi Leute

Folgendes Problem.

Hab mir nen login gebaut mit username und PW abfrage.Diese stehen in einer .xml textdatei.

Die Textdatei ist mit RijndaelManaged verschlüsselt.Von .net.

Im login Programm ist ein decrypter funktion.

Alles klappt tadellos auf dem Rechner wo ich es programmiert habe wenn ich allerdings das Proggi auf einem anderen Rechner abspielen will sagt er nur ungültige Daten.

Meine Frage ist jetzt muss ich auf jedem Rechner wo ich das Proggi starten will noch irgendwo nen Key eintragen?

Kenn mich in der Hinsicht noch nicht so aus mit der Verschlüsselung.
 
Zuletzt bearbeitet:
Hast du vielleicht ein bisschen Code? Verwendest du irgendwelche Keys, welche auf einem System einzigartig sind?
 
Ich benutze eigentlich die Standart verschlüsselung von msdn

Hier die encrypt funktion:
Code:
public: static void Encrypt(XmlDocument ^Doc, String ^ElementToEncrypt, String ^EncryptionElementID, RSA ^Alg, String ^KeyName)
    {
        // Check the arguments.
        if (Doc == nullptr)
            throw gcnew ArgumentNullException("Doc");
        if (ElementToEncrypt == nullptr)
            throw gcnew ArgumentNullException("ElementToEncrypt");
        if (EncryptionElementID == nullptr)
            throw gcnew ArgumentNullException("EncryptionElementID");
        if (Alg == nullptr)
            throw gcnew ArgumentNullException("Alg");
        if (KeyName == nullptr)
            throw gcnew ArgumentNullException("KeyName");

        ////////////////////////////////////////////////
        // Find the specified element in the XmlDocument
        // object and create a new XmlElemnt object.
        ////////////////////////////////////////////////
        //XmlElement elementToEncrypt = Doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;
		XmlElement ^elementToEncrypt = dynamic_cast<XmlElement^>(Doc->GetElementsByTagName(ElementToEncrypt)[0]);

        // Throw an XmlException if the element was not found.
        if (elementToEncrypt == nullptr)
        {
            throw gcnew XmlException("The specified element was not found");

        }
        RijndaelManaged ^sessionKey = nullptr;

        try
        {
            //////////////////////////////////////////////////
            // Create a new instance of the EncryptedXml class
            // and use it to encrypt the XmlElement with the
            // a new random symmetric key.
            //////////////////////////////////////////////////

            // Create a 256 bit Rijndael key.
            sessionKey = gcnew RijndaelManaged();
            sessionKey->KeySize = 256;

            EncryptedXml ^eXml = gcnew EncryptedXml();

			array<System::Byte> ^encryptedElement = eXml->EncryptData(elementToEncrypt, sessionKey, false);
            //byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false);
            ////////////////////////////////////////////////
            // Construct an EncryptedData object and populate
            // it with the desired encryption information.
            ////////////////////////////////////////////////

            EncryptedData ^edElement = gcnew EncryptedData();
			edElement->Type = EncryptedXml::XmlEncElementUrl;
            edElement->Id = EncryptionElementID;
            // Create an EncryptionMethod element so that the
            // receiver knows which algorithm to use for decryption.

			edElement->EncryptionMethod = gcnew EncryptionMethod(EncryptedXml::XmlEncAES256Url);
            // Encrypt the session key and add it to an EncryptedKey element.
            EncryptedKey ^ek = gcnew EncryptedKey();

			array<System::Byte> ^encryptedKey = EncryptedXml::EncryptKey(sessionKey->Key, Alg, false);
           // byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false);

            ek->CipherData = gcnew CipherData(encryptedKey);

			ek->EncryptionMethod = gcnew EncryptionMethod(EncryptedXml::XmlEncRSA15Url);

            // Create a new DataReference element
            // for the KeyInfo element.  This optional
            // element specifies which EncryptedData
            // uses this key.  An XML document can have
            // multiple EncryptedData elements that use
            // different keys.
            DataReference ^dRef = gcnew DataReference();

            // Specify the EncryptedData URI.
            dRef->Uri = "#" + EncryptionElementID;

            // Add the DataReference to the EncryptedKey.
            ek->AddReference(dRef);
            // Add the encrypted key to the
            // EncryptedData object.

            edElement->KeyInfo->AddClause(gcnew KeyInfoEncryptedKey(ek));
            // Set the KeyInfo element to specify the
            // name of the RSA key.


            // Create a new KeyInfoName element.
            KeyInfoName ^kin = gcnew KeyInfoName();

            // Specify a name for the key.
            kin->Value = KeyName;

            // Add the KeyInfoName element to the
            // EncryptedKey object.
            ek->KeyInfo->AddClause(kin);
            // Add the encrypted element data to the
            // EncryptedData object.
			edElement->CipherData->CipherValue = encryptedElement;
            ////////////////////////////////////////////////////
            // Replace the element from the original XmlDocument
            // object with the EncryptedData element.
            ////////////////////////////////////////////////////
			EncryptedXml::ReplaceElement(elementToEncrypt, edElement, false);
        }
        catch (Exception ^e)
        {
            // re-throw the exception.
            throw e;
        }
        finally
        {
            if (sessionKey != nullptr)
            {
                sessionKey->Clear();
            }

        }

    }

und die decrypt:
Code:
public: static void Decrypt(XmlDocument ^Doc, RSA ^Alg, String ^KeyName)
    {
        // Check the arguments.  
        if (Doc == nullptr)
            throw gcnew ArgumentNullException("Doc");
        if (Alg == nullptr)
            throw gcnew ArgumentNullException("Alg");
        if (KeyName == nullptr)
            throw gcnew ArgumentNullException("KeyName");

        // Create a new EncryptedXml object.
        EncryptedXml ^exml = gcnew EncryptedXml(Doc);

        // Add a key-name mapping.
        // This method can only decrypt documents
        // that present the specified key name.
		exml->AddKeyNameMapping(KeyName, Alg);

		while (Doc->GetElementsByTagName("EncryptedData", EncryptedXml::XmlEncNamespaceUrl)->Count > 0)
    {
        // Decrypt the element.
        exml->DecryptDocument();
    }

    }



};

aufrufen tu ich das ganze so:
Code:
 XmlDocument ^doc = gcnew XmlDocument();
					 doc->Load("cfg.xml");
							 // Create a new CspParameters object to specify
					// a key container.
					CspParameters ^cspParams = gcnew CspParameters();
					cspParams->KeyContainerName = "XML_ENC_RSA_KEY";

					// Create a new RSA key and save it in the container.  This key will encrypt
					// a symmetric key, which will then be encryped in the XML document.
					RSACryptoServiceProvider ^rsaKey = gcnew RSACryptoServiceProvider(cspParams);

					module::Decrypt(doc, rsaKey, "rsaKey");
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück