private static bool EncrypteFile(PrivatePublicKeyHelper ppk, string key, string keyType) { Console.Clear(); ConsoleEx.TitleBar(0, "Encrypte File", ConsoleColor.Yellow, ConsoleColor.DarkBlue); Console.Clear(); ConsoleEx.TitleBar(0, string.Format("Encrypte File With {0}", keyType), ConsoleColor.Yellow, ConsoleColor.DarkBlue); ConsoleEx.WriteLine(0, 2, "Filename:", ConsoleColor.Cyan); var fileName = Console.ReadLine(); ConsoleEx.WriteLine(0, 4, "", ConsoleColor.Cyan); if (!File.Exists(fileName)) { ConsoleEx.WriteLine(0, 3, "Cannot find the file", ConsoleColor.Red); return(false); } var dataFile = File.ReadAllBytes(fileName); var dataEncrypted = ppk.EncryptBuffer(dataFile, key); var tmpFile = fileName + ENCRYPTED_EXTENSION; if (File.Exists(tmpFile)) { File.Delete(tmpFile); } File.WriteAllBytes(tmpFile, dataEncrypted); Console.WriteLine(string.Format("Encyrpted file is located at {0}", tmpFile), ConsoleColor.Cyan); Pause(); return(true); }
private static void UnitTests(PrivatePublicKeyHelper ppk, sFs.sFsManager sFsManager) { Console.Clear(); var sw = Stopwatch.StartNew(); var source = @"C:\@USB_STICK_Data\Img_4950.jpg"; var sourceExtension = Path.GetExtension(source); ppk.PrivateKey = sFsManager["PrivateKey"].GetBufferAsUnicodeString(); ppk.PublicKey = sFsManager["PublicKey"].GetBufferAsUnicodeString(); var dataFile = File.ReadAllBytes(source); var dataEncrypted = ppk.EncryptBuffer(dataFile, ppk.PublicKey); var tmpFile = source + ENCRYPTED_EXTENSION; File.WriteAllBytes(tmpFile, dataEncrypted); sw.Stop(); Console.WriteLine("Encryption time: {0}", sw.ElapsedMilliseconds / 1000.0); sw = Stopwatch.StartNew(); var dataFile2 = File.ReadAllBytes(tmpFile); dataEncrypted = ppk.DecryptBuffer(dataFile2, ppk.PrivateKey); var newFile = tmpFile + sourceExtension; File.WriteAllBytes(newFile, dataEncrypted); sw.Stop(); Console.WriteLine("Dencryption time: {0}", sw.ElapsedMilliseconds / 1000.0); Pause(); }
private static bool DecrypteFile(PrivatePublicKeyHelper ppk, string key, string keyType) { Console.Clear(); ConsoleEx.TitleBar(0, string.Format("Decrypte file with {0}", keyType), ConsoleColor.Yellow, ConsoleColor.DarkBlue); ConsoleEx.WriteLine(0, 2, "Filename:", ConsoleColor.Cyan); var fileName = Console.ReadLine(); ConsoleEx.Gotoxy(0, 4); if (!File.Exists(fileName)) { ConsoleEx.WriteLine(0, 3, "Cannot find the file", ConsoleColor.Red); Pause(); return(false); } var sourceExtension = Path.GetExtension(fileName.Replace(ENCRYPTED_EXTENSION, "")); var dataFile = File.ReadAllBytes(fileName); var dataEncrypted = ppk.DecryptBuffer(dataFile, key); var tmpFile = fileName + sourceExtension; File.WriteAllBytes(tmpFile, dataEncrypted); Console.WriteLine(string.Format("Encyrpted file is located at {0}", tmpFile), ConsoleColor.Cyan); Pause(); return(true); }
static bool GeneratePrivateKeyPublicKey(string pw) { var ok = false; Console.Clear(); ConsoleEx.TitleBar(0, "Generate new private/public key", ConsoleColor.Yellow, ConsoleColor.DarkBlue); Console.Clear(); ConsoleEx.TitleBar(0, "Generate Private/Public Key", ConsoleColor.Yellow, ConsoleColor.DarkBlue); var keyInfo = ConsoleEx.Question(3, "Re generate the private key and publick key Y)es N)o", new List <char> { 'Y', 'N' }); if (keyInfo == 'N') { return(ok); } keyInfo = ConsoleEx.Question(3, "Are you sure you want to generate Private/Public Key Y)es N)o", new List <char> { 'Y', 'N' }); if (keyInfo == 'N') { return(ok); } ConsoleEx.WriteLine(0, 4, "Formatting...", ConsoleColor.Yellow); var ppk = new PrivatePublicKeyHelper(true); if (!_sfsManager.Format()) { ConsoleEx.WriteLine(0, 3, "Format failed", ConsoleColor.Red); } ConsoleEx.WriteLine(0, 5, "Loading and saving files...", ConsoleColor.Yellow); var privatek = _sfsManager.AddFile("PrivateKey"); privatek.SetContentAsString(ppk.PrivateKey); var publicK = _sfsManager.AddFile("PublicKey"); publicK.SetContentAsString(ppk.PublicKey); var readmeTxt = _sfsManager.AddFile("ReadMe.txt"); readmeTxt.SetContentAsString(LoremIpsum); var rr = _sfsManager.UpdateFileSystem(); if (rr) { Console.WriteLine(string.Format("Keys were generated"), ConsoleColor.Cyan); ok = ReloadFileSystem(pw, ppk); } else { Console.WriteLine(string.Format("Error generating the keys"), ConsoleColor.Red); } Pause(); return(ok); }
private static bool ReloadFileSystem(string pw, PrivatePublicKeyHelper ppk) { Console.WriteLine("Reading FAT and loading files"); var ok = _sfsManager.ReadFileSystem(); if (ok) { ppk.PrivateKey = _sfsManager.LoadFileContent("PrivateKey").GetBufferAsUnicodeString(); ppk.PublicKey = _sfsManager.LoadFileContent("PublicKey").GetBufferAsUnicodeString(); } return(true); }
public static void Run(string[] args) { var pw = string.Empty; Console.WriteLine("Nusbio initialization"); Nusbio.ActivateFastMode(); var serialNumber = Nusbio.Detect(); if (serialNumber == null) // Detect the first Nusbio available { Console.WriteLine("nusbio not detected"); return; } using (var nusbio = new Nusbio(serialNumber)) { pw = AskForPW(nusbio, VolumeName); _sfsManager = new sFs.sFsManager(VolumeName, pw, nusbio: nusbio, clockPin: NusbioGpio.Gpio0, mosiPin: NusbioGpio.Gpio1, misoPin: NusbioGpio.Gpio2, selectPin: NusbioGpio.Gpio3); _sfsManager.Begin(); Cls(nusbio, true); Console.WriteLine(Environment.NewLine + Environment.NewLine); Console.WriteLine("Initializing private public key module"); var ppk = new PrivatePublicKeyHelper(true); //GeneratePrivateKeyPublicKey(pw); var ok = ReloadFileSystem(pw, ppk); if (!ok) { Cls(nusbio, true); if (ConsoleEx.Question(3, string.Format("Cannot access {0} with this password, retry Y)es N)o", _sfsManager.VolumeName), new List <char> { 'Y', 'N' }) == 'Y') { Environment.Exit(1); } Cls(nusbio, true); // Re format - re init key var keyInfo = ConsoleEx.Question(4, string.Format("Would you like to initialize {0} Y)es N)o", _sfsManager.VolumeName), new List <char> { 'Y', 'N' }); if (keyInfo == 'N') { return; } GeneratePrivateKeyPublicKey(pw); } Cls(nusbio); while (nusbio.Loop()) { if (Console.KeyAvailable) { var kk = Console.ReadKey(true); var k = kk.Key; if (kk.Modifiers == ConsoleModifiers.Control) { if (DigitKeys.Contains(k)) { var a = (int)k - 48; if (a == 0) { a = 10; } a--; ShowFile(a); } } else { if (k == ConsoleKey.A) { AddNewTextFile(pw); } if (k == ConsoleKey.B) { AddNewTextFile(pw, @"C:\Users\fredericaltorres\Dropbox\FTO\MonteChristro.jpg"); } if (k == ConsoleKey.D1) { EncrypteFile(ppk, ppk.PublicKey, "PublicKey"); } if (k == ConsoleKey.D2) { EncrypteFile(ppk, ppk.PrivateKey, "PrivateKey"); } if (k == ConsoleKey.D3) { DecrypteFile(ppk, ppk.PrivateKey, "PrivateKey"); } if (k == ConsoleKey.D4) { ExportKey(ppk.PublicKey, "PublicKey"); } if (k == ConsoleKey.D5) { ExportKey(ppk.PrivateKey, "PrivateKey"); } if (k == ConsoleKey.D6) { GeneratePrivateKeyPublicKey(pw); } if (k == ConsoleKey.U) { UnitTests(ppk, _sfsManager); } if (k == ConsoleKey.Q) { break; } } Cls(nusbio); } } } Console.Clear(); }