11// <SNIPPET1>
22using System ;
3- using System . IO ;
43using System . Security . Cryptography ;
54using System . Text ;
6- using System . Threading . Tasks ;
5+ using System . IO ;
76
87class TripleDESSample2
98{
10- static async Task Main ( )
9+ static void Main ( )
1110 {
1211 try
1312 {
@@ -26,10 +25,10 @@ static async Task Main()
2625 string original = "Here is some data to encrypt." ;
2726
2827 // Encrypt the string to an in-memory buffer.
29- byte [ ] encrypted = await EncryptTextToMemory ( original , key , iv ) ;
28+ byte [ ] encrypted = EncryptTextToMemory ( original , key , iv ) ;
3029
3130 // Decrypt the buffer back to a string.
32- string decrypted = await DecryptTextFromMemory ( encrypted , key , iv ) ;
31+ string decrypted = DecryptTextFromMemory ( encrypted , key , iv ) ;
3332
3433 // Display the decrypted string to the console.
3534 Console . WriteLine ( decrypted ) ;
@@ -40,34 +39,35 @@ static async Task Main()
4039 }
4140 }
4241
43- public static async Task < byte [ ] > EncryptTextToMemory ( string text , byte [ ] key , byte [ ] iv )
42+ public static byte [ ] EncryptTextToMemory ( string text , byte [ ] key , byte [ ] iv )
4443 {
4544 try
4645 {
4746 // 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 ( ) )
5648 {
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 ) ;
5958
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 ) ;
6261
63- // Ending the using statement for the CryptoStream completes the encryption.
64- }
62+ // Ending the using statement for the CryptoStream completes the encryption.
63+ }
6564
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 ( ) ;
6867
69- // Return the encrypted buffer.
70- return ret ;
68+ // Return the encrypted buffer.
69+ return ret ;
70+ }
7171 }
7272 catch ( CryptographicException e )
7373 {
@@ -76,34 +76,38 @@ public static async Task<byte[]> EncryptTextToMemory(string text, byte[] key, by
7676 }
7777 }
7878
79- public static async Task < string > DecryptTextFromMemory ( byte [ ] encrypted , byte [ ] key , byte [ ] iv )
79+ public static string DecryptTextFromMemory ( byte [ ] encrypted , byte [ ] key , byte [ ] iv )
8080 {
8181 try
8282 {
8383 // Create a buffer to hold the decrypted data.
8484 // TripleDES-encrypted data will always be slightly bigger than the decrypted data.
8585 byte [ ] decrypted = new byte [ encrypted . Length ] ;
86- Memory < byte > buffer = decrypted ;
86+ int offset = 0 ;
8787
8888 // 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 ) )
9790 {
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 ) )
10097 {
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+ }
102106 }
103107 }
104108
105109 // 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 ) ;
107111 }
108112 catch ( CryptographicException e )
109113 {
0 commit comments