Exemplo n.º 1
0
        public void test_RijndaelAES()
        {
            DDPro.Software.Net20.Library.Rijndael _testObject = new DDPro.Software.Net20.Library.Rijndael(Encoding.Default);
            Assert.NotNull(_testObject, "AES-Class for Test test_RijndaelAES could no be created...");
            string _pw  = "Pa$$Phra53!";
            string _msg = "Message to cipher...";
            string _tmp = "";

            // check one cycle
            _tmp = _testObject.EasyEncrypt(_msg, _pw);
            Assert.AreEqual(_msg, _testObject.EasyDecrypt(_tmp, _pw), "test_RijndaelAES: encrypted String (1 cycle) not decrypted to original...");
            Assert.AreNotEqual(_msg, _testObject.EasyDecrypt(_tmp, "wrongPW"), "test_RijndaelAES: encrypted String (1 cycle) decrypted to original with wrong password --> error...");

            // check five cycles
            _tmp = _testObject.EasyEncrypt(_msg, _pw, 5);
            Assert.AreNotEqual(_msg, _testObject.EasyDecrypt(_tmp, _pw, 4), "test_RijndaelAES: encrypted String (5 cycles) decrypted to original with 4 cycles --> error...");
            Assert.AreEqual(_msg, _testObject.EasyDecrypt(_tmp, _pw, 5), "test_RijndaelAES: encrypted String (5 cycles) not decrypted to original...");
        }
Exemplo n.º 2
0
        /// <summary>
        /// (initial) load Data from File / DataSource
        /// </summary>
        /// <returns>TRUE on success, FALSE on error</returns>
        private bool _loadData(string FileName)
        {
            // something set in FileName? Exit if not...
            if (FileName.Trim() == "")
            {
                return(false);
            }
            // read Data from file
            string _fileContent;

            try
            {
                _fileContent = System.IO.File.ReadAllText(FileName, System.Text.Encoding.UTF8);
            }
            catch (Exception)
            {
                return(false);
            }
            // read Data from file
            StringBuilder _cData  = new StringBuilder();  // Element for ciphered Data
            IndexColumn   _fIndex = IndexColumn.Username; // Username as Default-index

            _set_Version = "";                            // init with default-version
            // split into lines
            string[] _lines = _fileContent.Split(Environment.NewLine.ToCharArray());
            // cleanup empty lines
            List <string> _cleaned = new List <string>();

            foreach (string _rawLine in _lines)
            {
                if (_rawLine.Trim() != "")
                {
                    _cleaned.Add(_rawLine);
                }
            }
            // check Version
            bool _dataActive = false; // reminder for active data-block

            foreach (string _line in _cleaned)
            {
                // detect headline
                if (_line.Trim().ToLower().StartsWith("cux"))
                {
                    // nothing for the moment...
                }
                // detect version
                if (_line.Trim().ToLower().StartsWith("version"))
                {
                    if (!_CheckFileVersion(_line.Trim())) // if invalid version
                    {
                        return(false);                    // exit
                    }
                }
                // detect index
                if (_line.Trim().ToLower().StartsWith("index"))
                {
                    _fIndex = _GetFileIndex(_line.Trim());
                }

                // detect end
                if (_line.Trim().ToLower() == "-| end |-")
                {
                    _dataActive = false;
                }
                // save data if active
                if (_dataActive)
                {
                    _cData.Append(_line.Trim());
                }
                // detect start
                if (_line.Trim().ToLower() == "-| start |-")
                {
                    _dataActive = true;
                }
            }
            // decrypt data
            DDPro.Software.Net20.Library.Rijndael _cipher = new DDPro.Software.Net20.Library.Rijndael(System.Text.Encoding.UTF8);
            string _xmlString;

            try
            {
                _xmlString = _cipher.EasyDecrypt(_cData.ToString(), _set_DataCipherPhrase);
            }
            catch (Exception)
            {
                return(false);
            }
            // initialize the internal DataSet by used Index-Column(s)
            _initializeDataSet(_fIndex);
            // apply XML to dataset
            try
            {
                System.IO.TextReader _dsData = new System.IO.StringReader(_xmlString);
                _int_DataSet.ReadXml(_dsData);
                _int_DataSet.AcceptChanges();
            }
            catch (Exception)
            {
                return(false);
            }
            // everything was ok
            return(true);
        }