@@ -27,6 +27,36 @@ public void Aes256Encryption()
27
27
CreateZipWithEncryptedEntries ( "foo" , 256 ) ;
28
28
}
29
29
30
+ [ Test ]
31
+ [ Category ( "Encryption" ) ]
32
+ [ Category ( "Zip" ) ]
33
+ public void ZipFileAesDecryption ( )
34
+ {
35
+ var password = "password" ;
36
+
37
+ using ( var ms = new MemoryStream ( ) )
38
+ {
39
+ WriteEncryptedZipToStream ( ms , password , 256 ) ;
40
+
41
+ var zipFile = new ZipFile ( ms )
42
+ {
43
+ Password = password
44
+ } ;
45
+
46
+ foreach ( ZipEntry entry in zipFile )
47
+ {
48
+ if ( ! entry . IsFile ) continue ;
49
+
50
+ using ( var zis = zipFile . GetInputStream ( entry ) )
51
+ using ( var sr = new StreamReader ( zis , Encoding . UTF8 ) )
52
+ {
53
+ var content = sr . ReadToEnd ( ) ;
54
+ Assert . AreEqual ( DummyDataString , content , "Decompressed content does not match input data" ) ;
55
+ }
56
+ }
57
+ }
58
+ }
59
+
30
60
private static readonly string [ ] possible7zPaths = new [ ] {
31
61
// Check in PATH
32
62
"7z" , "7za" ,
@@ -74,35 +104,37 @@ public static bool TryGet7zBinPath(out string path7z)
74
104
return false ;
75
105
}
76
106
77
- public void CreateZipWithEncryptedEntries ( string password , int keySize )
107
+ public void WriteEncryptedZipToStream ( Stream stream , string password , int keySize )
78
108
{
79
- using ( var ms = new MemoryStream ( ) )
109
+ using ( var zs = new ZipOutputStream ( stream ) )
80
110
{
81
- using ( var zs = new ZipOutputStream ( ms ) )
82
- {
83
- zs . IsStreamOwner = false ;
84
- zs . SetLevel ( 9 ) ; // 0-9, 9 being the highest level of compression
85
- zs . Password = password ; // optional. Null is the same as not setting. Required if using AES.
111
+ zs . IsStreamOwner = false ;
112
+ zs . SetLevel ( 9 ) ; // 0-9, 9 being the highest level of compression
113
+ zs . Password = password ; // optional. Null is the same as not setting. Required if using AES.
86
114
87
- ZipEntry zipEntry = new ZipEntry ( "test" ) ;
88
- zipEntry . AESKeySize = keySize ;
89
- zipEntry . DateTime = DateTime . Now ;
115
+ ZipEntry zipEntry = new ZipEntry ( "test" ) ;
116
+ zipEntry . AESKeySize = keySize ;
117
+ zipEntry . DateTime = DateTime . Now ;
90
118
91
- zs . PutNextEntry ( zipEntry ) ;
119
+ zs . PutNextEntry ( zipEntry ) ;
92
120
93
- byte [ ] dummyData = Encoding . UTF8 . GetBytes ( @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
94
- Fusce bibendum diam ac nunc rutrum ornare. Maecenas blandit elit ligula, eget suscipit lectus rutrum eu.
95
- Maecenas aliquam, purus mattis pulvinar pharetra, nunc orci maximus justo, sed facilisis massa dui sed lorem.
96
- Vestibulum id iaculis leo. Duis porta ante lorem. Duis condimentum enim nec lorem tristique interdum. Fusce in faucibus libero." ) ;
121
+ byte [ ] dummyData = Encoding . UTF8 . GetBytes ( DummyDataString ) ;
97
122
98
- using ( var dummyStream = new MemoryStream ( dummyData ) )
99
- {
100
- dummyStream . CopyTo ( zs ) ;
101
- }
102
-
103
- zs . CloseEntry ( ) ;
123
+ using ( var dummyStream = new MemoryStream ( dummyData ) )
124
+ {
125
+ dummyStream . CopyTo ( zs ) ;
104
126
}
105
127
128
+ zs . CloseEntry ( ) ;
129
+ }
130
+ }
131
+
132
+ public void CreateZipWithEncryptedEntries ( string password , int keySize )
133
+ {
134
+ using ( var ms = new MemoryStream ( ) )
135
+ {
136
+ WriteEncryptedZipToStream ( ms , password , keySize ) ;
137
+
106
138
if ( TryGet7zBinPath ( out string path7z ) )
107
139
{
108
140
Console . WriteLine ( $ "Using 7z path: \" { path7z } \" ") ;
@@ -140,5 +172,10 @@ public void CreateZipWithEncryptedEntries(string password, int keySize)
140
172
}
141
173
142
174
}
175
+
176
+ const string DummyDataString = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.
177
+ Fusce bibendum diam ac nunc rutrum ornare. Maecenas blandit elit ligula, eget suscipit lectus rutrum eu.
178
+ Maecenas aliquam, purus mattis pulvinar pharetra, nunc orci maximus justo, sed facilisis massa dui sed lorem.
179
+ Vestibulum id iaculis leo. Duis porta ante lorem. Duis condimentum enim nec lorem tristique interdum. Fusce in faucibus libero." ;
143
180
}
144
181
}
0 commit comments