-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathmemoryexample.cs
More file actions
121 lines (106 loc) · 4.19 KB
/
memoryexample.cs
File metadata and controls
121 lines (106 loc) · 4.19 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// <SNIPPET1>
// WARNING: DES has a 56-bit key and is considered insecure.
// For new applications, use a NIST-approved symmetric encryption algorithm instead.
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
class DESSample2
{
static void Main()
{
try
{
byte[] key;
byte[] iv;
// Create a new DES object to generate a random key
// and initialization vector (IV).
using (DES des = DES.Create())
{
key = des.Key;
iv = des.IV;
}
// Create a string to encrypt.
string original = "Here is some data to encrypt.";
// Encrypt the string to an in-memory buffer.
byte[] encrypted = EncryptTextToMemory(original, key, iv);
// Decrypt the buffer back to a string.
string decrypted = DecryptTextFromMemory(encrypted, key, iv);
// Display the decrypted string to the console.
Console.WriteLine(decrypted);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static byte[] EncryptTextToMemory(string text, byte[] key, byte[] iv)
{
try
{
// Create a MemoryStream.
using (MemoryStream mStream = new MemoryStream())
{
// Create a new DES object.
using (DES des = DES.Create())
// Create a DES encryptor from the key and IV
using (ICryptoTransform encryptor = des.CreateEncryptor(key, iv))
// Create a CryptoStream using the MemoryStream and encryptor
using (var cStream = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write))
{
// Convert the provided string to a byte array.
byte[] toEncrypt = Encoding.UTF8.GetBytes(text);
// Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
// Ending the using statement for the CryptoStream completes the encryption.
}
// Get an array of bytes from the MemoryStream that holds the encrypted data.
byte[] ret = mStream.ToArray();
// Return the encrypted buffer.
return ret;
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
throw;
}
}
public static string DecryptTextFromMemory(byte[] encrypted, byte[] key, byte[] iv)
{
try
{
// Create a buffer to hold the decrypted data.
// DES-encrypted data will always be slightly bigger than the decrypted data.
byte[] decrypted = new byte[encrypted.Length];
int offset = 0;
// Create a new MemoryStream using the provided array of encrypted data.
using (MemoryStream mStream = new MemoryStream(encrypted))
{
// Create a new DES object.
using (DES des = DES.Create())
// Create a DES decryptor from the key and IV
using (ICryptoTransform decryptor = des.CreateDecryptor(key, iv))
// Create a CryptoStream using the MemoryStream and decryptor
using (var cStream = new CryptoStream(mStream, decryptor, CryptoStreamMode.Read))
{
// Keep reading from the CryptoStream until it finishes (returns 0).
int read = 1;
while (read > 0)
{
read = cStream.Read(decrypted, offset, decrypted.Length - offset);
offset += read;
}
}
}
// Convert the buffer into a string and return it.
return Encoding.UTF8.GetString(decrypted, 0, offset);
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
throw;
}
}
}
// </SNIPPET1>