4 people like it.

Encrypting a Rijndael string

Counter part for http://www.fssnip.net/1n

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
open System
open System.IO
open System.Security.Cryptography
open System.Text
open System.Diagnostics.Contracts

let EncryptStringWith (plain:string) (key:string) (iv:string) = 
    let enc = new ASCIIEncoding()
    use encrypted = new MemoryStream()
    use encode = new ToBase64Transform()
    let errdesc = "Failure when encrypting the string: " + plain + "\r\n"
    try
        use encryptor = Rijndael.Create().CreateEncryptor(enc.GetBytes(key), enc.GetBytes(iv))
        use tmpcrypt = new CryptoStream(encrypted, encryptor, CryptoStreamMode.Write)
        use encodestream = new CryptoStream(tmpcrypt, encode, CryptoStreamMode.Write)
            
        let encryptedbytes = enc.GetBytes(plain);
        encodestream.Write(encryptedbytes, 0, encryptedbytes.Length);
        encodestream.Close() // lazy, has to close explicitly before use. use is not enough.
    with
        | :? CryptographicException as ex -> failwith(errdesc + ex.ToString())
        | :? FormatException as ex -> failwith(errdesc + ex.ToString())
    encrypted.ToArray() |> Convert.ToBase64String
namespace System
namespace System.IO
namespace System.Security
namespace System.Security.Cryptography
namespace System.Text
namespace System.Diagnostics
namespace System.Diagnostics.Contracts
val EncryptStringWith : plain:string -> key:string -> iv:string -> string

Full name: Script.EncryptStringWith
val plain : 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 key : string
val iv : string
val enc : ASCIIEncoding
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
val encrypted : MemoryStream
Multiple items
type MemoryStream =
  inherit Stream
  new : unit -> MemoryStream + 6 overloads
  member CanRead : bool
  member CanSeek : bool
  member CanWrite : bool
  member Capacity : int with get, set
  member Flush : unit -> unit
  member GetBuffer : unit -> byte[]
  member Length : int64
  member Position : int64 with get, set
  member Read : buffer:byte[] * offset:int * count:int -> int
  ...

Full name: System.IO.MemoryStream

--------------------
MemoryStream() : unit
MemoryStream(capacity: int) : unit
MemoryStream(buffer: byte []) : unit
MemoryStream(buffer: byte [], writable: bool) : unit
MemoryStream(buffer: byte [], index: int, count: int) : unit
MemoryStream(buffer: byte [], index: int, count: int, writable: bool) : unit
MemoryStream(buffer: byte [], index: int, count: int, writable: bool, publiclyVisible: bool) : unit
val encode : ToBase64Transform
Multiple items
type ToBase64Transform =
  new : unit -> ToBase64Transform
  member CanReuseTransform : bool
  member CanTransformMultipleBlocks : bool
  member Clear : unit -> unit
  member Dispose : unit -> unit
  member InputBlockSize : int
  member OutputBlockSize : int
  member TransformBlock : inputBuffer:byte[] * inputOffset:int * inputCount:int * outputBuffer:byte[] * outputOffset:int -> int
  member TransformFinalBlock : inputBuffer:byte[] * inputOffset:int * inputCount:int -> byte[]

Full name: System.Security.Cryptography.ToBase64Transform

--------------------
ToBase64Transform() : unit
val errdesc : string
val encryptor : ICryptoTransform
type Rijndael =
  inherit SymmetricAlgorithm
  static member Create : unit -> Rijndael + 1 overload

Full name: System.Security.Cryptography.Rijndael
Rijndael.Create() : Rijndael
Rijndael.Create(algName: string) : Rijndael
Encoding.GetBytes(s: string) : byte []
Encoding.GetBytes(chars: char []) : byte []
Encoding.GetBytes(chars: char [], index: int, count: int) : byte []
ASCIIEncoding.GetBytes(chars: nativeptr<char>, charCount: int, bytes: nativeptr<byte>, byteCount: int) : int
ASCIIEncoding.GetBytes(chars: char [], charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
ASCIIEncoding.GetBytes(chars: string, charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
val tmpcrypt : 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 encodestream : CryptoStream
val encryptedbytes : byte []
CryptoStream.Write(buffer: byte [], offset: int, count: int) : unit
property Array.Length: int
Stream.Close() : unit
Multiple items
type CryptographicException =
  inherit SystemException
  new : unit -> CryptographicException + 4 overloads

Full name: System.Security.Cryptography.CryptographicException

--------------------
CryptographicException() : unit
CryptographicException(message: string) : unit
CryptographicException(hr: int) : unit
CryptographicException(format: string, insert: string) : unit
CryptographicException(message: string, inner: exn) : unit
val ex : CryptographicException
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
Exception.ToString() : string
Multiple items
type FormatException =
  inherit SystemException
  new : unit -> FormatException + 2 overloads

Full name: System.FormatException

--------------------
FormatException() : unit
FormatException(message: string) : unit
FormatException(message: string, innerException: exn) : unit
val ex : FormatException
MemoryStream.ToArray() : byte []
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert
Convert.ToBase64String(inArray: byte []) : string
Convert.ToBase64String(inArray: byte [], options: Base64FormattingOptions) : string
Convert.ToBase64String(inArray: byte [], offset: int, length: int) : string
Convert.ToBase64String(inArray: byte [], offset: int, length: int, options: Base64FormattingOptions) : string
Raw view Test code New version

More information

Link:http://fssnip.net/7PN
Posted:8 years ago
Author:Tuomas Hietanen
Tags: crypto , cryptography , encyption , rijndael