1 people like it.
Like the snippet!
AES file encryption
Simple routines for encrypting/decrypting files using AES
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:
27:
|
open System
open System.IO
open System.Security.Cryptography
open System.Text
// Encrypt
let encrypt (key : string) inFile outFile =
use aes = new RijndaelManaged()
use encryptor = aes.CreateEncryptor(ASCIIEncoding.UTF8.GetBytes(key), ASCIIEncoding.UTF8.GetBytes(key))
use fsOut = new FileStream(outFile, FileMode.Create)
use cs = new CryptoStream(fsOut, encryptor, CryptoStreamMode.Write)
let buf = File.ReadAllBytes(inFile)
printfn "%A" buf.Length
cs.Write(buf, 0, buf.Length)
cs.FlushFinalBlock()
fsOut.Close()
// Decrypt
let decrypt (key : string) inFile outFile =
use aes = new RijndaelManaged()
use decryptor = aes.CreateDecryptor(ASCIIEncoding.UTF8.GetBytes(key), ASCIIEncoding.UTF8.GetBytes(key))
use fsIn = new FileStream(inFile, FileMode.Open)
printfn "%A" fsIn.Length
let cs = new CryptoStream(fsIn, decryptor, CryptoStreamMode.Read)
let (buf : byte[]) = Array.zeroCreate (int fsIn.Length)
cs.Read(buf, 0, buf.Length)
File.WriteAllBytes(outFile, buf)
|
namespace System
namespace System.IO
namespace System.Security
namespace System.Security.Cryptography
namespace System.Text
val encrypt : key:string -> inFile:string -> outFile:string -> unit
Full name: Script.encrypt
val key : string
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = String
Full name: Microsoft.FSharp.Core.string
val inFile : string
val outFile : string
val aes : RijndaelManaged
Multiple items
type RijndaelManaged =
inherit Rijndael
new : unit -> RijndaelManaged
member CreateDecryptor : rgbKey:byte[] * rgbIV:byte[] -> ICryptoTransform
member CreateEncryptor : rgbKey:byte[] * rgbIV:byte[] -> ICryptoTransform
member GenerateIV : unit -> unit
member GenerateKey : unit -> unit
Full name: System.Security.Cryptography.RijndaelManaged
--------------------
RijndaelManaged() : unit
val encryptor : ICryptoTransform
SymmetricAlgorithm.CreateEncryptor() : ICryptoTransform
RijndaelManaged.CreateEncryptor(rgbKey: byte [], rgbIV: byte []) : ICryptoTransform
Multiple items
type ASCIIEncoding =
inherit Encoding
new : unit -> ASCIIEncoding
member GetByteCount : chars:string -> int + 2 overloads
member GetBytes : chars:char * charCount:int * bytes:byte * byteCount:int -> int + 2 overloads
member GetCharCount : bytes:byte * count:int -> int + 1 overload
member GetChars : bytes:byte * byteCount:int * chars:char * charCount:int -> int + 1 overload
member GetDecoder : unit -> Decoder
member GetEncoder : unit -> Encoder
member GetMaxByteCount : charCount:int -> int
member GetMaxCharCount : byteCount:int -> int
member GetString : bytes:byte[] * byteIndex:int * byteCount:int -> string
...
Full name: System.Text.ASCIIEncoding
--------------------
ASCIIEncoding() : unit
property Encoding.UTF8: Encoding
Encoding.GetBytes(s: string) : byte []
Encoding.GetBytes(chars: char []) : byte []
Encoding.GetBytes(chars: char [], index: int, count: int) : byte []
Encoding.GetBytes(chars: nativeptr<char>, charCount: int, bytes: nativeptr<byte>, byteCount: int) : int
Encoding.GetBytes(s: string, charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
Encoding.GetBytes(chars: char [], charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
val fsOut : FileStream
Multiple items
type FileStream =
inherit Stream
new : path:string * mode:FileMode -> FileStream + 14 overloads
member BeginRead : array:byte[] * offset:int * numBytes:int * userCallback:AsyncCallback * stateObject:obj -> IAsyncResult
member BeginWrite : array:byte[] * offset:int * numBytes:int * userCallback:AsyncCallback * stateObject:obj -> IAsyncResult
member CanRead : bool
member CanSeek : bool
member CanWrite : bool
member EndRead : asyncResult:IAsyncResult -> int
member EndWrite : asyncResult:IAsyncResult -> unit
member Flush : unit -> unit + 1 overload
member GetAccessControl : unit -> FileSecurity
...
Full name: System.IO.FileStream
--------------------
FileStream(path: string, mode: FileMode) : unit
(+0 other overloads)
FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: FileAccess) : unit
(+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess) : unit
(+0 other overloads)
FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: FileAccess, bufferSize: int) : unit
(+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare) : unit
(+0 other overloads)
FileStream(handle: Win32.SafeHandles.SafeFileHandle, access: FileAccess, bufferSize: int, isAsync: bool) : unit
(+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare, bufferSize: int) : unit
(+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare, bufferSize: int, options: FileOptions) : unit
(+0 other overloads)
FileStream(path: string, mode: FileMode, access: FileAccess, share: FileShare, bufferSize: int, useAsync: bool) : unit
(+0 other overloads)
FileStream(path: string, mode: FileMode, rights: Security.AccessControl.FileSystemRights, share: FileShare, bufferSize: int, options: FileOptions) : unit
(+0 other overloads)
type FileMode =
| CreateNew = 1
| Create = 2
| Open = 3
| OpenOrCreate = 4
| Truncate = 5
| Append = 6
Full name: System.IO.FileMode
field FileMode.Create = 2
val cs : CryptoStream
Multiple items
type CryptoStream =
inherit Stream
new : stream:Stream * transform:ICryptoTransform * mode:CryptoStreamMode -> CryptoStream
member CanRead : bool
member CanSeek : bool
member CanWrite : bool
member Clear : unit -> unit
member Flush : unit -> unit
member FlushFinalBlock : unit -> unit
member HasFlushedFinalBlock : bool
member Length : int64
member Position : int64 with get, set
...
Full name: System.Security.Cryptography.CryptoStream
--------------------
CryptoStream(stream: Stream, transform: ICryptoTransform, mode: CryptoStreamMode) : unit
type CryptoStreamMode =
| Read = 0
| Write = 1
Full name: System.Security.Cryptography.CryptoStreamMode
field CryptoStreamMode.Write = 1
val buf : byte []
type File =
static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
static member AppendAllText : path:string * contents:string -> unit + 1 overload
static member AppendText : path:string -> StreamWriter
static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
static member Create : path:string -> FileStream + 3 overloads
static member CreateText : path:string -> StreamWriter
static member Decrypt : path:string -> unit
static member Delete : path:string -> unit
static member Encrypt : path:string -> unit
static member Exists : path:string -> bool
...
Full name: System.IO.File
File.ReadAllBytes(path: string) : byte []
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property Array.Length: int
CryptoStream.Write(buffer: byte [], offset: int, count: int) : unit
CryptoStream.FlushFinalBlock() : unit
Stream.Close() : unit
val decrypt : key:string -> inFile:string -> outFile:string -> unit
Full name: Script.decrypt
val decryptor : ICryptoTransform
SymmetricAlgorithm.CreateDecryptor() : ICryptoTransform
RijndaelManaged.CreateDecryptor(rgbKey: byte [], rgbIV: byte []) : ICryptoTransform
val fsIn : FileStream
field FileMode.Open = 3
property FileStream.Length: int64
field CryptoStreamMode.Read = 0
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.byte
--------------------
type byte = Byte
Full name: Microsoft.FSharp.Core.byte
type Array =
member Clone : unit -> obj
member CopyTo : array:Array * index:int -> unit + 1 overload
member GetEnumerator : unit -> IEnumerator
member GetLength : dimension:int -> int
member GetLongLength : dimension:int -> int64
member GetLowerBound : dimension:int -> int
member GetUpperBound : dimension:int -> int
member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
member Initialize : unit -> unit
member IsFixedSize : bool
...
Full name: System.Array
val zeroCreate : count:int -> 'T []
Full name: Microsoft.FSharp.Collections.Array.zeroCreate
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
CryptoStream.Read(buffer: byte [], offset: int, count: int) : int
File.WriteAllBytes(path: string, bytes: byte []) : unit
More information