/// <summary> /// save Data to File /// </summary> /// <param name="FileName">the FileName to use for save</param> /// <param name="FileVersion">optional File-Version to apply - use "" to apply DLL-Version</param> /// <returns>TRUE on success, FALSE on error</returns> private bool _SaveToFile(string FileName, string FileVersion) { // something set in FileName? Exit if not... if (FileName.Trim() == "") { return(false); } DDPro.Software.Net20.Library.Rijndael _cipher = new DDPro.Software.Net20.Library.Rijndael(System.Text.Encoding.UTF8); // get dataset-data string _innerXML = _int_DataSet.GetXml(); StringBuilder _FileCont = new StringBuilder(); _FileCont.AppendLine("CUX = Ciphered User XML"); // set File-Version by Assembly-Number if (FileVersion == "") // no Version specified - use DLL-Version { _FileCont.AppendLine("Version = " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Major.ToString() + "." + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Minor.ToString()); } else // Version is specified { _FileCont.AppendLine("Version = " + FileVersion); } _FileCont.AppendLine("Index = " + _set_IdxMode.ToString()); // specify used index _FileCont.AppendLine("-| start |-"); // set StartData-Tag // start Data-Block string _cDat = _cipher.EasyEncrypt(_innerXML, _set_DataCipherPhrase); // add linebreak every 60 signs in Data long _strLen = _cDat.Length; double _runCycles = (_strLen / 60); int _runs = Convert.ToInt32(Math.Floor(_runCycles)); for (long i = 0; i <= _runs; i++) { if ((i * 60) > int.MaxValue) // if not "runable" cycle { return(false); // exit on too many cycles } if (i == _runs) // last cycle - take the rest { _FileCont.AppendLine(_cDat.Substring(Convert.ToInt32(i * 60))); // add 60 signs } else // regular cycle - write 60 signs { _FileCont.AppendLine(_cDat.Substring(Convert.ToInt32(i * 60), 60)); // add 60 signs } } // end Data-Block _FileCont.AppendLine("-| end |-"); // _FileCont.AppendLine( "" ); // 4 debug // _FileCont.AppendLine( _cDat ); // 4 debug // write data to file if (!_fileWriter(FileName, _FileCont.ToString())) { return(false); } // everything has worked, data is saved _int_DataSet.AcceptChanges(); // remind save return(true); }
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..."); }
/// <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); }