Esempio n. 1
0
        /// <summary>
        /// Takes an encrypted key in the form of a string as input
        /// and applies the DES encryption algorithm to produce an
        /// unencrypted byte array
        /// </summary>
        /// <param name="strEncryptedKey">Encrypted security key</param>
        /// <returns></returns>
        //  Revision History
        //  MM/DD/YY Who Version Issue# Description
        //  -------- --- ------- ------ -------------------------------------------
        //  06/21/13 RCG 2.70.77        Copied from CENTRON_AMI

        private byte[] DecryptHANKey(string strEncryptedKey)
        {
            int Discarded;

            SecureDataStorage        DataStorage     = null;
            DESCryptoServiceProvider Crypto          = null;
            MemoryStream             EncryptedStream = null;
            MemoryStream             DecryptedStream = null;
            string       strDecryptedKey             = null;
            StreamReader sr = null;

            try
            {
                DataStorage = new SecureDataStorage(SecureDataStorage.DEFAULT_LOCATION);
                Crypto      = new DESCryptoServiceProvider();
                Crypto.Key  = DataStorage.RetrieveSecureData(SecureDataStorage.ZIGBEE_KEY_ID);
                Crypto.IV   = DataStorage.RetrieveSecureData(SecureDataStorage.ZIGBEE_IV_ID);


                byte[] EncryptedKey = HexEncoding.GetBytes(strEncryptedKey, out Discarded);

                //Create a memory stream to the passed buffer.
                EncryptedStream = new MemoryStream(EncryptedKey);
                DecryptedStream = new MemoryStream();

                Encryption.DecryptData(Crypto, EncryptedStream, DecryptedStream);

                //We must rewind the stream
                DecryptedStream.Position = 0;

                // Create a StreamReader for reading the stream.
                sr = new StreamReader(DecryptedStream);

                // Read the stream as a string.
                strDecryptedKey = sr.ReadLine();
            }
            finally
            {
                // Close the streams.
                //Closing sr also closes DecryptedStream
                if (null != sr)
                {
                    sr.Close();
                }
                else
                {
                    DecryptedStream.Close();
                }
                EncryptedStream.Close();
                Crypto.Dispose();
            }
            //Transform the string into a byte array
            return(HexEncoding.GetBytes(strDecryptedKey, out Discarded));
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the encryption algorithm to use.
        /// </summary>
        /// <returns>The encryption algorithm</returns>
        //  Revision History
        //  MM/DD/YY Who Version Issue# Description
        //  -------- --- ------- ------ ---------------------------------------------
        //  11/10/09 RCG 2.30.16	    Created

        protected override SymmetricAlgorithm CreateEncryptionAlgorithm()
        {
            TripleDES         Algorithm   = TripleDES.Create();
            SecureDataStorage DataStorage = new SecureDataStorage(SecureDataStorage.DEFAULT_LOCATION);

            Algorithm.KeySize = 192;
            Algorithm.Key     = DataStorage.RetrieveSecureData(SecureDataStorage.RMA_SIGNED_AUTH_KEY_ID);
            Algorithm.IV      = DataStorage.RetrieveSecureData(SecureDataStorage.RMA_SIGNED_AUTH_IV_ID);

            return(Algorithm);
        }
Esempio n. 3
0
        /// <summary>
        /// Saves the current XML data to the specified file. All XML data in the file will be overwritten.
        /// </summary>
        /// <param name="strFilePath"> FilePath to save to. If "" is passed in for strFilePath, then m_strXMLFileName
        ///  will be used for the path. Otherwise the default file name will be appended to the strFilePath</param>
        /// <returns>Whether or not the save was successful.</returns>
        // Revision History
        // MM/DD/YY who Version Issue# Description
        // -------- --- ------- ------ ---------------------------------------------
        // 05/27/08 jrf 1.50.28        Created
        //
        public override bool SaveSettings(string strFilePath)
        {
            bool         bReturn             = false;
            string       strTemp             = strFilePath;
            XmlDocument  xmldocTemp          = new XmlDocument();
            MemoryStream DecryptedStream     = new MemoryStream();
            FileStream   EncryptedStream     = null;
            Rijndael     EncryptionAlgorithm = null;

            try
            {
                if (1 > strFilePath.Length)
                {
                    strTemp = m_strXMLFileName;
                }
                else
                {
                    strTemp = strFilePath;
                }

                EncryptedStream     = new FileStream(strTemp, FileMode.Create);
                EncryptionAlgorithm = Rijndael.Create();

                SecureDataStorage DataStorage = new SecureDataStorage(SecureDataStorage.DEFAULT_LOCATION);

                //Set the key and IV properties
                EncryptionAlgorithm.Key = DataStorage.RetrieveSecureData(SecureDataStorage.REPLICA_KEY_ID);
                EncryptionAlgorithm.IV  = DataStorage.RetrieveSecureData(SecureDataStorage.REPLICA_IV_ID);

                Save(DecryptedStream);

                //Need to rewind stream before encrypting
                DecryptedStream.Position = 0;

                //Encrypt the data and write to the file
                Encryption.EncryptData(EncryptionAlgorithm, DecryptedStream, EncryptedStream);

                if (null != xmldocTemp)
                {
                    //Need to rewind stream before loading
                    DecryptedStream.Position = 0;

                    xmldocTemp.Load(DecryptedStream);

                    if (null != xmldocTemp.SelectSingleNode(DEFAULT_XML_ROOT_NODE))
                    {
                        bReturn = true;
                    }

                    xmldocTemp = null;
                }
            }
            catch
            {
                bReturn = false;
            }
            finally
            {
                if (null != EncryptionAlgorithm)
                {
                    EncryptionAlgorithm.Dispose();
                }

                if (null != DecryptedStream)
                {
                    DecryptedStream.Close();
                }

                if (null != EncryptedStream)
                {
                    EncryptedStream.Close();
                }
            }

            return(bReturn);
        }