0 people like it.

PGP F# with PGPCore

PGP F# with PGPCore https://blog.bitscry.com/2018/07/05/pgp-encryption-and-decryption-in-c/

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
#r "nuget:PgpCore"
open System.IO
use pgp = new PgpCore.PGP()

// Generate keys
pgp.GenerateKey(@"C:\TEMP\keys\public.asc", @"C:\TEMP\keys\private.asc", "email@email.com", "password")
// Encrypt file
pgp.EncryptFile(@"C:\TEMP\keys\content.txt", @"C:\TEMP\keys\content__encrypted.pgp", @"C:\TEMP\keys\public.asc", true, true)
// Encrypt and sign file
pgp.EncryptFileAndSign(@"C:\TEMP\keys\content.txt", @"C:\TEMP\keys\content__encrypted_signed.pgp", @"C:\TEMP\keys\public.asc", @"C:\TEMP\keys\private.asc", "password", true, true)
// Decrypt file
pgp.DecryptFile(@"C:\TEMP\keys\content__encrypted.pgp", @"C:\TEMP\keys\content__decrypted.txt", @"C:\TEMP\keys\private.asc", "password")
// Decrypt signed file
pgp.DecryptFile(@"C:\TEMP\keys\content__encrypted_signed.pgp", @"C:\TEMP\keys\content__decrypted_signed.txt", @"C:\TEMP\keys\private.asc", "password")

// Encrypt stream
use inputFileStream = new FileStream(@"C:\TEMP\keys\content.txt", FileMode.Open)
use outputFileStream = File.Create(@"C:\TEMP\keys\content__encrypted2.pgp")
use publicKeyStream = new FileStream(@"C:\TEMP\keys\public.asc", FileMode.Open)
pgp.EncryptStream(inputFileStream, outputFileStream, publicKeyStream, true, true)

// Decrypt stream
use inputFileStream = new FileStream(@"C:\TEMP\keys\content__encrypted2.pgp", FileMode.Open)
use outputFileStream = File.Create(@"C:\TEMP\keys\content__decrypted2.txt")
use privateKeyStream = new FileStream(@"C:\TEMP\keys\private.asc", FileMode.Open)
let _ = pgp.DecryptStream(inputFileStream, outputFileStream, privateKeyStream, "password")
namespace System
namespace System.IO
val pgp : PgpCore.PGP
namespace PgpCore
Multiple items
type PGP =
  new : unit -> PGP + 1 overload
  member ClearSignArmoredString : input:string -> string + 2 overloads
  member ClearSignArmoredStringAsync : input:string -> Task<string> + 2 overloads
  member ClearSignFile : inputFilePath:string * outputFilePath:string -> unit + 5 overloads
  member ClearSignFileAsync : inputFilePath:string * outputFilePath:string -> Task + 5 overloads
  member ClearSignStream : inputStream:Stream * outputStream:Stream -> unit + 2 overloads
  member ClearSignStreamAsync : inputStream:Stream * outputStream:Stream -> Task + 2 overloads
  member CompressionAlgorithm : CompressionAlgorithmTag with get, set
  member DecryptArmoredString : input:string -> string + 2 overloads
  member DecryptArmoredStringAndVerify : input:string -> string + 2 overloads
  ...

--------------------
PgpCore.PGP() : PgpCore.PGP
PgpCore.PGP(encryptionKeys: PgpCore.IEncryptionKeys) : PgpCore.PGP
val inputFileStream : FileStream
Multiple items
type FileStream =
  inherit Stream
  new : handle:nativeint * access:FileAccess -> FileStream + 12 overloads
  member BeginRead : array:byte[] * offset:int * numBytes:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginWrite : array:byte[] * offset:int * numBytes:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member CanRead : bool
  member CanSeek : bool
  member CanWrite : bool
  member CopyToAsync : destination:Stream * bufferSize:int * cancellationToken:CancellationToken -> Task
  member DisposeAsync : unit -> ValueTask
  member EndRead : asyncResult:IAsyncResult -> int
  member EndWrite : asyncResult:IAsyncResult -> unit
  ...

--------------------
FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: FileAccess) : FileStream
FileStream(path: string, mode: FileMode) : FileStream
FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: FileAccess, bufferSize: int) : FileStream
FileStream(path: string, mode: FileMode, access: FileAccess) : FileStream
FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: FileAccess, bufferSize: int, isAsync: bool) : FileStream
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare) : FileStream
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare, bufferSize: int) : FileStream
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare, bufferSize: int, useAsync: bool) : FileStream
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare, bufferSize: int, options: FileOptions) : FileStream
type FileMode =
  | CreateNew = 1
  | Create = 2
  | Open = 3
  | OpenOrCreate = 4
  | Truncate = 5
  | Append = 6
field FileMode.Open: FileMode = 3
val outputFileStream : FileStream
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllLinesAsync : path:string * contents:IEnumerable<string> * ?cancellationToken:CancellationToken -> Task + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendAllTextAsync : path:string * contents:string * ?cancellationToken:CancellationToken -> Task + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 2 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  ...
File.Create(path: string) : FileStream
File.Create(path: string, bufferSize: int) : FileStream
File.Create(path: string, bufferSize: int, options: FileOptions) : FileStream
val publicKeyStream : FileStream
val privateKeyStream : FileStream
Raw view Test code New version

More information

Link:http://fssnip.net/86k
Posted:8 months ago
Author:Tuomas Hietanen
Tags: #pgp