static int DecryptFileOptions(DecryptFileOptions opts) { if (FilesExist(new List <string> { opts.EncryptedPacketPath, opts.RecipientKeyPath, opts.SenderKeyPath })) { var encryptedPacketText = File.ReadAllText(opts.EncryptedPacketPath); var encryptedPacket = JsonConvert.DeserializeObject <EncryptedPacket>(encryptedPacketText); var decryptedData = new DecryptedData(); try { decryptedData = Decrypt(encryptedPacket, opts.KeyBitLength, opts.SenderKeyPath, opts.RecipientKeyPath); } catch (Exception ex) { WriteMessageToConsole($"Error while decrypting data: {ex.Message}", ConsoleColor.Red); return(1); } try { var outputPath = opts.OutputPath == null ? ".\\" : opts.OutputPath; File.WriteAllBytes($"{outputPath}\\{decryptedData.Filename}", decryptedData.FileContents); } catch (Exception ex) { WriteMessageToConsole(ex.Message, ConsoleColor.Red); return(1); } if (decryptedData.Version1Packet) { WriteMessageToConsole("WARNING: Specified Encrypted Packet did not contain a filename for the original file - temporary filename used instead but extension may be incorrect!!", ConsoleColor.Red); } WriteMessageToConsole($"File successfully decrypted to {decryptedData.Filename}", ConsoleColor.Green); return(0); } else { WriteMessageToConsole("One or more of the specified files were not found - see above", ConsoleColor.Red); return(1); } }
private static int DecryptFile(DecryptFileOptions opts) { string pkcs11LibPath = opts.LibPath ?? FindEidLibrary(); IBokPinProvider pinProvider = CreatePinpProvider(opts.UseConsolePin); using EidRsaCryptoAccessor eidRsaCryptoAccessor = new EidRsaCryptoAccessor(pkcs11LibPath, pinProvider); using FileStream inputFiletream = new FileStream(opts.EncryptedFile, FileMode.Open, FileAccess.Read); using ContainerReader reader = new ContainerReader(inputFiletream, eidRsaCryptoAccessor); string fileName = reader.ReadFileName().GetAwaiter().GetResult(); string outputFilePath = Path.Combine(Path.GetDirectoryName(opts.EncryptedFile), fileName); using FileStream outputFiletream = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite); using Stream contentSrream = reader.GetContentStream().GetAwaiter().GetResult(); contentSrream.CopyTo(outputFiletream); return(0); }