1
1
// <SNIPPET1>
2
2
using System ;
3
- using System . IO ;
4
3
using System . Security . Cryptography ;
5
4
using System . Text ;
6
- using System . Threading . Tasks ;
5
+ using System . IO ;
7
6
8
7
class TripleDESSample2
9
8
{
10
- static async Task Main ( )
9
+ static void Main ( )
11
10
{
12
11
try
13
12
{
@@ -26,10 +25,10 @@ static async Task Main()
26
25
string original = "Here is some data to encrypt." ;
27
26
28
27
// Encrypt the string to an in-memory buffer.
29
- byte [ ] encrypted = await EncryptTextToMemory ( original , key , iv ) ;
28
+ byte [ ] encrypted = EncryptTextToMemory ( original , key , iv ) ;
30
29
31
30
// Decrypt the buffer back to a string.
32
- string decrypted = await DecryptTextFromMemory ( encrypted , key , iv ) ;
31
+ string decrypted = DecryptTextFromMemory ( encrypted , key , iv ) ;
33
32
34
33
// Display the decrypted string to the console.
35
34
Console . WriteLine ( decrypted ) ;
@@ -40,34 +39,35 @@ static async Task Main()
40
39
}
41
40
}
42
41
43
- public static async Task < byte [ ] > EncryptTextToMemory ( string text , byte [ ] key , byte [ ] iv )
42
+ public static byte [ ] EncryptTextToMemory ( string text , byte [ ] key , byte [ ] iv )
44
43
{
45
44
try
46
45
{
47
46
// Create a MemoryStream.
48
- using var memoryStream = new MemoryStream ( ) ;
49
-
50
- // Create a new TripleDES object.
51
- using ( TripleDES tripleDes = TripleDES . Create ( ) )
52
- // Create a TripleDES encryptor from the key and IV
53
- using ( ICryptoTransform encryptor = tripleDes . CreateEncryptor ( key , iv ) )
54
- // Create a CryptoStream using the MemoryStream and encryptor
55
- using ( var cryptoStream = new CryptoStream ( memoryStream , encryptor , CryptoStreamMode . Write ) )
47
+ using ( MemoryStream mStream = new MemoryStream ( ) )
56
48
{
57
- // Convert the provided string to a byte array.
58
- byte [ ] toEncrypt = Encoding . UTF8 . GetBytes ( text ) ;
49
+ // Create a new TripleDES object.
50
+ using ( TripleDES tripleDes = TripleDES . Create ( ) )
51
+ // Create a TripleDES encryptor from the key and IV
52
+ using ( ICryptoTransform encryptor = tripleDes . CreateEncryptor ( key , iv ) )
53
+ // Create a CryptoStream using the MemoryStream and encryptor
54
+ using ( var cStream = new CryptoStream ( mStream , encryptor , CryptoStreamMode . Write ) )
55
+ {
56
+ // Convert the provided string to a byte array.
57
+ byte [ ] toEncrypt = Encoding . UTF8 . GetBytes ( text ) ;
59
58
60
- // Write the byte array to the crypto stream and flush it.
61
- await cryptoStream . WriteAsync ( toEncrypt ) ;
59
+ // Write the byte array to the crypto stream and flush it.
60
+ cStream . Write ( toEncrypt , 0 , toEncrypt . Length ) ;
62
61
63
- // Ending the using statement for the CryptoStream completes the encryption.
64
- }
62
+ // Ending the using statement for the CryptoStream completes the encryption.
63
+ }
65
64
66
- // Get an array of bytes from the MemoryStream that holds the encrypted data.
67
- byte [ ] ret = memoryStream . ToArray ( ) ;
65
+ // Get an array of bytes from the MemoryStream that holds the encrypted data.
66
+ byte [ ] ret = mStream . ToArray ( ) ;
68
67
69
- // Return the encrypted buffer.
70
- return ret ;
68
+ // Return the encrypted buffer.
69
+ return ret ;
70
+ }
71
71
}
72
72
catch ( CryptographicException e )
73
73
{
@@ -76,34 +76,38 @@ public static async Task<byte[]> EncryptTextToMemory(string text, byte[] key, by
76
76
}
77
77
}
78
78
79
- public static async Task < string > DecryptTextFromMemory ( byte [ ] encrypted , byte [ ] key , byte [ ] iv )
79
+ public static string DecryptTextFromMemory ( byte [ ] encrypted , byte [ ] key , byte [ ] iv )
80
80
{
81
81
try
82
82
{
83
83
// Create a buffer to hold the decrypted data.
84
84
// TripleDES-encrypted data will always be slightly bigger than the decrypted data.
85
85
byte [ ] decrypted = new byte [ encrypted . Length ] ;
86
- Memory < byte > buffer = decrypted ;
86
+ int offset = 0 ;
87
87
88
88
// Create a new MemoryStream using the provided array of encrypted data.
89
- using var memoryStream = new MemoryStream ( encrypted ) ;
90
-
91
- // Create a new TripleDES object.
92
- using ( TripleDES tripleDes = TripleDES . Create ( ) )
93
- // Create a TripleDES decryptor from the key and IV
94
- using ( ICryptoTransform decryptor = tripleDes . CreateDecryptor ( key , iv ) )
95
- // Create a CryptoStream using the MemoryStream and decryptor
96
- using ( var cryptoStream = new CryptoStream ( memoryStream , decryptor , CryptoStreamMode . Read ) )
89
+ using ( MemoryStream mStream = new MemoryStream ( encrypted ) )
97
90
{
98
- // Keep reading from the CryptoStream until it finishes (returns 0).
99
- while ( await cryptoStream . ReadAsync ( buffer ) is var read and > 0 )
91
+ // Create a new TripleDES object.
92
+ using ( TripleDES tripleDes = TripleDES . Create ( ) )
93
+ // Create a TripleDES decryptor from the key and IV
94
+ using ( ICryptoTransform decryptor = tripleDes . CreateDecryptor ( key , iv ) )
95
+ // Create a CryptoStream using the MemoryStream and decryptor
96
+ using ( var cStream = new CryptoStream ( mStream , decryptor , CryptoStreamMode . Read ) )
100
97
{
101
- buffer = buffer . Slice ( read ) ;
98
+ // Keep reading from the CryptoStream until it finishes (returns 0).
99
+ int read = 1 ;
100
+
101
+ while ( read > 0 )
102
+ {
103
+ read = cStream . Read ( decrypted , offset , decrypted . Length - offset ) ;
104
+ offset += read ;
105
+ }
102
106
}
103
107
}
104
108
105
109
// Convert the buffer into a string and return it.
106
- return Encoding . UTF8 . GetString ( decrypted . AsSpan ( .. ^ buffer . Length ) ) ;
110
+ return Encoding . UTF8 . GetString ( decrypted , 0 , offset ) ;
107
111
}
108
112
catch ( CryptographicException e )
109
113
{
0 commit comments