public void Load() { if (!File.Exists(FileName)) { throw new FileNotFoundException($"Could not find SHN File {FileName}."); } byte[] data; using (var file = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { var reader = new BinaryReader(file); CryptHeader = reader.ReadBytes(32); var Length = reader.ReadInt32() - 36; //minus int + header data = reader.ReadBytes(Length); FileCrypto.Crypt(data, 0, Length); //File.WriteAllBytes("Output.dat", data); //debug purpose } using (var stream = new MemoryStream(data)) { var reader = new SHNReader(stream); Header = reader.ReadUInt32(); RecordCount = reader.ReadUInt32(); DefaultRecordLength = reader.ReadUInt32(); ColumnCount = reader.ReadUInt32(); GenerateColumns(reader); GenerateRows(reader); } data = null; }
static void Main(string[] args) { string filepath = null; FileInfo fileinfo; Console.Write("Please enter the path to the file: "); filepath = Console.ReadLine(); try { fileinfo = FileCrypto.GetFileInfo(filepath); if (fileinfo.Encrypted) { Console.WriteLine("The file \"" + filepath + "\" is encrypted using key " + fileinfo.KeyId + "."); } else { Console.WriteLine("The file \"" + filepath + "\" is not encrypted."); } } catch (SdkException e) { Console.WriteLine(e); } Console.Write("\nPress Enter to continue..."); Console.ReadKey(); }
private void RegDB_Load(object sender, EventArgs e) { string str = Path.Combine(VMGlobal.ProfilePath, VMGlobal.ProfileFile); if (Directory.Exists(VMGlobal.ProfilePath)) { try { DBProfileData dBProfileDatum = (DBProfileData)FileCrypto.LoadConfig(str); this.chk_LocalDB.Checked = dBProfileDatum.IsLocalDB; this.chk_Security.Checked = dBProfileDatum.PersistSecurityInfo; this.DataSource.Text = dBProfileDatum.DataSource; this.Catalog.Text = dBProfileDatum.Catalog; this.UserID.Text = dBProfileDatum.UserId; vTextBox pwd1 = this.Pwd1; vTextBox pwd2 = this.Pwd2; string password = dBProfileDatum.Password; string str1 = password; pwd2.Text = password; pwd1.Text = str1; } catch { } } }
private CamProfile ReadAssetProfile(string DriveID) { CamProfile camProfile = new CamProfile(); if (File.Exists(Path.Combine(DriveID, "C3Sentinel.Dat"))) { camProfile = (CamProfile)FileCrypto.LoadConfig(Path.Combine(DriveID, "C3Sentinel.Dat")); } return(camProfile); }
public CamProfile ReadAssetProfile(string DriveID) { CamProfile camProfile = new CamProfile(); if (IsProfile(DriveID)) { camProfile = (CamProfile)FileCrypto.LoadConfig(Path.Combine(DriveID, "C3Sentinel.Dat")) ?? new CamProfile(); } return(camProfile); }
static void DecryptString() { byte[] key, iv; getEnryptData(out key, out iv); Console.WriteLine("String to Decrypt? (In base64)"); string enctext = Console.ReadLine(); string plaintext = FileCrypto.decryptString(enctext, Convert.ToBase64String(key), Convert.ToBase64String(iv)); Console.WriteLine("Plaintext: "); Console.WriteLine(plaintext); }
private void InternalSave(string path) { try { isSaving = true; UpdateDefaultRecordLength(); byte[] content; using (MemoryStream encrypted = new MemoryStream()) { SHNWriter writer = new SHNWriter(encrypted); writer.Write(this.Header); writer.Write((uint)this.Rows.Count); writer.Write(this.DefaultRecordLength); writer.Write((uint)this.Columns.Count); WriteColumns(writer); WriteRows(writer); content = new byte[encrypted.Length]; encrypted.Seek(0, SeekOrigin.Begin); encrypted.Read(content, 0, content.Length); } FileCrypto.Crypt(content, 0, content.Length); using (FileStream final = File.Create(path)) { BinaryWriter writer = new BinaryWriter(final); writer.Write(CryptHeader); writer.Write((int)(content.Length + 36)); writer.Write(content); } this.FileName = path; if (OnSaveFinished != null) { OnSaveFinished.Invoke(this); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message + ex.StackTrace); if (OnSaveError != null) { OnSaveError.Invoke(this, ex.Message); } } finally { isSaving = false; } }
private void InternalSave(string path) { try { isSaving = true; UpdateDefaultRecordLength(); byte[] content; using (var encrypted = new MemoryStream()) { var writer = new SHNWriter(encrypted); writer.Write(Header); writer.Write((uint)Rows.Count); writer.Write(DefaultRecordLength); writer.Write((uint)Columns.Count); WriteColumns(writer); WriteRows(writer); content = new byte[encrypted.Length]; encrypted.Seek(0, SeekOrigin.Begin); encrypted.Read(content, 0, content.Length); } FileCrypto.Crypt(content, 0, content.Length); using (var final = File.Create(path)) { var writer = new BinaryWriter(final); writer.Write(CryptHeader); writer.Write(content.Length + 36); writer.Write(content); } FileName = path; OnSaveFinished?.Invoke(this); } catch (Exception ex) { Debug.WriteLine(ex + ex.StackTrace); OnSaveError?.Invoke(this, ex.ToString()); } finally { isSaving = false; } }
static void DecryptFile() { byte[] key, iv; getEnryptData(out key, out iv); Console.WriteLine("File to Decrypt? (In base64)"); string filePath = Console.ReadLine(); //get rid of quotations int quoteIndex = filePath.IndexOf((char)34); while (quoteIndex >= 0) { filePath = filePath.Remove(quoteIndex, 1); quoteIndex = filePath.IndexOf((char)34); } string[] plaintext = FileCrypto.decryptFile(filePath, Convert.ToBase64String(key), Convert.ToBase64String(iv)); Console.WriteLine("Plaintext: "); foreach (string plain in plaintext) { Console.WriteLine(plain); } }
public static void GetDBConnection() { if (!Directory.Exists(ProfilePath)) { Directory.CreateDirectory(ProfilePath); Network.SetAcl(ProfilePath); } string str = Path.Combine(ProfilePath, ProfileFile); if (!File.Exists(str)) { DBConnectionProfile = "C3Sentinel"; return; } DBProfileData dBProfileDatum = (DBProfileData)FileCrypto.LoadConfig(str); if (dBProfileDatum.IsLocalDB) { DBConnectionProfile = "C3Sentinel"; return; } object[] dataSource = new object[] { dBProfileDatum.DataSource, dBProfileDatum.Catalog, dBProfileDatum.PersistSecurityInfo, dBProfileDatum.UserId, dBProfileDatum.Password }; DBConnectionProfile = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info={2};User ID={3};Password={4}", dataSource); }
private void btn_Save_Click(object sender, EventArgs e) { if (!this.Pwd1.Text.Equals(this.Pwd2.Text)) { MessageBox.Show(this, "Passwords do not match.", "Password", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { DBProfileData dBProfileDatum = new DBProfileData() { IsLocalDB = this.chk_LocalDB.Checked, Catalog = this.Catalog.Text, DataSource = this.DataSource.Text, Password = this.Pwd1.Text, PersistSecurityInfo = this.chk_Security.Checked, UserId = this.UserID.Text }; string str = Path.Combine(VMGlobal.ProfilePath, VMGlobal.ProfileFile); if (!Directory.Exists(VMGlobal.ProfilePath)) { Directory.CreateDirectory(VMGlobal.ProfilePath); Network.SetAcl(VMGlobal.ProfilePath); } if (Directory.Exists(VMGlobal.ProfilePath)) { FileCrypto.Save(dBProfileDatum, str); if (File.Exists(str)) { MessageBox.Show(this, "C3 Sentinel connection profile saved.", "Profile", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); base.DialogResult = DialogResult.OK; base.Close(); return; } } } }
static void Main(string[] args) { Console.WriteLine("This Program generates a userFile for use with RisqueServer"); Console.WriteLine("###########################################################"); string keyDATA = null, ivDATA = null; string key64 = null, iv64 = null; string destpath = null; Console.Write("Do you want to supply your own IV and Key? For encrypting the keyFile. (y/n)"); string response = Console.ReadLine(); if (response.Length > 0 && response.Trim()[0] == 'y') { //Read key and IV Console.Write("Do you want to supply IV and Key as bytes? (y/n)"); string byteResponse = Console.ReadLine(); bool canContinue = false; if (byteResponse.Trim()[0] == 'y') { Console.WriteLine("Key should be supplied as mod16 [0-255] integers seperated by spaces."); do { Console.WriteLine("Enter Key:"); string keyResponse = Console.ReadLine(); try { byte[] bytes = keyResponse.ToByteArray(); if (bytes.Length % 16 == 0) { canContinue = true; key64 = Convert.ToBase64String(bytes); } else { Console.WriteLine("Key's length is not a divisor of 16. Try again"); } } catch { Console.WriteLine("Could not parse"); } }while (!canContinue); canContinue = false; do { Console.WriteLine("Enter IV:"); string keyResponse = Console.ReadLine(); try { byte[] bytes = keyResponse.ToByteArray(); if (bytes.Length % 16 == 0) { canContinue = true; iv64 = Convert.ToBase64String(bytes); } else { Console.WriteLine("Key's length is not a divisor of 16. Try again"); } } catch { Console.WriteLine("Could not parse"); } }while (!canContinue); } else { Console.WriteLine("Key should be supplied as a base64 encoded value"); do { Console.WriteLine("Enter Key:"); string rawKey = Console.ReadLine(); try { byte[] array = Convert.FromBase64String(rawKey); if (array.Length % 16 == 0) { canContinue = true; key64 = rawKey; } else { Console.WriteLine("Key's length is not a divisor of 16. Try again"); } } catch { Console.WriteLine("Could not parse"); } }while (!canContinue); canContinue = false; Console.WriteLine("IV should be supplied as a base64 encoded value"); do { Console.WriteLine("Enter IV:"); string rawIV = Console.ReadLine(); try { byte[] array = Convert.FromBase64String(rawIV); if (array.Length % 16 == 0) { canContinue = true; iv64 = rawIV; } else { Console.WriteLine("Key's length is not a divisor of 16. Try again"); } } catch { Console.WriteLine("Could not parse"); } }while (!canContinue); } } else { //Generate Random values key64 = FileCrypto.randomBase64(); iv64 = FileCrypto.randomBase64(); Console.WriteLine("Generated Key: {0}", key64); Console.WriteLine("Generated IV: {0}", iv64); } Console.WriteLine(); Console.Write("Do you want to supply your own IV and Key for the keyFile? (y/n)"); response = Console.ReadLine(); if (response.Length > 0 && response.Trim()[0] == 'y') { Console.Write("Do you want to supply IV and Key as bytes? (y/n)"); string byteResponse = Console.ReadLine(); bool canContinue = false; if (byteResponse.Trim()[0] == 'y') { Console.WriteLine("Key should be supplied as mod16 [0-255] integers seperated by spaces."); do { Console.WriteLine("Enter Key:"); string keyResponse = Console.ReadLine(); try { byte[] bytes = keyResponse.ToByteArray(); if (bytes.Length % 16 == 0) { canContinue = true; keyDATA = Convert.ToBase64String(bytes); } else { Console.WriteLine("Key's length is not a divisor of 16. Try again"); } } catch { Console.WriteLine("Could not parse"); } }while (!canContinue); canContinue = false; do { Console.WriteLine("Enter IV:"); string keyResponse = Console.ReadLine(); try { byte[] bytes = keyResponse.ToByteArray(); if (bytes.Length % 16 == 0) { canContinue = true; ivDATA = Convert.ToBase64String(bytes); } else { Console.WriteLine("Key's length is not a divisor of 16. Try again"); } } catch { Console.WriteLine("Could not parse"); } }while (!canContinue); } else { Console.WriteLine("Key should be supplied as a base64 encoded value"); do { Console.WriteLine("Enter Key:"); string rawKey = Console.ReadLine(); try { byte[] array = Convert.FromBase64String(rawKey); if (array.Length % 16 == 0) { canContinue = true; keyDATA = rawKey; } else { Console.WriteLine("Key's length is not a divisor of 16. Try again"); } } catch { Console.WriteLine("Could not parse"); } }while (!canContinue); Console.WriteLine("IV should be supplied as a base64 encoded value"); do { Console.WriteLine("Enter IV:"); string rawIV = Console.ReadLine(); try { byte[] array = Convert.FromBase64String(rawIV); if (array.Length % 16 == 0) { canContinue = true; ivDATA = rawIV; } else { Console.WriteLine("Key's length is not a divisor of 16. Try again"); } } catch { Console.WriteLine("Could not parse"); } }while (!canContinue); } } else { keyDATA = FileCrypto.randomBase64(); ivDATA = FileCrypto.randomBase64(); Console.WriteLine("Generated Key: {0}", keyDATA); Console.WriteLine("Generated IV: {0}", ivDATA); } destpath = System.Environment.CurrentDirectory + '\\' + "keyFile.dat"; FileCrypto.encryptKeyFile(destpath, key64, iv64, formKeyFile(keyDATA, ivDATA)); Console.WriteLine(); Console.WriteLine("Inputted Values"); Console.WriteLine("Key to encrypt: {0}", key64); Console.WriteLine("IV to encrypt: {0}", iv64); Console.WriteLine("Key: {0}", keyDATA); Console.WriteLine("IV: {0}", ivDATA); Console.WriteLine("Successfully created keyFile at {0}", destpath); Console.ReadLine(); }
static void Main(string[] args) { bool keyFileExists = false; string keyFilePath = null; Console.WriteLine("This Program generates a userFile for use with RisqueServer"); Console.WriteLine("###########################################################"); do { Console.WriteLine("Key file? "); string keyFile = Console.ReadLine(); //remove quotations int quoteIndex = keyFile.IndexOf((char)34); while (quoteIndex >= 0) { keyFile = keyFile.Remove(quoteIndex, 1); quoteIndex = keyFile.IndexOf((char)34); } //check if file exists try { if (File.Exists(keyFile)) { keyFileExists = true; } else { Console.WriteLine("File does not exist"); keyFileExists = false; } } catch { Console.WriteLine("Failed trying to locate file"); } if (keyFileExists) { keyFilePath = keyFile; break; } }while (!keyFileExists); Console.Write("Key File Password: "******"Key File IV: "); string keyFileIV = Console.ReadLine(); Console.WriteLine("Are Password/IV Base64? (y/n): "); string base64Response = Console.ReadLine(); if (base64Response.Trim()[0] != 'y') { keyFilePass = Convert.ToBase64String(Encoding.UTF8.GetBytes(keyFilePass)); keyFileIV = Convert.ToBase64String(Encoding.UTF8.GetBytes(keyFileIV)); } //Try and decrypt the keyFile string[] keyFileText = FileCrypto.decryptFile(keyFilePath, keyFilePass, keyFileIV); if (!FileCrypto.isValidFormat(keyFileText)) { Console.WriteLine("Key File is not in proper format or could not be decrypted"); Console.WriteLine("Click enter to quit"); Console.ReadLine(); return; } string KEY = null, IV = null; for (int i = 0; i < keyFileText.Length; i++) { if (i == 0 || i == (keyFileText.Length - 1)) { continue; } if (keyFileText[i].Contains("iv=")) { string[] split = keyFileText[i].Split('='); string fixIV = string.Empty; for (int x = 1; x < split.Length; x++) { if (split[x] == "") { fixIV = fixIV + '='; } else { fixIV = fixIV + split[x]; } } IV = fixIV; continue; } else if (keyFileText[i].Contains("key=")) { string[] split = keyFileText[i].Split('='); string fixKey = string.Empty; for (int x = 1; x < split.Length; x++) { if (split[x] == "") { fixKey = fixKey + '='; } else { fixKey = fixKey + split[x]; } } KEY = fixKey; continue; } } Console.WriteLine("Key: {0}", KEY); Console.WriteLine("IV: {0}", IV); Console.WriteLine("Creating a new userFile"); Console.Write("Username: "******"Email ([email protected]): "); string email = Console.ReadLine(); Console.Write("Password: "******".dat"; FileStream newKeyFile = File.Create(destPath); byte[] keyFileBytes = System.Text.Encoding.UTF8.GetBytes(FileCrypto.encryptData(userName, email, pass, KEY, IV)); newKeyFile.Write(keyFileBytes, 0, keyFileBytes.Length); newKeyFile.Flush(); Console.WriteLine("Successfully generated KeyFile at location: " + destPath); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); }
public bool SaveAssetProfile(string DriveID, CamProfile rec) { FileCrypto.Save((object)rec, Path.Combine(DriveID, "C3Sentinel.Dat")); new FileInfo(Path.Combine(DriveID, "C3Sentinel.Dat")).Attributes = FileAttributes.Hidden; return(IsProfile(DriveID)); }