Ejemplo n.º 1
0
        static void TestEncryptXML()
        {
            RSA rsaKey = GTXXmlCrypt.CreateRSAAlgorithm("rsakey container");

            GTXXmlCrypt.EncryptXmlFile("TestUserSetting.xml", "User", false, rsaKey, "");

            //GTXXmlCrypt.DecryptXmlFile("TestUserSetting.xml", rsaKey, "");
        }
Ejemplo n.º 2
0
        private static void TestSymmetricAlgorithm(String filePath)
        {
            Console.WriteLine("Select the encrypt/decrypt algorithm: (Default is DES)");
            GTXXmlCrypt.SymmetricAlgTypes salTypeTemp = GTXXmlCrypt.SymmetricAlgTypes.DES;
            for (; salTypeTemp <= GTXXmlCrypt.SymmetricAlgTypes.TripleDES; ++salTypeTemp)
            {
                Console.WriteLine("{0} is {1}", (int)salTypeTemp, salTypeTemp.ToString());
            }
            // Read the symmetric algorithm command
            String algString = Console.ReadLine();

            GTXXmlCrypt.SymmetricAlgTypes salType = GTXXmlCrypt.SymmetricAlgTypes.DES;
            try
            {
                salType = (GTXXmlCrypt.SymmetricAlgTypes)Convert.ToInt32(algString);
                if (salType <= GTXXmlCrypt.SymmetricAlgTypes.DES &&
                    salType >= GTXXmlCrypt.SymmetricAlgTypes.TripleDES)
                {
                    salType = GTXXmlCrypt.SymmetricAlgTypes.DES;
                }
            }
            catch (System.Exception ex)
            {
                salType = GTXXmlCrypt.SymmetricAlgTypes.DES;
            }
            // Read the node which we want to encrypt/decrypt
            Console.WriteLine("Input the XML element node:");
            String elementName = Console.ReadLine();

            Console.WriteLine("Encrypt node element or its content, 0: Element, 1: Only content (Default is Element)");
            String             content  = Console.ReadLine();
            bool               bContent = content.Equals("1") ? true : false;
            SymmetricAlgorithm sal      = GTXXmlCrypt.CreateSymmetricAlgorithm(salType);

            if (GTXXmlCrypt.EncryptXmlFile(filePath, elementName, bContent, sal, "abc"))
            {
                // Print the encrypted XML to console
                Console.WriteLine("Encrypt XML with {0} algorithm Succeed!", salType);
                PrintXml(filePath, PrintTypes.PrintEncrypt);
                if (GTXXmlCrypt.DecryptXmlFile(filePath, sal, /*"abc"*/ ""))
                {
                    // Print the decrypted XML to console
                    Console.WriteLine("Decrypt XML with {0} algorithm Succeed!", salType);
                    PrintXml(filePath, PrintTypes.PrintDecrypt);
                }
                else
                {
                    Console.WriteLine("Decrypt with {0} algorithm Failed!", salType);
                }
            }
            else
            {
                Console.WriteLine("Encrypt with {0} algorithm Failed!", salType);
            }
        }
Ejemplo n.º 3
0
        private static void TestAsymmetricAlgorithm(String filePath)
        {
            String keyContainerName = "rsakey container";
            RSA    rsaKey           = GTXXmlCrypt.CreateRSAAlgorithm(keyContainerName);

            try
            {
                // Read the node which we want to encrypt/decrypt
                Console.WriteLine("Input the XML element node:");
                String elementName = Console.ReadLine();
                Console.WriteLine("Encrypt node element or its content, 0: Element, 1: Only content (Default is Element)");
                String content  = Console.ReadLine();
                bool   bContent = content.Equals("1") ? true : false;
                if (GTXXmlCrypt.EncryptXmlFile(filePath, elementName, bContent, rsaKey, "" /*"rsakey"*/))
                {
                    // Print the encrypted XML to console
                    Console.WriteLine("Encrypt XML with RSA algorithm Succeed!");
                    PrintXml(filePath, PrintTypes.PrintEncrypt);
                    if (GTXXmlCrypt.DecryptXmlFile(filePath, rsaKey, "" /*"rsakey"*/))
                    {
                        PrintXml(filePath, PrintTypes.PrintDecrypt);
                        Console.WriteLine("Decrypt XML with RSA algorithm Succeed!");
                    }
                    else
                    {
                        Console.WriteLine("Decrypt XML with RSA algorithm Failed!");
                    }
                }
                else
                {
                    Console.WriteLine("Encrypt xml with RSA algorithm Failed!");
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                rsaKey.Clear();
            }
        }