Skip to content

Commit df710df

Browse files
danieljsummersmairaw
authored andcommitted
F# Aes Samples (#1635)
* Add F# Aes snippets (#3154) * Shortened name to match other languages (#3154) C# and VB didn't have "service", they just had "serv"; matching that pattern for the F# sample
1 parent 1379983 commit df710df

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//<Snippet1>
2+
open System
3+
open System.IO
4+
open System.Security.Cryptography
5+
6+
//<Snippet2>
7+
let encryptStringToBytes_Aes (plainText: string, key : byte[], iv : byte[]) : byte[] =
8+
9+
// Check arguments.
10+
if (isNull plainText || plainText.Length <= 0) then nullArg "plainText"
11+
if (isNull key || key.Length <= 0) then nullArg "key"
12+
if (isNull iv || iv.Length <= 0) then nullArg "iv"
13+
14+
// Create an AesCryptoServiceProvider object
15+
// with the specified key and IV.
16+
use aesAlg = new AesCryptoServiceProvider()
17+
aesAlg.Key <- key
18+
aesAlg.IV <- iv
19+
20+
// Create an encryptor to perform the stream transform.
21+
let encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
22+
23+
// Create the streams used for encryption.
24+
use msEncrypt = new MemoryStream()
25+
use csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
26+
use swEncrypt = new StreamWriter(csEncrypt)
27+
28+
//Write all data to the stream.
29+
swEncrypt.Write(plainText)
30+
swEncrypt.Flush()
31+
32+
// Return the encrypted bytes from the memory stream.
33+
msEncrypt.ToArray()
34+
//</Snippet2>
35+
36+
//<Snippet3>
37+
let decryptStringFromBytes_Aes (cipherText : byte[], key : byte[], iv : byte[]) : string =
38+
39+
// Check arguments.
40+
if (isNull cipherText || cipherText.Length <= 0) then nullArg "cipherText"
41+
if (isNull key || key.Length <= 0) then nullArg "key"
42+
if (isNull iv || iv.Length <= 0) then nullArg "iv"
43+
44+
// Create an AesCryptoServiceProvider object
45+
// with the specified key and IV.
46+
use aesAlg = new AesCryptoServiceProvider()
47+
aesAlg.Key <- key
48+
aesAlg.IV <- iv
49+
50+
// Create a decryptor to perform the stream transform.
51+
let decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)
52+
53+
// Create the streams used for decryption.
54+
use msDecrypt = new MemoryStream(cipherText)
55+
use csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
56+
use srDecrypt = new StreamReader(csDecrypt)
57+
58+
// Read the decrypted bytes from the decrypting stream
59+
// and return the resulting string.
60+
srDecrypt.ReadToEnd()
61+
//</Snippet3>
62+
63+
[<EntryPoint>]
64+
let main argv =
65+
66+
let original = "Here is some data to encrypt!"
67+
68+
// Create a new instance of the AesCryptoServiceProvider
69+
// class. This generates a new key and initialization
70+
// vector (IV).
71+
use myAes = new AesCryptoServiceProvider()
72+
73+
// Encrypt the string to an array of bytes.
74+
let encrypted = encryptStringToBytes_Aes(original, myAes.Key, myAes.IV)
75+
76+
// Decrypt the bytes to a string.
77+
let roundtrip = decryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV)
78+
79+
//Display the original data and the decrypted data.
80+
Console.WriteLine("Original: {0}", original)
81+
Console.WriteLine("Round Trip: {0}", roundtrip)
82+
0
83+
//</Snippet1>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//<Snippet1>
2+
open System
3+
open System.IO
4+
open System.Security.Cryptography
5+
6+
//<Snippet2>
7+
let encryptStringToBytes_Aes (plainText: string, key : byte[], iv : byte[]) : byte[] =
8+
9+
// Check arguments.
10+
if (isNull plainText || plainText.Length <= 0) then nullArg "plainText"
11+
if (isNull key || key.Length <= 0) then nullArg "key"
12+
if (isNull iv || iv.Length <= 0) then nullArg "iv"
13+
14+
// Create an AesManaged object
15+
// with the specified key and IV.
16+
use aesAlg = new AesManaged()
17+
aesAlg.Key <- key
18+
aesAlg.IV <- iv
19+
20+
// Create an encryptor to perform the stream transform.
21+
let encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
22+
23+
// Create the streams used for encryption.
24+
use msEncrypt = new MemoryStream()
25+
use csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
26+
use swEncrypt = new StreamWriter(csEncrypt)
27+
28+
//Write all data to the stream.
29+
swEncrypt.Write(plainText)
30+
swEncrypt.Flush()
31+
32+
// Return the encrypted bytes from the memory stream.
33+
msEncrypt.ToArray()
34+
//</Snippet2>
35+
36+
//<Snippet3>
37+
let decryptStringFromBytes_Aes (cipherText : byte[], key : byte[], iv : byte[]) : string =
38+
39+
// Check arguments.
40+
if (isNull cipherText || cipherText.Length <= 0) then nullArg "cipherText"
41+
if (isNull key || key.Length <= 0) then nullArg "key"
42+
if (isNull iv || iv.Length <= 0) then nullArg "iv"
43+
44+
// Create an AesManaged object
45+
// with the specified key and IV.
46+
use aesAlg = new AesManaged()
47+
aesAlg.Key <- key
48+
aesAlg.IV <- iv
49+
50+
// Create a decryptor to perform the stream transform.
51+
let decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)
52+
53+
// Create the streams used for decryption.
54+
use msDecrypt = new MemoryStream(cipherText)
55+
use csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
56+
use srDecrypt = new StreamReader(csDecrypt)
57+
58+
// Read the decrypted bytes from the decrypting stream
59+
// and return the resulting string.
60+
srDecrypt.ReadToEnd()
61+
//</Snippet3>
62+
63+
[<EntryPoint>]
64+
let main argv =
65+
66+
let original = "Here is some data to encrypt!"
67+
68+
// Create a new instance of the AesManaged
69+
// class. This generates a new key and initialization
70+
// vector (IV).
71+
use myAes = new AesManaged()
72+
73+
// Encrypt the string to an array of bytes.
74+
let encrypted = encryptStringToBytes_Aes(original, myAes.Key, myAes.IV)
75+
76+
// Decrypt the bytes to a string.
77+
let roundtrip = decryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV)
78+
79+
//Display the original data and the decrypted data.
80+
Console.WriteLine("Original: {0}", original)
81+
Console.WriteLine("Round Trip: {0}", roundtrip)
82+
0
83+
//</Snippet1>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//<Snippet1>
2+
open System
3+
open System.IO
4+
open System.Security.Cryptography
5+
6+
//<Snippet2>
7+
let encryptStringToBytes_Aes (plainText: string, key : byte[], iv : byte[]) : byte[] =
8+
9+
// Check arguments.
10+
if (isNull plainText || plainText.Length <= 0) then nullArg "plainText"
11+
if (isNull key || key.Length <= 0) then nullArg "key"
12+
if (isNull iv || iv.Length <= 0) then nullArg "iv"
13+
14+
// Create an Aes object
15+
// with the specified key and IV.
16+
use aesAlg = Aes.Create()
17+
aesAlg.Key <- key
18+
aesAlg.IV <- iv
19+
20+
// Create an encryptor to perform the stream transform.
21+
let encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
22+
23+
// Create the streams used for encryption.
24+
use msEncrypt = new MemoryStream()
25+
use csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
26+
use swEncrypt = new StreamWriter(csEncrypt)
27+
28+
//Write all data to the stream.
29+
swEncrypt.Write(plainText)
30+
swEncrypt.Flush()
31+
32+
// Return the encrypted bytes from the memory stream.
33+
msEncrypt.ToArray()
34+
//</Snippet2>
35+
36+
//<Snippet3>
37+
let decryptStringFromBytes_Aes (cipherText : byte[], key : byte[], iv : byte[]) : string =
38+
39+
// Check arguments.
40+
if (isNull cipherText || cipherText.Length <= 0) then nullArg "cipherText"
41+
if (isNull key || key.Length <= 0) then nullArg "key"
42+
if (isNull iv || iv.Length <= 0) then nullArg "iv"
43+
44+
// Create an Aes object
45+
// with the specified key and IV.
46+
use aesAlg = Aes.Create()
47+
aesAlg.Key <- key
48+
aesAlg.IV <- iv
49+
50+
// Create a decryptor to perform the stream transform.
51+
let decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)
52+
53+
// Create the streams used for decryption.
54+
use msDecrypt = new MemoryStream(cipherText)
55+
use csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
56+
use srDecrypt = new StreamReader(csDecrypt)
57+
58+
// Read the decrypted bytes from the decrypting stream
59+
// and return the resulting string.
60+
srDecrypt.ReadToEnd()
61+
//</Snippet3>
62+
63+
[<EntryPoint>]
64+
let main argv =
65+
66+
let original = "Here is some data to encrypt!"
67+
68+
// Create a new instance of the Aes
69+
// class. This generates a new key and initialization
70+
// vector (IV).
71+
use myAes = Aes.Create()
72+
73+
// Encrypt the string to an array of bytes.
74+
let encrypted = encryptStringToBytes_Aes(original, myAes.Key, myAes.IV)
75+
76+
// Decrypt the bytes to a string.
77+
let roundtrip = decryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV)
78+
79+
//Display the original data and the decrypted data.
80+
Console.WriteLine("Original: {0}", original)
81+
Console.WriteLine("Round Trip: {0}", roundtrip)
82+
0
83+
//</Snippet1>

0 commit comments

Comments
 (0)