1111using CryptoNet . Models ;
1212using CryptoNet . Utils ;
1313
14- namespace CryptoNet . Cli
14+ namespace CryptoNet . Cli ;
15+
16+ public static class ExampleAes
1517{
18+ private const string ConfidentialDummyData = @"Some Secret Data" ;
19+
20+ private static readonly string BaseFolder = AppDomain . CurrentDomain . BaseDirectory ;
21+ private readonly static string SymmetricKeyFile = Path . Combine ( BaseFolder , $ "{ KeyType . SymmetricKey } .xml") ;
1622
17- public class ExampleAes
23+ public static void Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_SymmetricKey ( )
1824 {
19- protected ExampleAes ( ) { }
25+ ICryptoNet cryptoNet = new CryptoNetAes ( ) ;
26+ var key = cryptoNet . ExportKey ( ) ;
2027
21- private const string ConfidentialDummyData = @"Some Secret Data" ;
28+ ICryptoNet encryptClient = new CryptoNetAes ( key ) ;
29+ var encrypt = encryptClient . EncryptFromString ( ConfidentialDummyData ) ;
2230
23- private static readonly string BaseFolder = AppDomain . CurrentDomain . BaseDirectory ;
24- private readonly static string SymmetricKeyFile = Path . Combine ( BaseFolder , $ " { KeyType . SymmetricKey } .xml" ) ;
31+ ICryptoNet decryptClient = new CryptoNetAes ( key ) ;
32+ var decrypt = decryptClient . DecryptToString ( encrypt ) ;
2533
26- public static void Example_1_Encrypt_Decrypt_Content_With_SelfGenerated_SymmetricKey ( )
27- {
28- ICryptoNet cryptoNet = new CryptoNetAes ( ) ;
29- var key = cryptoNet . ExportKey ( ) ;
34+ Debug . Assert ( ConfidentialDummyData == decrypt ) ;
35+ }
36+
37+ public static void Example_2_SelfGenerated_And_Save_SymmetricKey ( )
38+ {
39+ ICryptoNet cryptoNet = new CryptoNetAes ( ) ;
40+ var file = new FileInfo ( SymmetricKeyFile ) ;
41+ cryptoNet . ExportKeyAndSave ( file ) ;
42+
43+ Debug . Assert ( File . Exists ( file . FullName ) ) ;
3044
31- ICryptoNet encryptClient = new CryptoNetAes ( key ) ;
32- var encrypt = encryptClient . EncryptFromString ( ConfidentialDummyData ) ;
45+ var encrypt = cryptoNet . EncryptFromString ( ConfidentialDummyData ) ;
3346
34- ICryptoNet decryptClient = new CryptoNetAes ( key ) ;
35- var decrypt = decryptClient . DecryptToString ( encrypt ) ;
47+ ICryptoNet cryptoNetKeyImport = new CryptoNetAes ( file ) ;
48+ var decrypt = cryptoNetKeyImport . DecryptToString ( encrypt ) ;
3649
37- Debug . Assert ( ConfidentialDummyData == decrypt ) ;
50+ Debug . Assert ( ConfidentialDummyData == decrypt ) ;
51+ }
52+
53+ public static void Example_3_Encrypt_Decrypt_Content_With_Own_SymmetricKey ( )
54+ {
55+ var symmetricKey = "12345678901234567890123456789012" ;
56+ if ( symmetricKey . Length != 32 )
57+ {
58+ Console . WriteLine ( "key should be 32 character long" ) ;
59+ Environment . Exit ( 0 ) ;
3860 }
3961
40- public static void Example_2_SelfGenerated_And_Save_SymmetricKey ( )
62+ var secret = "1234567890123456" ;
63+ if ( secret . Length != 16 )
4164 {
42- ICryptoNet cryptoNet = new CryptoNetAes ( ) ;
43- var file = new FileInfo ( SymmetricKeyFile ) ;
44- cryptoNet . ExportKeyAndSave ( file ) ;
65+ Console . WriteLine ( "key should be 16 character long" ) ;
66+ Environment . Exit ( 1 ) ;
67+ }
4568
46- Debug . Assert ( File . Exists ( file . FullName ) ) ;
69+ var key = Encoding . UTF8 . GetBytes ( symmetricKey ) ;
70+ var iv = Encoding . UTF8 . GetBytes ( secret ) ;
4771
48- var encrypt = cryptoNet . EncryptFromString ( ConfidentialDummyData ) ;
72+ ICryptoNet encryptClient = new CryptoNetAes ( key , iv ) ;
73+ var encrypt = encryptClient . EncryptFromString ( ConfidentialDummyData ) ;
4974
50- ICryptoNet cryptoNetKeyImport = new CryptoNetAes ( file ) ;
51- var decrypt = cryptoNetKeyImport . DecryptToString ( encrypt ) ;
75+ ICryptoNet decryptClient = new CryptoNetAes ( key , iv ) ;
76+ var decrypt = decryptClient . DecryptToString ( encrypt ) ;
5277
53- Debug . Assert ( ConfidentialDummyData == decrypt ) ;
54- }
78+ Debug . Assert ( ConfidentialDummyData == decrypt ) ;
79+ }
5580
56- public static void Example_3_Encrypt_Decrypt_Content_With_Own_SymmetricKey ( )
57- {
58- var symmetricKey = "12345678901234567890123456789012" ;
59- if ( symmetricKey . Length != 32 )
60- {
61- Console . WriteLine ( "key should be 32 character long" ) ;
62- Environment . Exit ( 0 ) ;
63- }
64-
65- var secret = "1234567890123456" ;
66- if ( secret . Length != 16 )
67- {
68- Console . WriteLine ( "key should be 16 character long" ) ;
69- Environment . Exit ( 1 ) ;
70- }
71-
72- var key = Encoding . UTF8 . GetBytes ( symmetricKey ) ;
73- var iv = Encoding . UTF8 . GetBytes ( secret ) ;
74-
75- ICryptoNet encryptClient = new CryptoNetAes ( key , iv ) ;
76- var encrypt = encryptClient . EncryptFromString ( ConfidentialDummyData ) ;
77-
78- ICryptoNet decryptClient = new CryptoNetAes ( key , iv ) ;
79- var decrypt = decryptClient . DecryptToString ( encrypt ) ;
80-
81- Debug . Assert ( ConfidentialDummyData == decrypt ) ;
82- }
81+ public static void Example_4_Encrypt_Decrypt_Content_With_Human_Readable_Key_Secret_SymmetricKey ( )
82+ {
83+ var symmetricKey = UniqueKeyGenerator ( "symmetricKey" ) ;
84+ var secret = new string ( UniqueKeyGenerator ( "password" ) . Take ( 16 ) . ToArray ( ) ) ;
8385
84- public static void Example_4_Encrypt_Decrypt_Content_With_Human_Readable_Key_Secret_SymmetricKey ( )
85- {
86- var symmetricKey = UniqueKeyGenerator ( "symmetricKey" ) ;
87- var secret = new string ( UniqueKeyGenerator ( "password" ) . Take ( 16 ) . ToArray ( ) ) ;
86+ var key = Encoding . UTF8 . GetBytes ( symmetricKey ) ;
87+ var iv = Encoding . UTF8 . GetBytes ( secret ) ;
8888
89- var key = Encoding . UTF8 . GetBytes ( symmetricKey ) ;
90- var iv = Encoding . UTF8 . GetBytes ( secret ) ;
89+ ICryptoNet encryptClient = new CryptoNetAes ( key , iv ) ;
90+ var encrypt = encryptClient . EncryptFromString ( ConfidentialDummyData ) ;
9191
92- ICryptoNet encryptClient = new CryptoNetAes ( key , iv ) ;
93- var encrypt = encryptClient . EncryptFromString ( ConfidentialDummyData ) ;
92+ ICryptoNet decryptClient = new CryptoNetAes ( key , iv ) ;
93+ var decrypt = decryptClient . DecryptToString ( encrypt ) ;
9494
95- ICryptoNet decryptClient = new CryptoNetAes ( key , iv ) ;
96- var decrypt = decryptClient . DecryptToString ( encrypt ) ;
95+ Debug . Assert ( ConfidentialDummyData == decrypt ) ;
96+ }
9797
98- Debug . Assert ( ConfidentialDummyData == decrypt ) ;
99- }
98+ public static void Example_5_Encrypt_And_Decrypt_PdfFile_With_SymmetricKey_Test ( string filename )
99+ {
100+ ICryptoNet cryptoNet = new CryptoNetAes ( ) ;
101+ var key = cryptoNet . ExportKey ( ) ;
100102
101- public static void Example_5_Encrypt_And_Decrypt_PdfFile_With_SymmetricKey_Test ( string filename )
102- {
103- ICryptoNet cryptoNet = new CryptoNetAes ( ) ;
104- var key = cryptoNet . ExportKey ( ) ;
103+ FileInfo fi = new FileInfo ( filename ) ;
105104
106- FileInfo fi = new FileInfo ( filename ) ;
105+ ICryptoNet encryptClient = new CryptoNetAes ( key ) ;
106+ string pdfFilePath = Path . Combine ( BaseFolder , filename ) ;
107+ byte [ ] pdfFileBytes = File . ReadAllBytes ( pdfFilePath ) ;
108+ var encrypt = encryptClient . EncryptFromBytes ( pdfFileBytes ) ;
107109
108- ICryptoNet encryptClient = new CryptoNetAes ( key ) ;
109- string pdfFilePath = Path . Combine ( BaseFolder , filename ) ;
110- byte [ ] pdfFileBytes = File . ReadAllBytes ( pdfFilePath ) ;
111- var encrypt = encryptClient . EncryptFromBytes ( pdfFileBytes ) ;
110+ ICryptoNet decryptClient = new CryptoNetAes ( key ) ;
111+ var decrypt = decryptClient . DecryptToBytes ( encrypt ) ;
112+ string pdfDecryptedFilePath = $ "TestFiles \\ { Path . GetFileNameWithoutExtension ( fi . Name ) } -decrypted. { fi . Extension } " ;
113+ File . WriteAllBytes ( pdfDecryptedFilePath , decrypt ) ;
112114
113- ICryptoNet decryptClient = new CryptoNetAes ( key ) ;
114- var decrypt = decryptClient . DecryptToBytes ( encrypt ) ;
115- string pdfDecryptedFilePath = $ "TestFiles\\ { Path . GetFileNameWithoutExtension ( fi . Name ) } -decrypted.{ fi . Extension } ";
116- File . WriteAllBytes ( pdfDecryptedFilePath , decrypt ) ;
115+ var isIdenticalFile = CryptoNetUtils . ByteArrayCompare ( pdfFileBytes , decrypt ) ;
116+ Debug . Assert ( isIdenticalFile ) ;
117+ }
117118
118- var isIdenticalFile = CryptoNetUtils . ByteArrayCompare ( pdfFileBytes , decrypt ) ;
119- Debug . Assert ( isIdenticalFile ) ;
120- }
119+ public static string UniqueKeyGenerator ( string input )
120+ {
121+ byte [ ] inputBytes = Encoding . ASCII . GetBytes ( input ) ;
122+ byte [ ] hash = MD5 . HashData ( inputBytes ) ;
121123
122- public static string UniqueKeyGenerator ( string input )
124+ StringBuilder sb = new StringBuilder ( ) ;
125+ foreach ( var t in hash )
123126 {
124- byte [ ] inputBytes = Encoding . ASCII . GetBytes ( input ) ;
125- byte [ ] hash = MD5 . HashData ( inputBytes ) ;
126-
127- StringBuilder sb = new StringBuilder ( ) ;
128- foreach ( var t in hash )
129- {
130- sb . Append ( t . ToString ( "X2" ) ) ;
131- }
132- return sb . ToString ( ) ;
127+ sb . Append ( t . ToString ( "X2" ) ) ;
133128 }
129+ return sb . ToString ( ) ;
134130 }
135131}
0 commit comments