private static int ExportCertificate(ExportCertificateOptions opts) { string pkcs11LibPath = opts.LibPath ?? FindEidLibrary(); IBokPinProvider pinProvider = CreatePinpProvider(opts.UseConsolePin); using EidRsaCryptoAccessor eidRsaCryptoAccessor = new EidRsaCryptoAccessor(pkcs11LibPath, pinProvider); X509Certificate2 certificate = eidRsaCryptoAccessor.ExtractPublicCertificate().GetAwaiter().GetResult(); string savePath = opts.ExportCertificatePath; if (string.IsNullOrEmpty(savePath)) { savePath = string.Concat(certificate.Thumbprint, ".cer"); } File.WriteAllBytes(savePath, certificate.RawData); return(0); }
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); }