Element Encryption

Follow

Comments

1 comment

  • Avatar
    Guy Swartwout

    I had trouble in .NET until I used openssl from one of our Linux boxes to create my key pair. Then I used OpenSSLKey (http://www.jensign.com/opensslkey/) to read the private key. Here's a little sample console app that decrypts an XML file downloaded from iFormBuilder.

    using JavaScience;
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Xml;

    namespace Keys
    {
    class Program
    {
    static void Main(string[] args)
    {
    string fileWithEncryptedFields = @"C:\Temp\keys\dataXML.php.xml";
    string decryptedFile = @"C:\Temp\keys\fixed.xml";
    string[] encryptedFieldNames = new string[] { "encrypted" };

            decryptData(fileWithEncryptedFields, decryptedFile, encryptedFieldNames);
        }
    
        static private void decryptData(string fileWithEncryptedFields, string decryptedFile, string[] encryptedFieldNames)
        {
            string privateKeyFile = @"D:\Transfer\keys\private_key.pem";
    
            RSACryptoServiceProvider rsa = GetRSACryptoServiceProvider(privateKeyFile);
    
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(fileWithEncryptedFields);
    
            foreach (string encryptedFieldName in encryptedFieldNames)
            {
                XmlNodeList encryptedNodes = xDoc.GetElementsByTagName(encryptedFieldName);
    
                foreach (XmlNode encryptedNode in encryptedNodes)
                {
    
                    byte[] encryptedBytes = Convert.FromBase64String(encryptedNode.InnerText);
                    byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, true);
                    encryptedNode.InnerText = System.Text.Encoding.Default.GetString(decryptedBytes);
                }
            }
    
            xDoc.Save(decryptedFile);
        }
    
        static private RSACryptoServiceProvider GetRSACryptoServiceProvider(string privateKeyFile)
        {
            FileInfo fi = new FileInfo(privateKeyFile);
    
            string overall = "";
            using (StreamReader sr = fi.OpenText())
            {
                overall = sr.ReadToEnd();
                sr.Close();
            }
            using (StreamReader sr = fi.OpenText())
            {
                string rsaPrivKeyString = "";
    
                string tmp = sr.ReadLine().Trim();
                while (!sr.EndOfStream && (tmp.StartsWith("-") || tmp.EndsWith("-")))
                    tmp = sr.ReadLine().Trim();
    
                while (!sr.EndOfStream && !tmp.StartsWith("-") && !tmp.EndsWith("-"))
                {
                    rsaPrivKeyString += tmp;
                    tmp = sr.ReadLine().Trim();
                }
    
                sr.Close();
    
                byte[] data = Convert.FromBase64String(rsaPrivKeyString);
    
                RSACryptoServiceProvider rsa = opensslkey.DecodeRSAPrivateKey(data);
    
                return rsa;
            }
        }
    }
    

    }

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk