2 people like it.

Decrypting a Rijndael string

This F# code decrypts an encrypted string using Rijndael symmetric encryption algorithm. It uses key and initialization vector stored in a registry key.

 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: 
28: 
29: 
30: 
31: 
open System.IO
open System.Security.Cryptography
open System.Text
open System.Diagnostics.Contracts

let private DeCryptStringWith (crypted:string) (key:string) (iv:string) = 
    let enc = new ASCIIEncoding()
    let algo = Rijndael.Create()
    if(crypted.Length < 5) then failwith("Crypted string length has to be over 5 chars.")
    use decrypted = new MemoryStream()
    use decode = new FromBase64Transform()
    let errdesc = "Failure when decrypting the string " + crypted.[0..3] + "...\r\n"
    try
        use decryptor = algo.CreateDecryptor(enc.GetBytes(key), enc.GetBytes(iv))
        use tmpcrypt = new CryptoStream(decrypted, decryptor, CryptoStreamMode.Write)
        use decodestream = new CryptoStream(tmpcrypt, decode, CryptoStreamMode.Write)
        let cryptedbytes = enc.GetBytes(crypted);
        decodestream.Write(cryptedbytes, 0, cryptedbytes.Length);
        decodestream.Close() // lazy, has to close explicitly before use. using is not enough.
    with
        | :? CryptographicException as ex -> failwith(errdesc + ex.ToString())
        | :? System.FormatException as ex -> failwith(errdesc + ex.ToString())
    enc.GetString(decrypted.ToArray())



//I recommend to get key and iv from registry and then make one more method like:
let internal DeCryptString crypted =
    let key = GetRegistryValue("rgbKey").ToString()
    let iv = GetRegistryValue("rgbIV").ToString()
    DeCryptStringWith crypted key iv
namespace System
namespace System.IO
namespace System.Security
namespace System.Security.Cryptography
namespace System.Text
namespace System.Diagnostics
namespace System.Diagnostics.Contracts
val private DeCryptStringWith : crypted:string -> key:string -> iv:string -> string

Full name: Script.DeCryptStringWith
val crypted : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.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 algo : Rijndael
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
property System.String.Length: int
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
val decrypted : 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 decode : FromBase64Transform
Multiple items
type FromBase64Transform =
  new : unit -> FromBase64Transform + 1 overload
  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.FromBase64Transform

--------------------
FromBase64Transform() : unit
FromBase64Transform(whitespaces: FromBase64TransformMode) : unit
val errdesc : string
val decryptor : ICryptoTransform
SymmetricAlgorithm.CreateDecryptor() : ICryptoTransform
SymmetricAlgorithm.CreateDecryptor(rgbKey: byte [], rgbIV: byte []) : ICryptoTransform
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 decodestream : CryptoStream
val cryptedbytes : byte []
CryptoStream.Write(buffer: byte [], offset: int, count: int) : unit
property System.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
System.Exception.ToString() : string
Multiple items
type FormatException =
  inherit SystemException
  new : unit -> FormatException + 2 overloads

Full name: System.FormatException

--------------------
System.FormatException() : unit
System.FormatException(message: string) : unit
System.FormatException(message: string, innerException: exn) : unit
val ex : System.FormatException
Encoding.GetString(bytes: byte []) : string
ASCIIEncoding.GetString(bytes: byte [], byteIndex: int, byteCount: int) : string
MemoryStream.ToArray() : byte []
val internal DeCryptString : crypted:string -> string

Full name: Script.DeCryptString
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/1n
Posted:2 years ago
Author:Tuomas Hietanen
Tags: rijndael , decrypt , cryptography