2 people like it.
Like the snippet!
RSA encryption/decryption
Simple routines for RSA encryption/decryption
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:
32:
33:
34:
35:
36:
37:
38:
|
open System
open System.Security.Cryptography
open System.Text
// RSA encryption function
let RsaEncrypt (data : byte[]) (key : RSAParameters) (padding : bool) : byte[] =
try
let rsa = new RSACryptoServiceProvider()
rsa.ImportParameters(key)
rsa.Encrypt(data, padding)
with
| :? CryptographicException as e ->
printfn "%A" e.Message
Array.empty
// RSA decryption function
let RsaDecrypt (data : byte[]) (key : RSAParameters) (padding : bool) : byte[] =
try
let rsa = new RSACryptoServiceProvider()
rsa.ImportParameters(key)
rsa.Decrypt(data, padding)
with
| :? CryptographicException as e ->
printfn "%A" e.Message
Array.empty
// Example
// ========================
let rsa = new RSACryptoServiceProvider(2048)
let publicPrivateKeyXML = rsa.ToXmlString(true)
rsa.FromXmlString(publicPrivateKeyXML)
let publicOnlyKeyXML = rsa.ToXmlString(false)
let plainText = "hello world"
let plainTextArray = System.Text.Encoding.ASCII.GetBytes(plainText)
let cipherArray = RsaEncrypt plainTextArray (rsa.ExportParameters(false)) false
let cipherText = System.Text.Encoding.ASCII.GetString(cipherArray)
let decryptedTextArray = RsaDecrypt (System.Text.Encoding.ASCII.GetBytes(cipherText)) (rsa.ExportParameters(true)) false
let decryptedText = System.Text.Encoding.ASCII.GetString(decryptedTextArray)
|
namespace System
namespace System.Security
namespace System.Security.Cryptography
namespace System.Text
val RsaEncrypt : data:byte [] -> key:RSAParameters -> padding:bool -> byte []
Full name: Script.RsaEncrypt
val data : byte []
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
val key : RSAParameters
type RSAParameters =
struct
val Exponent : byte[]
val Modulus : byte[]
val P : byte[]
val Q : byte[]
val DP : byte[]
val DQ : byte[]
val InverseQ : byte[]
val D : byte[]
end
Full name: System.Security.Cryptography.RSAParameters
val padding : bool
type bool = Boolean
Full name: Microsoft.FSharp.Core.bool
val rsa : RSACryptoServiceProvider
Multiple items
type RSACryptoServiceProvider =
inherit RSA
new : unit -> RSACryptoServiceProvider + 3 overloads
member CspKeyContainerInfo : CspKeyContainerInfo
member Decrypt : rgb:byte[] * fOAEP:bool -> byte[]
member DecryptValue : rgb:byte[] -> byte[]
member Encrypt : rgb:byte[] * fOAEP:bool -> byte[]
member EncryptValue : rgb:byte[] -> byte[]
member ExportCspBlob : includePrivateParameters:bool -> byte[]
member ExportParameters : includePrivateParameters:bool -> RSAParameters
member ImportCspBlob : keyBlob:byte[] -> unit
member ImportParameters : parameters:RSAParameters -> unit
...
Full name: System.Security.Cryptography.RSACryptoServiceProvider
--------------------
RSACryptoServiceProvider() : unit
RSACryptoServiceProvider(dwKeySize: int) : unit
RSACryptoServiceProvider(parameters: CspParameters) : unit
RSACryptoServiceProvider(dwKeySize: int, parameters: CspParameters) : unit
RSACryptoServiceProvider.ImportParameters(parameters: RSAParameters) : unit
RSACryptoServiceProvider.Encrypt(rgb: byte [], fOAEP: bool) : byte []
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 e : CryptographicException
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
property Exception.Message: string
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 empty<'T> : 'T []
Full name: Microsoft.FSharp.Collections.Array.empty
val RsaDecrypt : data:byte [] -> key:RSAParameters -> padding:bool -> byte []
Full name: Script.RsaDecrypt
RSACryptoServiceProvider.Decrypt(rgb: byte [], fOAEP: bool) : byte []
val rsa : RSACryptoServiceProvider
Full name: Script.rsa
val publicPrivateKeyXML : string
Full name: Script.publicPrivateKeyXML
RSA.ToXmlString(includePrivateParameters: bool) : string
RSA.FromXmlString(xmlString: string) : unit
val publicOnlyKeyXML : string
Full name: Script.publicOnlyKeyXML
val plainText : string
Full name: Script.plainText
val plainTextArray : byte []
Full name: Script.plainTextArray
type Encoding =
member BodyName : string
member Clone : unit -> obj
member CodePage : int
member DecoderFallback : DecoderFallback with get, set
member EncoderFallback : EncoderFallback with get, set
member EncodingName : string
member Equals : value:obj -> bool
member GetByteCount : chars:char[] -> int + 3 overloads
member GetBytes : chars:char[] -> byte[] + 5 overloads
member GetCharCount : bytes:byte[] -> int + 2 overloads
...
Full name: System.Text.Encoding
property Encoding.ASCII: 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 cipherArray : byte []
Full name: Script.cipherArray
RSACryptoServiceProvider.ExportParameters(includePrivateParameters: bool) : RSAParameters
val cipherText : string
Full name: Script.cipherText
Encoding.GetString(bytes: byte []) : string
Encoding.GetString(bytes: byte [], index: int, count: int) : string
val decryptedTextArray : byte []
Full name: Script.decryptedTextArray
val decryptedText : string
Full name: Script.decryptedText
More information