├── .gitignore
├── Building Secure Applications with Cryptography
├── AES
│ ├── AES.csproj
│ ├── AesEncryption.cs
│ ├── AesGCMEncryption.cs
│ └── Program.cs
├── Building Secure Applications with Cryptography.sln
├── DES
│ ├── DES.csproj
│ ├── DesEncryption.cs
│ └── Program.cs
├── DigitalSignature
│ ├── DigitalSignature.cs
│ ├── DigitalSignature.csproj
│ ├── NewDigitalSignature.cs
│ └── Program.cs
├── HMAC
│ ├── HMAC.cs
│ ├── HMAC.csproj
│ └── Program.cs
├── HashPassword
│ ├── Hash.cs
│ ├── HashPassword.csproj
│ └── Program.cs
├── Hashing
│ ├── Hashing.csproj
│ ├── Hashng.cs
│ └── Program.cs
├── Hybrid
│ ├── AesEncryption.cs
│ ├── EncryptedPacket.cs
│ ├── Hybrid.csproj
│ ├── HybridEncryption.cs
│ ├── Program.cs
│ └── RSAWithRSAParameterKey.cs
├── HybridWithIntegrity
│ ├── AesEncryption.cs
│ ├── EncryptedPacket.cs
│ ├── HybridEncryption.cs
│ ├── HybridWithIntegrity.csproj
│ ├── Program.cs
│ └── RSAWithRSAParameterKey.cs
├── HybridWithIntegrityAndSignature
│ ├── AesEncryption.cs
│ ├── DigitalSignature.cs
│ ├── EncryptedPacket.cs
│ ├── HybridEncryption.cs
│ ├── HybridWithIntegrityAndSignature.csproj
│ ├── Program.cs
│ └── RSAWithRSAParameterKey.cs
├── HybridWithIntegrityAndSignatureGCM
│ ├── AesGCMEncryption.cs
│ ├── EncryptedPacket.cs
│ ├── HybridEncryption.cs
│ ├── HybridWithIntegrityAndSignatureGCM.csproj
│ ├── NewDigitalSignature.cs
│ ├── NewRSA.cs
│ └── Program.cs
├── PBKDF2
│ ├── PBKDF2.cs
│ ├── PBKDF2.csproj
│ └── Program.cs
├── ProofOfWorkTest
│ ├── Program.cs
│ ├── ProofOfWork.cs
│ └── ProofOfWorkTest.csproj
├── ProtectedDataExample
│ ├── AesGCMEncryption.cs
│ ├── Program.cs
│ ├── Protected.cs
│ └── ProtectedDataExample.csproj
├── RSA
│ ├── NewRSA.cs
│ ├── Program.cs
│ ├── RSA.csproj
│ ├── RSAWithCSPKey.cs
│ └── RSAWithRSAParameterKey.cs
├── RandomNumber
│ ├── Program.cs
│ ├── Random.cs
│ └── RandomNumber.csproj
└── TripleDES
│ ├── Program.cs
│ ├── TripleDES.csproj
│ └── TripleDesEncryption.cs
└── LICENSE
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Aa][Rr][Mm]/
27 | [Aa][Rr][Mm]64/
28 | bld/
29 | [Bb]in/
30 | [Oo]bj/
31 | [Ll]og/
32 | [Ll]ogs/
33 |
34 | # Visual Studio 2015/2017 cache/options directory
35 | .vs/
36 | # Uncomment if you have tasks that create the project's static files in wwwroot
37 | #wwwroot/
38 |
39 | # Visual Studio 2017 auto generated files
40 | Generated\ Files/
41 |
42 | # MSTest test Results
43 | [Tt]est[Rr]esult*/
44 | [Bb]uild[Ll]og.*
45 |
46 | # NUnit
47 | *.VisualState.xml
48 | TestResult.xml
49 | nunit-*.xml
50 |
51 | # Build Results of an ATL Project
52 | [Dd]ebugPS/
53 | [Rr]eleasePS/
54 | dlldata.c
55 |
56 | # Benchmark Results
57 | BenchmarkDotNet.Artifacts/
58 |
59 | # .NET Core
60 | project.lock.json
61 | project.fragment.lock.json
62 | artifacts/
63 |
64 | # StyleCop
65 | StyleCopReport.xml
66 |
67 | # Files built by Visual Studio
68 | *_i.c
69 | *_p.c
70 | *_h.h
71 | *.ilk
72 | *.meta
73 | *.obj
74 | *.iobj
75 | *.pch
76 | *.pdb
77 | *.ipdb
78 | *.pgc
79 | *.pgd
80 | *.rsp
81 | *.sbr
82 | *.tlb
83 | *.tli
84 | *.tlh
85 | *.tmp
86 | *.tmp_proj
87 | *_wpftmp.csproj
88 | *.log
89 | *.vspscc
90 | *.vssscc
91 | .builds
92 | *.pidb
93 | *.svclog
94 | *.scc
95 |
96 | # Chutzpah Test files
97 | _Chutzpah*
98 |
99 | # Visual C++ cache files
100 | ipch/
101 | *.aps
102 | *.ncb
103 | *.opendb
104 | *.opensdf
105 | *.sdf
106 | *.cachefile
107 | *.VC.db
108 | *.VC.VC.opendb
109 |
110 | # Visual Studio profiler
111 | *.psess
112 | *.vsp
113 | *.vspx
114 | *.sap
115 |
116 | # Visual Studio Trace Files
117 | *.e2e
118 |
119 | # TFS 2012 Local Workspace
120 | $tf/
121 |
122 | # Guidance Automation Toolkit
123 | *.gpState
124 |
125 | # ReSharper is a .NET coding add-in
126 | _ReSharper*/
127 | *.[Rr]e[Ss]harper
128 | *.DotSettings.user
129 |
130 | # TeamCity is a build add-in
131 | _TeamCity*
132 |
133 | # DotCover is a Code Coverage Tool
134 | *.dotCover
135 |
136 | # AxoCover is a Code Coverage Tool
137 | .axoCover/*
138 | !.axoCover/settings.json
139 |
140 | # Visual Studio code coverage results
141 | *.coverage
142 | *.coveragexml
143 |
144 | # NCrunch
145 | _NCrunch_*
146 | .*crunch*.local.xml
147 | nCrunchTemp_*
148 |
149 | # MightyMoose
150 | *.mm.*
151 | AutoTest.Net/
152 |
153 | # Web workbench (sass)
154 | .sass-cache/
155 |
156 | # Installshield output folder
157 | [Ee]xpress/
158 |
159 | # DocProject is a documentation generator add-in
160 | DocProject/buildhelp/
161 | DocProject/Help/*.HxT
162 | DocProject/Help/*.HxC
163 | DocProject/Help/*.hhc
164 | DocProject/Help/*.hhk
165 | DocProject/Help/*.hhp
166 | DocProject/Help/Html2
167 | DocProject/Help/html
168 |
169 | # Click-Once directory
170 | publish/
171 |
172 | # Publish Web Output
173 | *.[Pp]ublish.xml
174 | *.azurePubxml
175 | # Note: Comment the next line if you want to checkin your web deploy settings,
176 | # but database connection strings (with potential passwords) will be unencrypted
177 | *.pubxml
178 | *.publishproj
179 |
180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
181 | # checkin your Azure Web App publish settings, but sensitive information contained
182 | # in these scripts will be unencrypted
183 | PublishScripts/
184 |
185 | # NuGet Packages
186 | *.nupkg
187 | # NuGet Symbol Packages
188 | *.snupkg
189 | # The packages folder can be ignored because of Package Restore
190 | **/[Pp]ackages/*
191 | # except build/, which is used as an MSBuild target.
192 | !**/[Pp]ackages/build/
193 | # Uncomment if necessary however generally it will be regenerated when needed
194 | #!**/[Pp]ackages/repositories.config
195 | # NuGet v3's project.json files produces more ignorable files
196 | *.nuget.props
197 | *.nuget.targets
198 |
199 | # Microsoft Azure Build Output
200 | csx/
201 | *.build.csdef
202 |
203 | # Microsoft Azure Emulator
204 | ecf/
205 | rcf/
206 |
207 | # Windows Store app package directories and files
208 | AppPackages/
209 | BundleArtifacts/
210 | Package.StoreAssociation.xml
211 | _pkginfo.txt
212 | *.appx
213 | *.appxbundle
214 | *.appxupload
215 |
216 | # Visual Studio cache files
217 | # files ending in .cache can be ignored
218 | *.[Cc]ache
219 | # but keep track of directories ending in .cache
220 | !?*.[Cc]ache/
221 |
222 | # Others
223 | ClientBin/
224 | ~$*
225 | *~
226 | *.dbmdl
227 | *.dbproj.schemaview
228 | *.jfm
229 | *.pfx
230 | *.publishsettings
231 | orleans.codegen.cs
232 |
233 | # Including strong name files can present a security risk
234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
235 | #*.snk
236 |
237 | # Since there are multiple workflows, uncomment next line to ignore bower_components
238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
239 | #bower_components/
240 |
241 | # RIA/Silverlight projects
242 | Generated_Code/
243 |
244 | # Backup & report files from converting an old project file
245 | # to a newer Visual Studio version. Backup files are not needed,
246 | # because we have git ;-)
247 | _UpgradeReport_Files/
248 | Backup*/
249 | UpgradeLog*.XML
250 | UpgradeLog*.htm
251 | ServiceFabricBackup/
252 | *.rptproj.bak
253 |
254 | # SQL Server files
255 | *.mdf
256 | *.ldf
257 | *.ndf
258 |
259 | # Business Intelligence projects
260 | *.rdl.data
261 | *.bim.layout
262 | *.bim_*.settings
263 | *.rptproj.rsuser
264 | *- [Bb]ackup.rdl
265 | *- [Bb]ackup ([0-9]).rdl
266 | *- [Bb]ackup ([0-9][0-9]).rdl
267 |
268 | # Microsoft Fakes
269 | FakesAssemblies/
270 |
271 | # GhostDoc plugin setting file
272 | *.GhostDoc.xml
273 |
274 | # Node.js Tools for Visual Studio
275 | .ntvs_analysis.dat
276 | node_modules/
277 |
278 | # Visual Studio 6 build log
279 | *.plg
280 |
281 | # Visual Studio 6 workspace options file
282 | *.opt
283 |
284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
285 | *.vbw
286 |
287 | # Visual Studio LightSwitch build output
288 | **/*.HTMLClient/GeneratedArtifacts
289 | **/*.DesktopClient/GeneratedArtifacts
290 | **/*.DesktopClient/ModelManifest.xml
291 | **/*.Server/GeneratedArtifacts
292 | **/*.Server/ModelManifest.xml
293 | _Pvt_Extensions
294 |
295 | # Paket dependency manager
296 | .paket/paket.exe
297 | paket-files/
298 |
299 | # FAKE - F# Make
300 | .fake/
301 |
302 | # CodeRush personal settings
303 | .cr/personal
304 |
305 | # Python Tools for Visual Studio (PTVS)
306 | __pycache__/
307 | *.pyc
308 |
309 | # Cake - Uncomment if you are using it
310 | # tools/**
311 | # !tools/packages.config
312 |
313 | # Tabs Studio
314 | *.tss
315 |
316 | # Telerik's JustMock configuration file
317 | *.jmconfig
318 |
319 | # BizTalk build output
320 | *.btp.cs
321 | *.btm.cs
322 | *.odx.cs
323 | *.xsd.cs
324 |
325 | # OpenCover UI analysis results
326 | OpenCover/
327 |
328 | # Azure Stream Analytics local run output
329 | ASALocalRun/
330 |
331 | # MSBuild Binary and Structured Log
332 | *.binlog
333 |
334 | # NVidia Nsight GPU debugger configuration file
335 | *.nvuser
336 |
337 | # MFractors (Xamarin productivity tool) working folder
338 | .mfractor/
339 |
340 | # Local History for Visual Studio
341 | .localhistory/
342 |
343 | # BeatPulse healthcheck temp database
344 | healthchecksdb
345 |
346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
347 | MigrationBackup/
348 |
349 | # Ionide (cross platform F# VS Code tools) working folder
350 | .ionide/
351 |
352 | Building Secure Applications with Cryptography/.idea/.idea.SecureCodingWorkshop/.idea/
353 |
354 | Building Secure Applications with Cryptography/.idea/.idea.SecureCodingWorkshop/
355 |
356 | Building Secure Applications with Cryptography/.DS_Store
357 |
358 | .DS_Store
359 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/AES/AES.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.AES
7 |
8 |
9 |
10 | Project
11 | true
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/AES/AesEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.IO;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.AES
28 | {
29 | public class AesEncryption
30 | {
31 | public byte[] GenerateRandomNumber(int length)
32 | {
33 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
34 | {
35 | var randomNumber = new byte[length];
36 | randomNumberGenerator.GetBytes(randomNumber);
37 |
38 | return randomNumber;
39 | }
40 | }
41 |
42 | public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] iv)
43 | {
44 | using (var aes = new AesCryptoServiceProvider())
45 | {
46 | aes.Mode = CipherMode.CBC;
47 | aes.Padding = PaddingMode.PKCS7;
48 |
49 | aes.Key = key;
50 | aes.IV = iv;
51 |
52 | using (var memoryStream = new MemoryStream())
53 | {
54 | var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(),
55 | CryptoStreamMode.Write);
56 |
57 | cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
58 | cryptoStream.FlushFinalBlock();
59 |
60 | return memoryStream.ToArray();
61 | }
62 | }
63 | }
64 |
65 | public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv)
66 | {
67 | using (var aes = new AesCryptoServiceProvider())
68 | {
69 | aes.Mode = CipherMode.CBC;
70 | aes.Padding = PaddingMode.PKCS7;
71 |
72 | aes.Key = key;
73 | aes.IV = iv;
74 |
75 | using (var memoryStream = new MemoryStream())
76 | {
77 | var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(),
78 | CryptoStreamMode.Write);
79 |
80 | cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length);
81 | cryptoStream.FlushFinalBlock();
82 |
83 | var decryptBytes = memoryStream.ToArray();
84 |
85 | return decryptBytes;
86 | }
87 | }
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/AES/AesGCMEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.AES
27 | {
28 | public class AesGCMEncryption
29 | {
30 | public byte[] GenerateRandomNumber(int length)
31 | {
32 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
33 | {
34 | var randomNumber = new byte[length];
35 | randomNumberGenerator.GetBytes(randomNumber);
36 |
37 | return randomNumber;
38 | }
39 | }
40 |
41 | public (byte[], byte[]) Encrypt(byte[] dataToEncrypt, byte[] key, byte[] nonce, byte[] associatedData)
42 | {
43 | // these will be filled during the encryption
44 | byte[] tag = new byte[16];
45 | byte[] ciphertext = new byte[dataToEncrypt.Length];
46 |
47 | using (AesGcm aesGcm = new AesGcm(key))
48 | {
49 | aesGcm.Encrypt(nonce, dataToEncrypt, ciphertext, tag, associatedData);
50 | }
51 |
52 | return (ciphertext, tag);
53 | }
54 |
55 | public byte[] Decrypt(byte[] cipherText, byte[] key, byte[] nonce, byte[] tag, byte[] associatedData)
56 | {
57 | byte[] decryptedData = new byte[cipherText.Length];
58 |
59 | using (AesGcm aesGcm = new AesGcm(key))
60 | {
61 | aesGcm.Decrypt(nonce, cipherText, tag, decryptedData, associatedData);
62 | }
63 |
64 | return decryptedData;
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/AES/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 | using System.Text;
27 |
28 | namespace Pluralsight.AES
29 | {
30 | static class Program
31 | {
32 | static void Main(string[] args)
33 | {
34 | TestAesGCM();
35 |
36 | Console.WriteLine();
37 | Console.WriteLine();
38 | Console.WriteLine();
39 |
40 | TestAesCBC();
41 |
42 | Console.ReadLine();
43 | }
44 |
45 | private static void TestAesCBC()
46 | {
47 | const string original = "Text to encrypt";
48 | var aes = new AesEncryption();
49 | var key = aes.GenerateRandomNumber(32);
50 | var iv = aes.GenerateRandomNumber(16);
51 |
52 |
53 | var encrypted = aes.Encrypt(Encoding.UTF8.GetBytes(original), key, iv);
54 | var decrypted = aes.Decrypt(encrypted, key, iv);
55 |
56 | var decryptedMessage = Encoding.UTF8.GetString(decrypted);
57 |
58 | Console.WriteLine("AES Encryption Demonstration in .NET");
59 | Console.WriteLine("------------------------------------");
60 | Console.WriteLine();
61 | Console.WriteLine("Original Text = " + original);
62 | Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(encrypted));
63 | Console.WriteLine("Decrypted Text = " + decryptedMessage);
64 | }
65 |
66 | private static void TestAesGCM()
67 | {
68 | const string original = "Text to encrypt";
69 |
70 | var aesGCM = new AesGCMEncryption();
71 |
72 | var gcmKey = aesGCM.GenerateRandomNumber(32);
73 | var nonce = aesGCM.GenerateRandomNumber(12);
74 |
75 | try
76 | {
77 | (byte[] ciphereText, byte[] tag) result = aesGCM.Encrypt(Encoding.UTF8.GetBytes(original), gcmKey, nonce, Encoding.UTF8.GetBytes("some metadata"));
78 | byte[] decryptedText = aesGCM.Decrypt(result.ciphereText, gcmKey, nonce, result.tag, Encoding.UTF8.GetBytes("some metadata"));
79 |
80 | Console.WriteLine("AES GCM Encryption Demonstration in .NET");
81 | Console.WriteLine("----------------------------------------");
82 | Console.WriteLine();
83 | Console.WriteLine("Original Text = " + original);
84 | Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(result.ciphereText));
85 | Console.WriteLine("Decrypted Text = " + Encoding.UTF8.GetString(decryptedText));
86 | }
87 | catch (CryptographicException ex)
88 | {
89 | Console.WriteLine(ex.Message);
90 | }
91 | catch (ArgumentException ex)
92 | {
93 | Console.WriteLine(ex.Message);
94 | }
95 | }
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Building Secure Applications with Cryptography.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2012
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RandomNumber", "RandomNumber\RandomNumber.csproj", "{9CC5FFEF-0E29-444C-9708-49AC9A5DECE0}"
5 | EndProject
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hashing", "Hashing\Hashing.csproj", "{B36B052B-3B3B-430A-B905-B569895EE194}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HMAC", "HMAC\HMAC.csproj", "{43707132-9F94-40B4-A246-A1B73164907C}"
9 | EndProject
10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HashPassword", "HashPassword\HashPassword.csproj", "{1FB27F9D-850C-4B78-84CF-8A33C8BA421C}"
11 | EndProject
12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PBKDF2", "PBKDF2\PBKDF2.csproj", "{F0501287-D2C6-46BF-969D-507A7B8022D4}"
13 | EndProject
14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "1 - Random Numbers", "1 - Random Numbers", "{B45B3315-8CFB-41F0-B8EE-8F9AE7274C80}"
15 | EndProject
16 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2 - Hashing", "2 - Hashing", "{473583FF-2534-44CB-860E-958456FADDBF}"
17 | EndProject
18 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3 - Encryption", "3 - Encryption", "{C466F6EA-3992-4BA7-9D0D-C26415DD6384}"
19 | EndProject
20 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "4 - Digital Signatures", "4 - Digital Signatures", "{2B54F712-87D5-4C62-8E4D-E50FFBE9D4B8}"
21 | EndProject
22 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "5 - Hybrid Encryption", "5 - Hybrid Encryption", "{D0A5F757-2764-4D39-BD9F-B3406DC80058}"
23 | EndProject
24 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AES", "AES\AES.csproj", "{7ED0352A-F470-4415-A24A-318A60ABC43E}"
25 | EndProject
26 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DES", "DES\DES.csproj", "{C7BDD6EF-FC39-4D2E-84BC-1B812D21F14E}"
27 | EndProject
28 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TripleDES", "TripleDES\TripleDES.csproj", "{7B9F7EC4-AB36-4F2E-AF30-A7CB3251BCB6}"
29 | EndProject
30 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RSA", "RSA\RSA.csproj", "{E66ACF6F-11E0-4628-A126-F4440C54B291}"
31 | EndProject
32 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigitalSignature", "DigitalSignature\DigitalSignature.csproj", "{9040999A-3831-48E2-91E3-0311F024A4C6}"
33 | EndProject
34 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hybrid", "Hybrid\Hybrid.csproj", "{C26569B5-024E-4325-B02D-9F1E228D2AC9}"
35 | EndProject
36 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HybridWithIntegrity", "HybridWithIntegrity\HybridWithIntegrity.csproj", "{17E2CC11-01AC-4588-B87C-AF03B0F267CF}"
37 | EndProject
38 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HybridWithIntegrityAndSignature", "HybridWithIntegrityAndSignature\HybridWithIntegrityAndSignature.csproj", "{33E6FCC9-42C2-4792-BBEE-8C58F15B3EA0}"
39 | EndProject
40 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HybridWithIntegrityAndSignatureGCM", "HybridWithIntegrityAndSignatureGCM\HybridWithIntegrityAndSignatureGCM.csproj", "{72EE7EE1-A9F9-4171-9290-A0186E5379F6}"
41 | EndProject
42 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtectedDataExample", "ProtectedDataExample\ProtectedDataExample.csproj", "{430F6A50-8DC5-4F26-90DC-DA1631F4D749}"
43 | EndProject
44 | Global
45 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
46 | Debug|Any CPU = Debug|Any CPU
47 | Release|Any CPU = Release|Any CPU
48 | EndGlobalSection
49 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
50 | {9CC5FFEF-0E29-444C-9708-49AC9A5DECE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51 | {9CC5FFEF-0E29-444C-9708-49AC9A5DECE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
52 | {9CC5FFEF-0E29-444C-9708-49AC9A5DECE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
53 | {9CC5FFEF-0E29-444C-9708-49AC9A5DECE0}.Release|Any CPU.Build.0 = Release|Any CPU
54 | {B36B052B-3B3B-430A-B905-B569895EE194}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55 | {B36B052B-3B3B-430A-B905-B569895EE194}.Debug|Any CPU.Build.0 = Debug|Any CPU
56 | {B36B052B-3B3B-430A-B905-B569895EE194}.Release|Any CPU.ActiveCfg = Release|Any CPU
57 | {B36B052B-3B3B-430A-B905-B569895EE194}.Release|Any CPU.Build.0 = Release|Any CPU
58 | {43707132-9F94-40B4-A246-A1B73164907C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59 | {43707132-9F94-40B4-A246-A1B73164907C}.Debug|Any CPU.Build.0 = Debug|Any CPU
60 | {43707132-9F94-40B4-A246-A1B73164907C}.Release|Any CPU.ActiveCfg = Release|Any CPU
61 | {43707132-9F94-40B4-A246-A1B73164907C}.Release|Any CPU.Build.0 = Release|Any CPU
62 | {1FB27F9D-850C-4B78-84CF-8A33C8BA421C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63 | {1FB27F9D-850C-4B78-84CF-8A33C8BA421C}.Debug|Any CPU.Build.0 = Debug|Any CPU
64 | {1FB27F9D-850C-4B78-84CF-8A33C8BA421C}.Release|Any CPU.ActiveCfg = Release|Any CPU
65 | {1FB27F9D-850C-4B78-84CF-8A33C8BA421C}.Release|Any CPU.Build.0 = Release|Any CPU
66 | {F0501287-D2C6-46BF-969D-507A7B8022D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67 | {F0501287-D2C6-46BF-969D-507A7B8022D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
68 | {F0501287-D2C6-46BF-969D-507A7B8022D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
69 | {F0501287-D2C6-46BF-969D-507A7B8022D4}.Release|Any CPU.Build.0 = Release|Any CPU
70 | {7ED0352A-F470-4415-A24A-318A60ABC43E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71 | {7ED0352A-F470-4415-A24A-318A60ABC43E}.Debug|Any CPU.Build.0 = Debug|Any CPU
72 | {7ED0352A-F470-4415-A24A-318A60ABC43E}.Release|Any CPU.ActiveCfg = Release|Any CPU
73 | {7ED0352A-F470-4415-A24A-318A60ABC43E}.Release|Any CPU.Build.0 = Release|Any CPU
74 | {C7BDD6EF-FC39-4D2E-84BC-1B812D21F14E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75 | {C7BDD6EF-FC39-4D2E-84BC-1B812D21F14E}.Debug|Any CPU.Build.0 = Debug|Any CPU
76 | {C7BDD6EF-FC39-4D2E-84BC-1B812D21F14E}.Release|Any CPU.ActiveCfg = Release|Any CPU
77 | {C7BDD6EF-FC39-4D2E-84BC-1B812D21F14E}.Release|Any CPU.Build.0 = Release|Any CPU
78 | {7B9F7EC4-AB36-4F2E-AF30-A7CB3251BCB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
79 | {7B9F7EC4-AB36-4F2E-AF30-A7CB3251BCB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
80 | {7B9F7EC4-AB36-4F2E-AF30-A7CB3251BCB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
81 | {7B9F7EC4-AB36-4F2E-AF30-A7CB3251BCB6}.Release|Any CPU.Build.0 = Release|Any CPU
82 | {E66ACF6F-11E0-4628-A126-F4440C54B291}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
83 | {E66ACF6F-11E0-4628-A126-F4440C54B291}.Debug|Any CPU.Build.0 = Debug|Any CPU
84 | {E66ACF6F-11E0-4628-A126-F4440C54B291}.Release|Any CPU.ActiveCfg = Release|Any CPU
85 | {E66ACF6F-11E0-4628-A126-F4440C54B291}.Release|Any CPU.Build.0 = Release|Any CPU
86 | {9040999A-3831-48E2-91E3-0311F024A4C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
87 | {9040999A-3831-48E2-91E3-0311F024A4C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
88 | {9040999A-3831-48E2-91E3-0311F024A4C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
89 | {9040999A-3831-48E2-91E3-0311F024A4C6}.Release|Any CPU.Build.0 = Release|Any CPU
90 | {C26569B5-024E-4325-B02D-9F1E228D2AC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
91 | {C26569B5-024E-4325-B02D-9F1E228D2AC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
92 | {C26569B5-024E-4325-B02D-9F1E228D2AC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
93 | {C26569B5-024E-4325-B02D-9F1E228D2AC9}.Release|Any CPU.Build.0 = Release|Any CPU
94 | {17E2CC11-01AC-4588-B87C-AF03B0F267CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
95 | {17E2CC11-01AC-4588-B87C-AF03B0F267CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
96 | {17E2CC11-01AC-4588-B87C-AF03B0F267CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
97 | {17E2CC11-01AC-4588-B87C-AF03B0F267CF}.Release|Any CPU.Build.0 = Release|Any CPU
98 | {33E6FCC9-42C2-4792-BBEE-8C58F15B3EA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
99 | {33E6FCC9-42C2-4792-BBEE-8C58F15B3EA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
100 | {33E6FCC9-42C2-4792-BBEE-8C58F15B3EA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
101 | {33E6FCC9-42C2-4792-BBEE-8C58F15B3EA0}.Release|Any CPU.Build.0 = Release|Any CPU
102 | {72EE7EE1-A9F9-4171-9290-A0186E5379F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103 | {72EE7EE1-A9F9-4171-9290-A0186E5379F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
104 | {72EE7EE1-A9F9-4171-9290-A0186E5379F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
105 | {72EE7EE1-A9F9-4171-9290-A0186E5379F6}.Release|Any CPU.Build.0 = Release|Any CPU
106 | {430F6A50-8DC5-4F26-90DC-DA1631F4D749}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
107 | {430F6A50-8DC5-4F26-90DC-DA1631F4D749}.Debug|Any CPU.Build.0 = Debug|Any CPU
108 | {430F6A50-8DC5-4F26-90DC-DA1631F4D749}.Release|Any CPU.ActiveCfg = Release|Any CPU
109 | {430F6A50-8DC5-4F26-90DC-DA1631F4D749}.Release|Any CPU.Build.0 = Release|Any CPU
110 | EndGlobalSection
111 | GlobalSection(NestedProjects) = preSolution
112 | {9CC5FFEF-0E29-444C-9708-49AC9A5DECE0} = {B45B3315-8CFB-41F0-B8EE-8F9AE7274C80}
113 | {B36B052B-3B3B-430A-B905-B569895EE194} = {473583FF-2534-44CB-860E-958456FADDBF}
114 | {1FB27F9D-850C-4B78-84CF-8A33C8BA421C} = {473583FF-2534-44CB-860E-958456FADDBF}
115 | {43707132-9F94-40B4-A246-A1B73164907C} = {473583FF-2534-44CB-860E-958456FADDBF}
116 | {F0501287-D2C6-46BF-969D-507A7B8022D4} = {473583FF-2534-44CB-860E-958456FADDBF}
117 | {7ED0352A-F470-4415-A24A-318A60ABC43E} = {C466F6EA-3992-4BA7-9D0D-C26415DD6384}
118 | {C7BDD6EF-FC39-4D2E-84BC-1B812D21F14E} = {C466F6EA-3992-4BA7-9D0D-C26415DD6384}
119 | {7B9F7EC4-AB36-4F2E-AF30-A7CB3251BCB6} = {C466F6EA-3992-4BA7-9D0D-C26415DD6384}
120 | {E66ACF6F-11E0-4628-A126-F4440C54B291} = {C466F6EA-3992-4BA7-9D0D-C26415DD6384}
121 | {9040999A-3831-48E2-91E3-0311F024A4C6} = {2B54F712-87D5-4C62-8E4D-E50FFBE9D4B8}
122 | {C26569B5-024E-4325-B02D-9F1E228D2AC9} = {D0A5F757-2764-4D39-BD9F-B3406DC80058}
123 | {17E2CC11-01AC-4588-B87C-AF03B0F267CF} = {D0A5F757-2764-4D39-BD9F-B3406DC80058}
124 | {33E6FCC9-42C2-4792-BBEE-8C58F15B3EA0} = {D0A5F757-2764-4D39-BD9F-B3406DC80058}
125 | {72EE7EE1-A9F9-4171-9290-A0186E5379F6} = {D0A5F757-2764-4D39-BD9F-B3406DC80058}
126 | {430F6A50-8DC5-4F26-90DC-DA1631F4D749} = {C466F6EA-3992-4BA7-9D0D-C26415DD6384}
127 | EndGlobalSection
128 | EndGlobal
129 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/DES/DES.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.DES
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/DES/DesEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.IO;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.DES
28 | {
29 | public class DesEncryption
30 | {
31 | public byte[] GenerateRandomNumber(int length)
32 | {
33 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
34 | {
35 | var randomNumber = new byte[length];
36 | randomNumberGenerator.GetBytes(randomNumber);
37 |
38 | return randomNumber;
39 | }
40 | }
41 |
42 | public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] iv)
43 | {
44 | using (var des = new DESCryptoServiceProvider())
45 | {
46 | des.Mode = CipherMode.CBC;
47 | des.Padding = PaddingMode.PKCS7;
48 |
49 | des.Key = key;
50 | des.IV = iv;
51 |
52 | using (var memoryStream = new MemoryStream())
53 | {
54 | var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(),
55 | CryptoStreamMode.Write);
56 |
57 | cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
58 | cryptoStream.FlushFinalBlock();
59 |
60 | return memoryStream.ToArray();
61 | }
62 | }
63 | }
64 |
65 | public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv)
66 | {
67 | using (var des = new DESCryptoServiceProvider())
68 | {
69 | des.Mode = CipherMode.CBC;
70 | des.Padding = PaddingMode.PKCS7;
71 |
72 | des.Key = key;
73 | des.IV = iv;
74 |
75 | using (var memoryStream = new MemoryStream())
76 | {
77 | var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(),
78 | CryptoStreamMode.Write);
79 |
80 | cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length);
81 | cryptoStream.FlushFinalBlock();
82 |
83 | return memoryStream.ToArray();
84 | }
85 | }
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/DES/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Text;
26 |
27 | namespace Pluralsight.DES
28 | {
29 | static class Program
30 | {
31 | static void Main(string[] args)
32 | {
33 | var des = new DesEncryption();
34 | var key = des.GenerateRandomNumber(8);
35 | var iv = des.GenerateRandomNumber(8);
36 | const string original = "Text to encrypt";
37 |
38 | var encrypted = des.Encrypt(Encoding.UTF8.GetBytes(original), key, iv);
39 | var decrypted = des.Decrypt(encrypted, key, iv);
40 |
41 | var decryptedMessage = Encoding.UTF8.GetString(decrypted);
42 |
43 | Console.WriteLine("DES Encryption Demonstration in .NET");
44 | Console.WriteLine("------------------------------------");
45 | Console.WriteLine();
46 | Console.WriteLine("Original Text = " + original);
47 | Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(encrypted));
48 | Console.WriteLine("Decrypted Text = " + decryptedMessage);
49 |
50 | Console.ReadLine();
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/DigitalSignature/DigitalSignature.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.DigitalSignature
28 | {
29 | public class DigitalSignature
30 | {
31 | private RSAParameters _publicKey;
32 | private RSAParameters _privateKey;
33 |
34 | public void AssignNewKey()
35 | {
36 | using (var rsa = new RSACryptoServiceProvider(2048))
37 | {
38 | rsa.PersistKeyInCsp = false;
39 | _publicKey = rsa.ExportParameters(false);
40 | _privateKey = rsa.ExportParameters(true);
41 | }
42 | }
43 |
44 | public byte[] SignData(byte[] hashOfDataToSign)
45 | {
46 | using (var rsa = new RSACryptoServiceProvider())
47 | {
48 | rsa.PersistKeyInCsp = false;
49 | rsa.ImportParameters(_privateKey);
50 |
51 | var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
52 | rsaFormatter.SetHashAlgorithm("SHA256");
53 |
54 | return rsaFormatter.CreateSignature(hashOfDataToSign);
55 | }
56 | }
57 |
58 | public bool VerifySignature(byte[] hashOfDataToSign, byte[] signature)
59 | {
60 | using (var rsa = new RSACryptoServiceProvider())
61 | {
62 | rsa.ImportParameters(_publicKey);
63 |
64 | var rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
65 | rsaDeformatter.SetHashAlgorithm("SHA256");
66 |
67 | return rsaDeformatter.VerifySignature(hashOfDataToSign, signature);
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/DigitalSignature/DigitalSignature.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Pluralsight.DigitalSignature
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/DigitalSignature/NewDigitalSignature.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Security.Cryptography;
3 | using System.Text;
4 |
5 | namespace Pluralsight.DigitalSignature
6 | {
7 | class NewDigitalSignature
8 | {
9 | private RSA rsa;
10 |
11 | public NewDigitalSignature()
12 | {
13 | rsa = RSA.Create(2048);
14 | }
15 |
16 | public static byte[] ComputeHashSha256(byte[] toBeHashed)
17 | {
18 | using (var sha256 = SHA256.Create())
19 | {
20 | return sha256.ComputeHash(toBeHashed);
21 | }
22 | }
23 |
24 | public (byte[], byte[]) SignData(byte[] dataToSign)
25 | {
26 | byte[] hashOfDataToSign = ComputeHashSha256(dataToSign);
27 |
28 | return (rsa.SignHash(hashOfDataToSign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1), hashOfDataToSign);
29 | }
30 |
31 | public bool VerifySignature(byte[] signature, byte[] hashOfDataToSign)
32 | {
33 | return rsa.VerifyHash(hashOfDataToSign, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
34 | }
35 |
36 | public byte[] ExportPrivateKey(int numberOfIterations, string password)
37 | {
38 | byte[] encryptedPrivateKey = new byte[2000];
39 |
40 | PbeParameters keyParams = new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, numberOfIterations);
41 | encryptedPrivateKey = rsa.ExportEncryptedPkcs8PrivateKey(Encoding.UTF8.GetBytes(password), keyParams);
42 |
43 | return encryptedPrivateKey;
44 | }
45 |
46 | public void ImportEncryptedPrivateKey(byte[] encryptedKey, string password)
47 | {
48 | rsa.ImportEncryptedPkcs8PrivateKey(Encoding.UTF8.GetBytes(password), encryptedKey, out _);
49 | }
50 |
51 | public byte[] ExportPublicKey()
52 | {
53 | return rsa.ExportRSAPublicKey();
54 | }
55 |
56 | public void ImportPublicKey(byte[] publicKey)
57 | {
58 | rsa.ImportRSAPublicKey(publicKey, out _);
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/DigitalSignature/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 | using System.Text;
27 |
28 | namespace Pluralsight.DigitalSignature
29 | {
30 | static class Program
31 | {
32 | static void Main()
33 | {
34 | SignAndVerifyData();
35 |
36 | SignAndVerifyData2();
37 |
38 | SignAndVerifyDataWithKeyExport();
39 |
40 | Console.ReadLine();
41 | }
42 |
43 | private static void SignAndVerifyData()
44 | {
45 | var document = Encoding.UTF8.GetBytes("Document to Sign");
46 | byte[] hashedDocument;
47 |
48 | using (var sha256 = SHA256.Create())
49 | {
50 | hashedDocument = sha256.ComputeHash(document);
51 | }
52 |
53 | var digitalSignature = new DigitalSignature();
54 | digitalSignature.AssignNewKey();
55 |
56 | var signature = digitalSignature.SignData(hashedDocument);
57 | var verified = digitalSignature.VerifySignature(hashedDocument, signature);
58 |
59 | Console.WriteLine("Digital Signature Demonstration in .NET");
60 | Console.WriteLine("---------------------------------------");
61 | Console.WriteLine();
62 | Console.WriteLine();
63 | Console.WriteLine(" Original Text = " + Encoding.Default.GetString(document));
64 |
65 | Console.WriteLine();
66 | Console.WriteLine(" Digital Signature = " + Convert.ToBase64String(signature));
67 |
68 | Console.WriteLine();
69 |
70 | Console.WriteLine(verified
71 | ? "The digital signature has been correctly verified."
72 | : "The digital signature has NOT been correctly verified.");
73 | }
74 |
75 | private static void SignAndVerifyData2()
76 | {
77 | var document = Encoding.UTF8.GetBytes("Document to Sign");
78 |
79 | var digitalSignature = new NewDigitalSignature();
80 |
81 | var signature = digitalSignature.SignData(document);
82 |
83 | var valid = digitalSignature.VerifySignature(signature.Item1, signature.Item2);
84 |
85 | if (valid)
86 | {
87 | Console.WriteLine("The digital signature is VALID");
88 | }
89 | else
90 | {
91 | Console.WriteLine("The digital signature is INVALID");
92 | }
93 | }
94 |
95 | private static void SignAndVerifyDataWithKeyExport()
96 | {
97 | // Create some RSA keys and export them.
98 | var digitalSignature = new NewDigitalSignature();
99 | byte[] encryptedPrivateKey = digitalSignature.ExportPrivateKey(100000, "iwf57yn783425y");
100 | byte[] publicKey = digitalSignature.ExportPublicKey();
101 |
102 |
103 | var document = Encoding.UTF8.GetBytes("Document to Sign");
104 |
105 | // Import our existing keys
106 | var digitalSignature2 = new NewDigitalSignature();
107 | digitalSignature2.ImportPublicKey(publicKey);
108 | digitalSignature2.ImportEncryptedPrivateKey(encryptedPrivateKey, "iwf57yn783425y");
109 |
110 | var signature = digitalSignature2.SignData(document);
111 |
112 | var valid = digitalSignature2.VerifySignature(signature.Item1, signature.Item2);
113 |
114 | if (valid)
115 | {
116 | Console.WriteLine("The digital signature is VALID");
117 | }
118 | else
119 | {
120 | Console.WriteLine("The digital signature is INVALID");
121 | }
122 | }
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HMAC/HMAC.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.HMAC
27 | {
28 | public static class Hmac
29 | {
30 | private const int KeySize = 32;
31 |
32 | public static byte[] GenerateKey()
33 | {
34 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
35 | {
36 | var randomNumber = new byte[KeySize];
37 | randomNumberGenerator.GetBytes(randomNumber);
38 |
39 | return randomNumber;
40 | }
41 | }
42 |
43 | public static byte[] ComputeHmacsha256(byte[] toBeHashed, byte[] key)
44 | {
45 | using (var hmac = new HMACSHA256(key))
46 | {
47 | return hmac.ComputeHash(toBeHashed);
48 | }
49 | }
50 |
51 | public static byte[] ComputeHmacsha1(byte[] toBeHashed, byte[] key)
52 | {
53 | using (var hmac = new HMACSHA1(key))
54 | {
55 | return hmac.ComputeHash(toBeHashed);
56 | }
57 | }
58 |
59 | public static byte[] ComputeHmacsha512(byte[] toBeHashed, byte[] key)
60 | {
61 | using (var hmac = new HMACSHA512(key))
62 | {
63 | return hmac.ComputeHash(toBeHashed);
64 | }
65 | }
66 |
67 | public static byte[] ComputeHmacmd5(byte[] toBeHashed, byte[] key)
68 | {
69 | using (var hmac = new HMACMD5(key))
70 | {
71 | return hmac.ComputeHash(toBeHashed);
72 | }
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HMAC/HMAC.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.HMAC
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HMAC/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Text;
26 |
27 | namespace Pluralsight.HMAC
28 | {
29 | static class Program
30 | {
31 | static void Main()
32 | {
33 | const string originalMessage = "Original Message to hash";
34 | const string originalMessage2 = "Original xessage to hash";
35 |
36 | Console.WriteLine("HMAC Demonstration in .NET");
37 | Console.WriteLine("--------------------------");
38 | Console.WriteLine();
39 |
40 | var key = Hmac.GenerateKey();
41 |
42 | var hmacMd5Message = Hmac.ComputeHmacmd5(Encoding.UTF8.GetBytes(originalMessage), key);
43 | var hmacMd5Message2 = Hmac.ComputeHmacmd5(Encoding.UTF8.GetBytes(originalMessage2), key);
44 |
45 | var hmacSha1Message = Hmac.ComputeHmacsha1(Encoding.UTF8.GetBytes(originalMessage), key);
46 | var hmacSha1Message2 = Hmac.ComputeHmacsha1(Encoding.UTF8.GetBytes(originalMessage2), key);
47 |
48 | var hmacSha256Message = Hmac.ComputeHmacsha256(Encoding.UTF8.GetBytes(originalMessage), key);
49 | var hmacSha256Message2 = Hmac.ComputeHmacsha256(Encoding.UTF8.GetBytes(originalMessage2), key);
50 |
51 | var hmacSha512Message = Hmac.ComputeHmacsha512(Encoding.UTF8.GetBytes(originalMessage), key);
52 | var hmacSha512Message2 = Hmac.ComputeHmacsha512(Encoding.UTF8.GetBytes(originalMessage2), key);
53 |
54 | Console.WriteLine();
55 | Console.WriteLine("MD5 HMAC");
56 | Console.WriteLine();
57 | Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(hmacMd5Message));
58 | Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(hmacMd5Message2));
59 |
60 | Console.WriteLine();
61 | Console.WriteLine("SHA 1 HMAC");
62 | Console.WriteLine();
63 | Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(hmacSha1Message));
64 | Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(hmacSha1Message2));
65 |
66 | Console.WriteLine();
67 | Console.WriteLine("SHA 256 HMAC");
68 | Console.WriteLine();
69 | Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(hmacSha256Message));
70 | Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(hmacSha256Message2));
71 |
72 | Console.WriteLine();
73 | Console.WriteLine("SHA 512 HMAC");
74 | Console.WriteLine();
75 | Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(hmacSha512Message));
76 | Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(hmacSha512Message2));
77 | Console.WriteLine();
78 |
79 | Console.ReadLine();
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HashPassword/Hash.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.HashPassword
28 | {
29 | public static class Hash
30 | {
31 | public static byte[] GenerateSalt()
32 | {
33 | const int saltLength = 32;
34 |
35 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
36 | {
37 | var randomNumber = new byte[saltLength];
38 | randomNumberGenerator.GetBytes(randomNumber);
39 |
40 | return randomNumber;
41 | }
42 | }
43 |
44 | private static byte[] Combine(byte[] first, byte[] second)
45 | {
46 | var ret = new byte[first.Length + second.Length];
47 |
48 | Buffer.BlockCopy(first, 0, ret, 0, first.Length);
49 | Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
50 |
51 | return ret;
52 | }
53 |
54 | public static byte[] HashPasswordWithSalt(byte[] toBeHashed, byte[] salt)
55 | {
56 | using (var sha256 = SHA256.Create())
57 | {
58 | return sha256.ComputeHash(Combine(toBeHashed, salt));
59 | }
60 | }
61 | }
62 | }
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HashPassword/HashPassword.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.HashPassword
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HashPassword/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Text;
26 |
27 | namespace Pluralsight.HashPassword
28 | {
29 | static class Program
30 | {
31 | static void Main()
32 | {
33 | const string password = "V3ryC0mpl3xP455w0rd";
34 | var salt = Hash.GenerateSalt();
35 |
36 | Console.WriteLine("Hash Password with Salt Demonstration in .NET");
37 | Console.WriteLine("---------------------------------------------");
38 | Console.WriteLine();
39 | Console.WriteLine("Password : " + password);
40 | Console.WriteLine("Salt = " + Convert.ToBase64String(salt));
41 | Console.WriteLine();
42 |
43 | var hashedPassword1 = Hash.HashPasswordWithSalt(Encoding.UTF8.GetBytes(password), salt);
44 |
45 | Console.WriteLine();
46 | Console.WriteLine("Hashed Password = " + Convert.ToBase64String(hashedPassword1));
47 | Console.WriteLine();
48 |
49 | Console.ReadLine();
50 | }
51 | }
52 | }
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Hashing/Hashing.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.Hashing
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Hashing/Hashng.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.Hashing
27 | {
28 | public static class HashData
29 | {
30 | public static byte[] ComputeHashSha1(byte[] toBeHashed)
31 | {
32 | using (var sha1 = SHA1.Create())
33 | {
34 | return sha1.ComputeHash(toBeHashed);
35 | }
36 | }
37 |
38 | public static byte[] ComputeHashSha256(byte[] toBeHashed)
39 | {
40 | using (var sha256 = SHA256.Create())
41 | {
42 | return sha256.ComputeHash(toBeHashed);
43 | }
44 | }
45 |
46 | public static byte[] ComputeHashSha512(byte[] toBeHashed)
47 | {
48 | using (var sha512 = SHA512.Create())
49 | {
50 | return sha512.ComputeHash(toBeHashed);
51 | }
52 | }
53 |
54 | public static byte[] ComputeHashMd5(byte[] toBeHashed)
55 | {
56 | using (var md5 = MD5.Create())
57 | {
58 | return md5.ComputeHash(toBeHashed);
59 | }
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Hashing/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Text;
26 |
27 | namespace Pluralsight.Hashing
28 | {
29 | static class Program
30 | {
31 | static void Main()
32 | {
33 | const string originalMessage = "Original Message to hash";
34 | const string originalMessage2 = "Or1ginal Message to hash";
35 |
36 | Console.WriteLine("Secure HashData Demonstration in .NET");
37 | Console.WriteLine("---------------------------------");
38 | Console.WriteLine();
39 | Console.WriteLine("Original Message 1 : " + originalMessage);
40 | Console.WriteLine("Original Message 2 : " + originalMessage2);
41 | Console.WriteLine();
42 |
43 | var md5HashedMessage = HashData.ComputeHashMd5(Encoding.UTF8.GetBytes(originalMessage));
44 | var md5HashedMessage2 = HashData.ComputeHashMd5(Encoding.UTF8.GetBytes(originalMessage2));
45 |
46 | var sha1HashedMessage = HashData.ComputeHashSha1(Encoding.UTF8.GetBytes(originalMessage));
47 | var sha1HashedMessage2 = HashData.ComputeHashSha1(Encoding.UTF8.GetBytes(originalMessage2));
48 |
49 | var sha256HashedMessage = HashData.ComputeHashSha256(Encoding.UTF8.GetBytes(originalMessage));
50 | var sha256HashedMessage2 = HashData.ComputeHashSha256(Encoding.UTF8.GetBytes(originalMessage2));
51 |
52 | var sha512HashedMessage = HashData.ComputeHashSha512(Encoding.UTF8.GetBytes(originalMessage));
53 | var sha512HashedMessage2 = HashData.ComputeHashSha512(Encoding.UTF8.GetBytes(originalMessage2));
54 |
55 | Console.WriteLine();
56 | Console.WriteLine("MD5 Hashes");
57 | Console.WriteLine();
58 | Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(md5HashedMessage));
59 | Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(md5HashedMessage2));
60 | Console.WriteLine();
61 | Console.WriteLine("SHA 1 Hashes");
62 | Console.WriteLine();
63 | Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(sha1HashedMessage));
64 | Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(sha1HashedMessage2));
65 | Console.WriteLine();
66 |
67 | Console.WriteLine("SHA 256 Hashes");
68 | Console.WriteLine();
69 | Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(sha256HashedMessage));
70 | Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(sha256HashedMessage2));
71 | Console.WriteLine();
72 | Console.WriteLine("SHA 512 Hashes");
73 | Console.WriteLine();
74 | Console.WriteLine("Message 1 hash = " + Convert.ToBase64String(sha512HashedMessage));
75 | Console.WriteLine("Message 2 hash = " + Convert.ToBase64String(sha512HashedMessage2));
76 | Console.WriteLine();
77 | Console.ReadLine();
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Hybrid/AesEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.IO;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.Hybrid
28 | {
29 | public class AesEncryption
30 | {
31 | public byte[] GenerateRandomNumber(int length)
32 | {
33 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
34 | {
35 | var randomNumber = new byte[length];
36 | randomNumberGenerator.GetBytes(randomNumber);
37 |
38 | return randomNumber;
39 | }
40 | }
41 |
42 | public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] iv)
43 | {
44 | using (var aes = new AesCryptoServiceProvider())
45 | {
46 | aes.Mode = CipherMode.CBC;
47 | aes.Padding = PaddingMode.PKCS7;
48 |
49 | aes.Key = key;
50 | aes.IV = iv;
51 |
52 | using (var memoryStream = new MemoryStream())
53 | {
54 | var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(),
55 | CryptoStreamMode.Write);
56 |
57 | cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
58 | cryptoStream.FlushFinalBlock();
59 |
60 | return memoryStream.ToArray();
61 | }
62 | }
63 | }
64 |
65 | public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv)
66 | {
67 | using (var aes = new AesCryptoServiceProvider())
68 | {
69 | aes.Mode = CipherMode.CBC;
70 | aes.Padding = PaddingMode.PKCS7;
71 |
72 | aes.Key = key;
73 | aes.IV = iv;
74 |
75 | using (var memoryStream = new MemoryStream())
76 | {
77 | var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(),
78 | CryptoStreamMode.Write);
79 |
80 | cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length);
81 | cryptoStream.FlushFinalBlock();
82 |
83 | var decryptBytes = memoryStream.ToArray();
84 |
85 | return decryptBytes;
86 | }
87 | }
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Hybrid/EncryptedPacket.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 |
25 | namespace Pluralsight.Hybrid
26 | {
27 | public class EncryptedPacket
28 | {
29 | public byte[] EncryptedSessionKey;
30 | public byte[] EncryptedData;
31 | public byte[] Iv;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Hybrid/Hybrid.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.Hybrid
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Hybrid/HybridEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | namespace Pluralsight.Hybrid
25 | {
26 | public class HybridEncryption
27 | {
28 | private readonly AesEncryption _aes = new AesEncryption();
29 |
30 | public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams)
31 | {
32 | // Generate our session key.
33 | var sessionKey = _aes.GenerateRandomNumber(32);
34 |
35 | // Create the encrypted packet and generate the IV.
36 | var encryptedPacket = new EncryptedPacket { Iv = _aes.GenerateRandomNumber(16) };
37 |
38 | // Encrypt our data with AES.
39 | encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv);
40 |
41 | // Encrypt the session key with RSA
42 | encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);
43 |
44 | return encryptedPacket;
45 | }
46 |
47 | public byte[] DecryptData(EncryptedPacket encryptedPacket, RSAWithRSAParameterKey rsaParams)
48 | {
49 | // Decrypt AES Key with RSA.
50 | var decryptedSessionKey = rsaParams.DecryptData(encryptedPacket.EncryptedSessionKey);
51 |
52 | // Decrypt our data with AES using the decrypted session key.
53 | var decryptedData = _aes.Decrypt(encryptedPacket.EncryptedData,
54 | decryptedSessionKey, encryptedPacket.Iv);
55 |
56 | return decryptedData;
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Hybrid/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Text;
26 |
27 | namespace Pluralsight.Hybrid
28 | {
29 | static class Program
30 | {
31 | static void Main()
32 | {
33 | const string original = "Very secret and important information that can not fall into the wrong hands.";
34 |
35 | var rsaParams = new RSAWithRSAParameterKey();
36 | rsaParams.AssignNewKey();
37 |
38 | var hybrid = new HybridEncryption();
39 |
40 | var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams);
41 | var decrpyted = hybrid.DecryptData(encryptedBlock, rsaParams);
42 |
43 | Console.WriteLine("Hybrid Encryption Demonstration in .NET");
44 | Console.WriteLine("---------------------------------------");
45 | Console.WriteLine();
46 | Console.WriteLine("Original Message = " + original);
47 | Console.WriteLine();
48 | Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted));
49 | Console.ReadLine();
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/Hybrid/RSAWithRSAParameterKey.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.Hybrid
27 | {
28 | public class RSAWithRSAParameterKey
29 | {
30 | private RSAParameters _publicKey;
31 | private RSAParameters _privateKey;
32 |
33 | public void AssignNewKey()
34 | {
35 | using (var rsa = new RSACryptoServiceProvider(2048))
36 | {
37 | rsa.PersistKeyInCsp = false;
38 | _publicKey = rsa.ExportParameters(false);
39 | _privateKey = rsa.ExportParameters(true);
40 | }
41 | }
42 |
43 | public byte[] EncryptData(byte[] dataToEncrypt)
44 | {
45 | byte[] cipherbytes;
46 |
47 | using (var rsa = new RSACryptoServiceProvider())
48 | {
49 | rsa.PersistKeyInCsp = false;
50 | rsa.ImportParameters(_publicKey);
51 |
52 | cipherbytes = rsa.Encrypt(dataToEncrypt, true);
53 | }
54 |
55 | return cipherbytes;
56 | }
57 |
58 | public byte[] DecryptData(byte[] dataToEncrypt)
59 | {
60 | byte[] plain;
61 |
62 | using (var rsa = new RSACryptoServiceProvider())
63 | {
64 | rsa.PersistKeyInCsp = false;
65 |
66 | rsa.ImportParameters(_privateKey);
67 | plain = rsa.Decrypt(dataToEncrypt, true);
68 | }
69 |
70 | return plain;
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrity/AesEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.IO;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.HybridWithIntegrity
28 | {
29 | public class AesEncryption
30 | {
31 | public byte[] GenerateRandomNumber(int length)
32 | {
33 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
34 | {
35 | var randomNumber = new byte[length];
36 | randomNumberGenerator.GetBytes(randomNumber);
37 |
38 | return randomNumber;
39 | }
40 | }
41 |
42 | public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] iv)
43 | {
44 | using (var aes = new AesCryptoServiceProvider())
45 | {
46 | aes.Mode = CipherMode.CBC;
47 | aes.Padding = PaddingMode.PKCS7;
48 |
49 | aes.Key = key;
50 | aes.IV = iv;
51 |
52 | using (var memoryStream = new MemoryStream())
53 | {
54 | var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(),
55 | CryptoStreamMode.Write);
56 |
57 | cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
58 | cryptoStream.FlushFinalBlock();
59 |
60 | return memoryStream.ToArray();
61 | }
62 | }
63 | }
64 |
65 | public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv)
66 | {
67 | using (var aes = new AesCryptoServiceProvider())
68 | {
69 | aes.Mode = CipherMode.CBC;
70 | aes.Padding = PaddingMode.PKCS7;
71 |
72 | aes.Key = key;
73 | aes.IV = iv;
74 |
75 | using (var memoryStream = new MemoryStream())
76 | {
77 | var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(),
78 | CryptoStreamMode.Write);
79 |
80 | cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length);
81 | cryptoStream.FlushFinalBlock();
82 |
83 | var decryptBytes = memoryStream.ToArray();
84 |
85 | return decryptBytes;
86 | }
87 | }
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrity/EncryptedPacket.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | namespace Pluralsight.HybridWithIntegrity
25 | {
26 | public class EncryptedPacket
27 | {
28 | public byte[] EncryptedSessionKey;
29 | public byte[] EncryptedData;
30 | public byte[] Iv;
31 | public byte[] Hmac;
32 | }
33 | }
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrity/HybridEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.HybridWithIntegrity
28 | {
29 | public class HybridEncryption
30 | {
31 | private readonly AesEncryption _aes = new AesEncryption();
32 |
33 | public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams)
34 | {
35 | var sessionKey = _aes.GenerateRandomNumber(32);
36 | var encryptedPacket = new EncryptedPacket { Iv = _aes.GenerateRandomNumber(16) };
37 |
38 | // Encrypt data with AES and AES Key with RSA
39 | encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv);
40 | encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);
41 |
42 | using (var hmac = new HMACSHA256(sessionKey))
43 | {
44 | encryptedPacket.Hmac = hmac.ComputeHash(Combine(encryptedPacket.EncryptedData, encryptedPacket.Iv));
45 | }
46 |
47 | return encryptedPacket;
48 | }
49 |
50 | public byte[] DecryptData(EncryptedPacket encryptedPacket, RSAWithRSAParameterKey rsaParams)
51 | {
52 | // Decrypt AES Key with RSA and then decrypt data with AES.
53 | var decryptedSessionKey = rsaParams.DecryptData(encryptedPacket.EncryptedSessionKey);
54 |
55 | using (var hmac = new HMACSHA256(decryptedSessionKey))
56 | {
57 | var hmacToCheck = hmac.ComputeHash(Combine(encryptedPacket.EncryptedData, encryptedPacket.Iv));
58 |
59 | if (!Compare(encryptedPacket.Hmac, hmacToCheck))
60 | {
61 | throw new CryptographicException("HMAC for decryption does not match encrypted packet.");
62 | }
63 | }
64 |
65 | var decryptedData = _aes.Decrypt(encryptedPacket.EncryptedData, decryptedSessionKey, encryptedPacket.Iv);
66 |
67 | return decryptedData;
68 | }
69 |
70 | private static byte[] Combine(byte[] first, byte[] second)
71 | {
72 | var ret = new byte[first.Length + second.Length];
73 |
74 | Buffer.BlockCopy(first, 0, ret, 0, first.Length);
75 | Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
76 |
77 | return ret;
78 | }
79 |
80 | private static bool Compare(byte[] array1, byte[] array2)
81 | {
82 | var result = array1.Length == array2.Length;
83 |
84 | for (var i = 0; i < array1.Length && i < array2.Length; ++i)
85 | {
86 | result &= array1[i] == array2[i];
87 | }
88 |
89 | return result;
90 | }
91 |
92 | // Don't use this method for comparing byte arrays.
93 | // It is left here as an example.
94 | private static bool CompareUnSecure(byte[] array1, byte[] array2)
95 | {
96 | if (array1.Length != array2.Length)
97 | {
98 | return false;
99 | }
100 |
101 | for (int i = 0; i < array1.Length; ++i)
102 | {
103 | if (array1[i] != array2[i])
104 | {
105 | return false;
106 | }
107 | }
108 |
109 | return true;
110 | }
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrity/HybridWithIntegrity.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.HybridWithIntegrity
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrity/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 | using System.Text;
27 |
28 | namespace Pluralsight.HybridWithIntegrity
29 | {
30 | static class Program
31 | {
32 | static void Main()
33 | {
34 | const string original = "Very secret and important information that can not fall into the wrong hands.";
35 |
36 | var hybrid = new HybridEncryption();
37 |
38 | var rsaParams = new RSAWithRSAParameterKey();
39 | rsaParams.AssignNewKey();
40 |
41 | Console.WriteLine("Hybrid Encryption with Integrity Check Demonstration in .NET");
42 | Console.WriteLine("------------------------------------------------------------");
43 | Console.WriteLine();
44 |
45 | try
46 | {
47 | var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams);
48 | var decrpyted = hybrid.DecryptData(encryptedBlock, rsaParams);
49 |
50 | Console.WriteLine("Original Message = " + original);
51 | Console.WriteLine();
52 | Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted));
53 | }
54 | catch (CryptographicException ex)
55 | {
56 | Console.WriteLine("Error : " + ex.Message);
57 | }
58 |
59 | Console.ReadLine();
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrity/RSAWithRSAParameterKey.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.HybridWithIntegrity
27 | {
28 | public class RSAWithRSAParameterKey
29 | {
30 | private RSAParameters _publicKey;
31 | private RSAParameters _privateKey;
32 |
33 | public void AssignNewKey()
34 | {
35 | using (var rsa = new RSACryptoServiceProvider(2048))
36 | {
37 | rsa.PersistKeyInCsp = false;
38 | _publicKey = rsa.ExportParameters(false);
39 | _privateKey = rsa.ExportParameters(true);
40 | }
41 | }
42 |
43 | public byte[] EncryptData(byte[] dataToEncrypt)
44 | {
45 | byte[] cipherbytes;
46 |
47 | using (var rsa = new RSACryptoServiceProvider())
48 | {
49 | rsa.PersistKeyInCsp = false;
50 | rsa.ImportParameters(_publicKey);
51 |
52 | cipherbytes = rsa.Encrypt(dataToEncrypt, true);
53 | }
54 |
55 | return cipherbytes;
56 | }
57 |
58 | public byte[] DecryptData(byte[] dataToEncrypt)
59 | {
60 | byte[] plain;
61 |
62 | using (var rsa = new RSACryptoServiceProvider())
63 | {
64 | rsa.PersistKeyInCsp = false;
65 |
66 | rsa.ImportParameters(_privateKey);
67 | plain = rsa.Decrypt(dataToEncrypt, true);
68 | }
69 |
70 | return plain;
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignature/AesEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.IO;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.HybridWithIntegrityAndSignature
28 | {
29 | public class AesEncryption
30 | {
31 | public byte[] GenerateRandomNumber(int length)
32 | {
33 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
34 | {
35 | var randomNumber = new byte[length];
36 | randomNumberGenerator.GetBytes(randomNumber);
37 |
38 | return randomNumber;
39 | }
40 | }
41 |
42 | public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] iv)
43 | {
44 | using (var aes = new AesCryptoServiceProvider())
45 | {
46 | aes.Mode = CipherMode.CBC;
47 | aes.Padding = PaddingMode.PKCS7;
48 |
49 | aes.Key = key;
50 | aes.IV = iv;
51 |
52 | using (var memoryStream = new MemoryStream())
53 | {
54 | var cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(),
55 | CryptoStreamMode.Write);
56 |
57 | cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
58 | cryptoStream.FlushFinalBlock();
59 |
60 | return memoryStream.ToArray();
61 | }
62 | }
63 | }
64 |
65 | public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv)
66 | {
67 | using (var aes = new AesCryptoServiceProvider())
68 | {
69 | aes.Mode = CipherMode.CBC;
70 | aes.Padding = PaddingMode.PKCS7;
71 |
72 | aes.Key = key;
73 | aes.IV = iv;
74 |
75 | using (var memoryStream = new MemoryStream())
76 | {
77 | var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(),
78 | CryptoStreamMode.Write);
79 |
80 | cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length);
81 | cryptoStream.FlushFinalBlock();
82 |
83 | var decryptBytes = memoryStream.ToArray();
84 |
85 | return decryptBytes;
86 | }
87 | }
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignature/DigitalSignature.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.HybridWithIntegrityAndSignature
27 | {
28 | public class DigitalSignature
29 | {
30 | private RSAParameters _publicKey;
31 | private RSAParameters _privateKey;
32 |
33 | public void AssignNewKey()
34 | {
35 | using (var rsa = new RSACryptoServiceProvider(2048))
36 | {
37 | rsa.PersistKeyInCsp = false;
38 | _publicKey = rsa.ExportParameters(false);
39 | _privateKey = rsa.ExportParameters(true);
40 | }
41 | }
42 |
43 | public byte[] SignData(byte[] hashOfDataToSign)
44 | {
45 | using (var rsa = new RSACryptoServiceProvider())
46 | {
47 | rsa.PersistKeyInCsp = false;
48 | rsa.ImportParameters(_privateKey);
49 |
50 | var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
51 | rsaFormatter.SetHashAlgorithm("SHA256");
52 |
53 | return rsaFormatter.CreateSignature(hashOfDataToSign);
54 | }
55 | }
56 |
57 | public bool VerifySignature(byte[] hashOfDataToSign, byte[] signature)
58 | {
59 | using (var rsa = new RSACryptoServiceProvider())
60 | {
61 | rsa.ImportParameters(_publicKey);
62 |
63 | var rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
64 | rsaDeformatter.SetHashAlgorithm("SHA256");
65 |
66 | return rsaDeformatter.VerifySignature(hashOfDataToSign, signature);
67 | }
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignature/EncryptedPacket.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | namespace Pluralsight.HybridWithIntegrityAndSignature
25 | {
26 | public class EncryptedPacket
27 | {
28 | public byte[] EncryptedSessionKey;
29 | public byte[] EncryptedData;
30 | public byte[] Iv;
31 | public byte[] Hmac;
32 | public byte[] Signature;
33 | }
34 | }
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignature/HybridEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.HybridWithIntegrityAndSignature
28 | {
29 | public class HybridEncryption
30 | {
31 | private readonly AesEncryption _aes = new AesEncryption();
32 |
33 | public EncryptedPacket EncryptData(byte[] original, RSAWithRSAParameterKey rsaParams,
34 | DigitalSignature digitalSignature)
35 | {
36 | var sessionKey = _aes.GenerateRandomNumber(32);
37 |
38 | var encryptedPacket = new EncryptedPacket { Iv = _aes.GenerateRandomNumber(16) };
39 |
40 | encryptedPacket.EncryptedData = _aes.Encrypt(original, sessionKey, encryptedPacket.Iv);
41 |
42 | encryptedPacket.EncryptedSessionKey = rsaParams.EncryptData(sessionKey);
43 |
44 | using (var hmac = new HMACSHA256(sessionKey))
45 | {
46 | encryptedPacket.Hmac = hmac.ComputeHash(Combine(encryptedPacket.EncryptedData, encryptedPacket.Iv));
47 | }
48 |
49 | encryptedPacket.Signature = digitalSignature.SignData(encryptedPacket.Hmac);
50 |
51 | return encryptedPacket;
52 | }
53 |
54 | public byte[] DecryptData(EncryptedPacket encryptedPacket, RSAWithRSAParameterKey rsaParams,
55 | DigitalSignature digitalSignature)
56 | {
57 | var decryptedSessionKey = rsaParams.DecryptData(encryptedPacket.EncryptedSessionKey);
58 |
59 | using (var hmac = new HMACSHA256(decryptedSessionKey))
60 | {
61 | var hmacToCheck = hmac.ComputeHash(Combine(encryptedPacket.EncryptedData, encryptedPacket.Iv));
62 |
63 | if (!Compare(encryptedPacket.Hmac, hmacToCheck))
64 | {
65 | throw new CryptographicException(
66 | "HMAC for decryption does not match encrypted packet.");
67 | }
68 | }
69 |
70 | if (!digitalSignature.VerifySignature(encryptedPacket.Hmac,
71 | encryptedPacket.Signature))
72 | {
73 | throw new CryptographicException(
74 | "Digital Signature can not be verified.");
75 | }
76 |
77 | var decryptedData = _aes.Decrypt(encryptedPacket.EncryptedData, decryptedSessionKey,
78 | encryptedPacket.Iv);
79 |
80 | return decryptedData;
81 | }
82 |
83 | private static byte[] Combine(byte[] first, byte[] second)
84 | {
85 | var ret = new byte[first.Length + second.Length];
86 |
87 | Buffer.BlockCopy(first, 0, ret, 0, first.Length);
88 | Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
89 |
90 | return ret;
91 | }
92 |
93 | private static bool Compare(byte[] array1, byte[] array2)
94 | {
95 | var result = array1.Length == array2.Length;
96 |
97 | for (var i = 0; i < array1.Length && i < array2.Length; ++i)
98 | {
99 | result &= array1[i] == array2[i];
100 | }
101 |
102 | return result;
103 | }
104 |
105 | private static bool CompareUnSecure(byte[] array1, byte[] array2)
106 | {
107 | if (array1.Length != array2.Length)
108 | {
109 | return false;
110 | }
111 |
112 | for (int i = 0; i < array1.Length; ++i)
113 | {
114 | if (array1[i] != array2[i])
115 | {
116 | return false;
117 | }
118 | }
119 |
120 | return true;
121 | }
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignature/HybridWithIntegrityAndSignature.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.HybridWithIntegrityAndSignature
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignature/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 | using System.Text;
27 |
28 | namespace Pluralsight.HybridWithIntegrityAndSignature
29 | {
30 | static class Program
31 | {
32 | static void Main()
33 | {
34 | const string original = "Very secret and important information that can not fall into the wrong hands.";
35 |
36 | var hybrid = new HybridEncryption();
37 |
38 | var rsaParams = new RSAWithRSAParameterKey();
39 | rsaParams.AssignNewKey();
40 |
41 | var digitalSignature = new DigitalSignature();
42 | digitalSignature.AssignNewKey();
43 |
44 | Console.WriteLine("Hybrid Encryption with Integrity Check and Digital Signature Demonstration in .NET");
45 | Console.WriteLine("----------------------------------------------------------------------------------");
46 | Console.WriteLine();
47 |
48 | try
49 | {
50 | var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams,
51 | digitalSignature);
52 |
53 | var decrpyted = hybrid.DecryptData(encryptedBlock, rsaParams, digitalSignature);
54 |
55 | Console.WriteLine("Original Message = " + original);
56 | Console.WriteLine();
57 | Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted));
58 | }
59 | catch (CryptographicException ex)
60 | {
61 | Console.WriteLine("Error : " + ex.Message);
62 | }
63 |
64 | Console.ReadLine();
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignature/RSAWithRSAParameterKey.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.HybridWithIntegrityAndSignature
27 | {
28 | public class RSAWithRSAParameterKey
29 | {
30 | private RSAParameters _publicKey;
31 | private RSAParameters _privateKey;
32 |
33 | public void AssignNewKey()
34 | {
35 | using (var rsa = new RSACryptoServiceProvider(2048))
36 | {
37 | rsa.PersistKeyInCsp = false;
38 | _publicKey = rsa.ExportParameters(false);
39 | _privateKey = rsa.ExportParameters(true);
40 | }
41 | }
42 |
43 | public byte[] EncryptData(byte[] dataToEncrypt)
44 | {
45 | byte[] cipherbytes;
46 |
47 | using (var rsa = new RSACryptoServiceProvider())
48 | {
49 | rsa.PersistKeyInCsp = false;
50 | rsa.ImportParameters(_publicKey);
51 |
52 | cipherbytes = rsa.Encrypt(dataToEncrypt, true);
53 | }
54 |
55 | return cipherbytes;
56 | }
57 |
58 | public byte[] DecryptData(byte[] dataToEncrypt)
59 | {
60 | byte[] plain;
61 |
62 | using (var rsa = new RSACryptoServiceProvider())
63 | {
64 | rsa.PersistKeyInCsp = false;
65 |
66 | rsa.ImportParameters(_privateKey);
67 | plain = rsa.Decrypt(dataToEncrypt, true);
68 | }
69 |
70 | return plain;
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignatureGCM/AesGCMEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.HybridWithIntegrityAndSignatureGCM
27 | {
28 | public class AesGCMEncryption
29 | {
30 | public byte[] GenerateRandomNumber(int length)
31 | {
32 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
33 | {
34 | var randomNumber = new byte[length];
35 | randomNumberGenerator.GetBytes(randomNumber);
36 |
37 | return randomNumber;
38 | }
39 | }
40 |
41 | public (byte[], byte[]) Encrypt(byte[] dataToEncrypt, byte[] key, byte[] nonce, byte[] associatedData)
42 | {
43 | // these will be filled during the encryption
44 | byte[] tag = new byte[16];
45 | byte[] ciphertext = new byte[dataToEncrypt.Length];
46 |
47 | using (AesGcm aesGcm = new AesGcm(key))
48 | {
49 | aesGcm.Encrypt(nonce, dataToEncrypt, ciphertext, tag, associatedData);
50 | }
51 |
52 | return (ciphertext, tag);
53 | }
54 |
55 | public byte[] Decrypt(byte[] cipherText, byte[] key, byte[] nonce, byte[] tag, byte[] associatedData)
56 | {
57 | byte[] decryptedData = new byte[cipherText.Length];
58 |
59 | using (AesGcm aesGcm = new AesGcm(key))
60 | {
61 | aesGcm.Decrypt(nonce, cipherText, tag, decryptedData, associatedData);
62 | }
63 |
64 | return decryptedData;
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignatureGCM/EncryptedPacket.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | namespace Pluralsight.HybridWithIntegrityAndSignatureGCM
25 | {
26 | public class EncryptedPacket
27 | {
28 | public byte[] EncryptedSessionKey;
29 | public byte[] EncryptedData;
30 | public byte[] Iv;
31 | public byte[] Tag;
32 | public byte[] SignatureHMAC;
33 | public byte[] Signature;
34 | }
35 | }
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignatureGCM/HybridEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.HybridWithIntegrityAndSignatureGCM
28 | {
29 | public class HybridEncryption
30 | {
31 | private readonly AesGCMEncryption _aes = new AesGCMEncryption();
32 |
33 | public static byte[] ComputeHMACSha256(byte[] toBeHashed, byte[] hmacKey)
34 | {
35 | using (var hmacSha256 = new HMACSHA256(hmacKey))
36 | {
37 | return hmacSha256.ComputeHash(toBeHashed);
38 | }
39 | }
40 |
41 | public EncryptedPacket EncryptData(byte[] original, NewRSA rsaParams,
42 | NewDigitalSignature digitalSignature)
43 | {
44 | // Create AES session key.
45 | var sessionKey = _aes.GenerateRandomNumber(32);
46 |
47 | var encryptedPacket = new EncryptedPacket {
48 | Iv = _aes.GenerateRandomNumber(12) };
49 |
50 | // Encrypt data with AES-GCM
51 | (byte[] ciphereText, byte[] tag) encrypted =
52 | _aes.Encrypt(original, sessionKey, encryptedPacket.Iv, null);
53 |
54 | encryptedPacket.EncryptedData = encrypted.ciphereText;
55 |
56 | encryptedPacket.Tag = encrypted.tag;
57 |
58 | encryptedPacket.EncryptedSessionKey = rsaParams.Encrypt(sessionKey);
59 |
60 | encryptedPacket.SignatureHMAC =
61 | ComputeHMACSha256(
62 | Combine(encryptedPacket.EncryptedData, encryptedPacket.Iv),
63 | sessionKey);
64 |
65 | encryptedPacket.Signature =
66 | digitalSignature.SignData(encryptedPacket.SignatureHMAC);
67 |
68 | return encryptedPacket;
69 | }
70 |
71 | public byte[] DecryptData(EncryptedPacket encryptedPacket, NewRSA rsaParams,
72 | NewDigitalSignature digitalSignature)
73 | {
74 | var decryptedSessionKey =
75 | rsaParams.Decrypt(encryptedPacket.EncryptedSessionKey);
76 |
77 | byte[] newHMAC = ComputeHMACSha256(
78 | Combine(encryptedPacket.EncryptedData, encryptedPacket.Iv),
79 | decryptedSessionKey);
80 |
81 | if (!Compare(encryptedPacket.SignatureHMAC, newHMAC))
82 | {
83 | throw new CryptographicException(
84 | "HMAC for decryption does not match encrypted packet.");
85 | }
86 |
87 | if (!digitalSignature.VerifySignature(
88 | encryptedPacket.Signature,
89 | encryptedPacket.SignatureHMAC))
90 | {
91 | throw new CryptographicException(
92 | "Digital Signature can not be verified.");
93 | }
94 |
95 | var decryptedData = _aes.Decrypt(encryptedPacket.EncryptedData,
96 | decryptedSessionKey,
97 | encryptedPacket.Iv,
98 | encryptedPacket.Tag,
99 | null);
100 |
101 | return decryptedData;
102 | }
103 |
104 | private static byte[] Combine(byte[] first, byte[] second)
105 | {
106 | var ret = new byte[first.Length + second.Length];
107 |
108 | Buffer.BlockCopy(first, 0, ret, 0, first.Length);
109 | Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
110 |
111 | return ret;
112 | }
113 |
114 | private static bool Compare(byte[] array1, byte[] array2)
115 | {
116 | var result = array1.Length == array2.Length;
117 |
118 | for (var i = 0; i < array1.Length && i < array2.Length; ++i)
119 | {
120 | result &= array1[i] == array2[i];
121 | }
122 |
123 | return result;
124 | }
125 | }
126 | }
127 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignatureGCM/HybridWithIntegrityAndSignatureGCM.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.HybridWithIntegrityAndSignatureGCM
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignatureGCM/NewDigitalSignature.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Security.Cryptography;
3 | using System.Text;
4 |
5 | namespace Pluralsight.HybridWithIntegrityAndSignatureGCM
6 | {
7 | public class NewDigitalSignature
8 | {
9 | private RSA rsa;
10 |
11 | public NewDigitalSignature()
12 | {
13 | rsa = RSA.Create(2048);
14 | }
15 |
16 |
17 | public byte[] SignData(byte[] dataToSign)
18 | {
19 | return (rsa.SignHash(dataToSign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
20 | }
21 |
22 | public bool VerifySignature(byte[] signature, byte[] hashOfDataToSign)
23 | {
24 | return rsa.VerifyHash(hashOfDataToSign, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
25 | }
26 |
27 | public byte[] ExportPrivateKey(int numberOfIterations, string password)
28 | {
29 | byte[] encryptedPrivateKey = new byte[2000];
30 |
31 | PbeParameters keyParams = new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, numberOfIterations);
32 | encryptedPrivateKey = rsa.ExportEncryptedPkcs8PrivateKey(Encoding.UTF8.GetBytes(password), keyParams);
33 |
34 | return encryptedPrivateKey;
35 | }
36 |
37 | public void ImportEncryptedPrivateKey(byte[] encryptedKey, string password)
38 | {
39 | rsa.ImportEncryptedPkcs8PrivateKey(Encoding.UTF8.GetBytes(password), encryptedKey, out _);
40 | }
41 |
42 | public byte[] ExportPublicKey()
43 | {
44 | return rsa.ExportRSAPublicKey();
45 | }
46 |
47 | public void ImportPublicKey(byte[] publicKey)
48 | {
49 | rsa.ImportRSAPublicKey(publicKey, out _);
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignatureGCM/NewRSA.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Security.Cryptography;
3 | using System.Text;
4 |
5 | namespace Pluralsight.HybridWithIntegrityAndSignatureGCM
6 | {
7 | public class NewRSA
8 | {
9 | private RSA rsa;
10 |
11 | public NewRSA()
12 | {
13 | rsa = RSA.Create(2048);
14 | }
15 |
16 | public byte[] Encrypt(string dataToEncrypt)
17 | {
18 | return rsa.Encrypt(Encoding.UTF8.GetBytes(dataToEncrypt), RSAEncryptionPadding.OaepSHA256);
19 | }
20 |
21 | public byte[] Encrypt(byte[] dataToEncrypt)
22 | {
23 | return rsa.Encrypt(dataToEncrypt, RSAEncryptionPadding.OaepSHA256);
24 | }
25 |
26 | public byte[] Decrypt(byte[] dataToDecrypt)
27 | {
28 | return rsa.Decrypt(dataToDecrypt, RSAEncryptionPadding.OaepSHA256);
29 | }
30 |
31 | public byte[] ExportPrivateKey(int numberOfIterations, string password)
32 | {
33 | byte[] encryptedPrivateKey = new byte[2000];
34 |
35 | PbeParameters keyParams = new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, numberOfIterations);
36 | encryptedPrivateKey = rsa.ExportEncryptedPkcs8PrivateKey(Encoding.UTF8.GetBytes(password), keyParams);
37 |
38 | return encryptedPrivateKey;
39 | }
40 |
41 | public void ImportEncryptedPrivateKey(byte[] encryptedKey, string password)
42 | {
43 | rsa.ImportEncryptedPkcs8PrivateKey(Encoding.UTF8.GetBytes(password), encryptedKey, out _);
44 | }
45 |
46 | public byte[] ExportPublicKey()
47 | {
48 | return rsa.ExportRSAPublicKey();
49 | }
50 |
51 | public void ImportPublicKey(byte[] publicKey)
52 | {
53 | rsa.ImportRSAPublicKey(publicKey, out _);
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/HybridWithIntegrityAndSignatureGCM/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 | using System.Text;
27 |
28 | namespace Pluralsight.HybridWithIntegrityAndSignatureGCM
29 | {
30 | static class Program
31 | {
32 | static void Main()
33 | {
34 | const string original = "Very secret and important information that can not fall into the wrong hands.";
35 |
36 | var hybrid = new HybridEncryption();
37 |
38 | var rsaParams = new NewRSA();
39 |
40 | var digitalSignature = new NewDigitalSignature();
41 |
42 | Console.WriteLine("Hybrid Encryption with Integrity Check and Digital Signature Demonstration in .NET");
43 | Console.WriteLine("----------------------------------------------------------------------------------");
44 | Console.WriteLine();
45 |
46 | try
47 | {
48 | var encryptedBlock = hybrid.EncryptData(Encoding.UTF8.GetBytes(original), rsaParams,
49 | digitalSignature);
50 |
51 | var decrpyted = hybrid.DecryptData(encryptedBlock, rsaParams, digitalSignature);
52 |
53 | Console.WriteLine("Original Message = " + original);
54 | Console.WriteLine();
55 | Console.WriteLine("Message After Decryption = " + Encoding.UTF8.GetString(decrpyted));
56 | }
57 | catch (CryptographicException ex)
58 | {
59 | Console.WriteLine("Error : " + ex.Message);
60 | }
61 |
62 | Console.ReadLine();
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/PBKDF2/PBKDF2.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.PBKDF2
27 | {
28 | public static class PBKDF2
29 | {
30 | public static byte[] GenerateSalt()
31 | {
32 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
33 | {
34 | var randomNumber = new byte[32];
35 | randomNumberGenerator.GetBytes(randomNumber);
36 |
37 | return randomNumber;
38 | }
39 | }
40 |
41 | public static byte[] HashPassword(byte[] toBeHashed, byte[] salt, int numberOfRounds)
42 | {
43 | using (var rfc2898 = new Rfc2898DeriveBytes(toBeHashed, salt, numberOfRounds))
44 | {
45 | return rfc2898.GetBytes(20);
46 | }
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/PBKDF2/PBKDF2.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.PBKDF2
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/PBKDF2/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Diagnostics;
26 | using System.Text;
27 |
28 | namespace Pluralsight.PBKDF2
29 | {
30 | static class Program
31 | {
32 | static void Main()
33 | {
34 | const string passwordToHash = "VeryComplexPassword";
35 |
36 | Console.WriteLine("Password Based Key Derivation Function Demonstration in .NET");
37 | Console.WriteLine("------------------------------------------------------------");
38 | Console.WriteLine();
39 | Console.WriteLine("PBKDF2 Hashes");
40 | Console.WriteLine();
41 |
42 | HashPassword(passwordToHash, 100);
43 | HashPassword(passwordToHash, 1000);
44 | HashPassword(passwordToHash, 10000);
45 | HashPassword(passwordToHash, 50000);
46 | HashPassword(passwordToHash, 100000);
47 | HashPassword(passwordToHash, 200000);
48 | HashPassword(passwordToHash, 500000);
49 |
50 | Console.ReadLine();
51 | }
52 |
53 | private static void HashPassword(string passwordToHash, int numberOfRounds)
54 | {
55 | var sw = new Stopwatch();
56 |
57 | sw.Start();
58 |
59 | var hashedPassword = PBKDF2.HashPassword(Encoding.UTF8.GetBytes(passwordToHash),
60 | PBKDF2.GenerateSalt(),
61 | numberOfRounds);
62 | sw.Stop();
63 |
64 | Console.WriteLine();
65 | Console.WriteLine("Password to hash : " + passwordToHash);
66 | Console.WriteLine("Hashed Password : " + Convert.ToBase64String(hashedPassword));
67 | Console.WriteLine("Iterations <" + numberOfRounds + "> Elapsed Time : " + sw.ElapsedMilliseconds + "ms");
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/ProofOfWorkTest/Program.cs:
--------------------------------------------------------------------------------
1 | namespace BlockChainCourse.ProofOfWorkTest
2 | {
3 | class Program
4 | {
5 | static void Main(string[] args)
6 | {
7 | ProofOfWork pow0 = new ProofOfWork("Mary had a little lamb", 0);
8 | ProofOfWork pow1 = new ProofOfWork("Mary had a little lamb", 1);
9 | ProofOfWork pow2 = new ProofOfWork("Mary had a little lamb", 2);
10 | ProofOfWork pow3 = new ProofOfWork("Mary had a little lamb", 3);
11 | ProofOfWork pow4 = new ProofOfWork("Mary had a little lamb", 4);
12 | ProofOfWork pow5 = new ProofOfWork("Mary had a little lamb", 5);
13 | ProofOfWork pow6 = new ProofOfWork("Mary had a little lamb", 6);
14 |
15 | pow0.CalculateProofOfWork();
16 | pow1.CalculateProofOfWork();
17 | pow2.CalculateProofOfWork();
18 | pow3.CalculateProofOfWork();
19 | pow4.CalculateProofOfWork();
20 | pow5.CalculateProofOfWork();
21 | pow6.CalculateProofOfWork();
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/ProofOfWorkTest/ProofOfWork.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Text;
4 | using BlockChainCourse.Cryptography;
5 |
6 | namespace BlockChainCourse.ProofOfWorkTest
7 | {
8 | public class ProofOfWork
9 | {
10 | public string MyData { get; private set; }
11 | public int Difficulty { get; private set; }
12 | public int Nonce { get; private set; }
13 |
14 | public ProofOfWork(string dataToHash, int difficulty)
15 | {
16 | MyData = dataToHash;
17 | Difficulty = difficulty;
18 | }
19 |
20 | public string CalculateProofOfWork()
21 | {
22 | string difficulty = DifficultyString();
23 | Stopwatch stopWatch = new Stopwatch();
24 | stopWatch.Start();
25 |
26 | while(true)
27 | {
28 | string hashedData = Convert.ToBase64String(HashData.ComputeHashSha256(Encoding.UTF8.GetBytes(Nonce + MyData)));
29 |
30 | if (hashedData.StartsWith(difficulty, StringComparison.Ordinal))
31 | {
32 | stopWatch.Stop();
33 | TimeSpan ts = stopWatch.Elapsed;
34 |
35 | // Format and display the TimeSpan value.
36 | string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
37 |
38 | Console.WriteLine("Difficulty Level " + Difficulty + " - Nonce = " + Nonce + " - Elapsed = " + elapsedTime + " - " + hashedData);
39 | return hashedData;
40 | }
41 |
42 | Nonce++;
43 | }
44 | }
45 |
46 | private string DifficultyString()
47 | {
48 | string difficultyString = string.Empty;
49 |
50 | for (int i = 0; i < Difficulty; i++ )
51 | {
52 | difficultyString += "0";
53 | }
54 |
55 | return difficultyString;
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/ProofOfWorkTest/ProofOfWorkTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | BlockChainCourse.ProofOfWorkTest
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/ProtectedDataExample/AesGCMEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight
27 | {
28 | public class AesGCMEncryption
29 | {
30 | public byte[] GenerateRandomNumber(int length)
31 | {
32 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
33 | {
34 | var randomNumber = new byte[length];
35 | randomNumberGenerator.GetBytes(randomNumber);
36 |
37 | return randomNumber;
38 | }
39 | }
40 |
41 | public (byte[], byte[]) Encrypt(byte[] dataToEncrypt, byte[] key, byte[] nonce, byte[] associatedData)
42 | {
43 | // these will be filled during the encryption
44 | byte[] tag = new byte[16];
45 | byte[] ciphertext = new byte[dataToEncrypt.Length];
46 |
47 | using (AesGcm aesGcm = new AesGcm(key))
48 | {
49 | aesGcm.Encrypt(nonce, dataToEncrypt, ciphertext, tag, associatedData);
50 | }
51 |
52 | return (ciphertext, tag);
53 | }
54 |
55 | public byte[] Decrypt(byte[] cipherText, byte[] key, byte[] nonce, byte[] tag, byte[] associatedData)
56 | {
57 | byte[] decryptedData = new byte[cipherText.Length];
58 |
59 | using (AesGcm aesGcm = new AesGcm(key))
60 | {
61 | aesGcm.Decrypt(nonce, cipherText, tag, decryptedData, associatedData);
62 | }
63 |
64 | return decryptedData;
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/ProtectedDataExample/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Security.Cryptography;
26 | using System.Text;
27 |
28 | namespace Pluralsight.ProtectedDataExample
29 | {
30 | class Program
31 | {
32 | static AesGCMEncryption aesGCM = new AesGCMEncryption();
33 |
34 | static void Main(string[] args)
35 | {
36 | ProtectedDataTest();
37 |
38 | EncryptAndDecryptWithProtectedKey();
39 | }
40 |
41 | private static void EncryptAndDecryptWithProtectedKey()
42 | {
43 | string original = "Text to encrypt";
44 | Console.WriteLine("Original Text = ", original);
45 |
46 | // Create a key and nonce. Encrypt our text with AES/
47 | var gcmKey = aesGCM.GenerateRandomNumber(32);
48 | var nonce = aesGCM.GenerateRandomNumber(12);
49 | var result = EncryptText(original, gcmKey, nonce);
50 |
51 | // Create some entropy and protect the AES key.
52 | var entropy = aesGCM.GenerateRandomNumber(16);
53 | byte[] protectedKey = Protected.Protect(gcmKey, entropy, DataProtectionScope.CurrentUser);
54 |
55 | // Decrypt the text with AES. First the AES key has to be retrieved with DPAPI.
56 | string decryptedText = DecryptText(result.encrypted, nonce, result.tag, protectedKey, entropy);
57 | Console.WriteLine("Decrypted Text = ", decryptedText);
58 | }
59 |
60 | private static (byte [] encrypted, byte [] tag) EncryptText(string original, byte[] gcmKey, byte[] nonce)
61 | {
62 | return aesGCM.Encrypt(Encoding.UTF8.GetBytes(original), gcmKey, nonce, Encoding.UTF8.GetBytes("some metadata"));
63 | }
64 |
65 | private static string DecryptText(byte[] encrypted, byte[] nonce, byte[] tag, byte[] protectedKey, byte[] entropy)
66 | {
67 | byte[] key = Protected.Unprotect(protectedKey, entropy, DataProtectionScope.CurrentUser);
68 |
69 | byte[] decryptedText = aesGCM.Decrypt(encrypted, key, nonce, tag, Encoding.UTF8.GetBytes("some metadata"));
70 |
71 | return Encoding.UTF8.GetString(decryptedText);
72 | }
73 |
74 | private static void ProtectedDataTest()
75 | {
76 | var encrypted = Protected.Protect("Mary had a little lamb", "8wef5juy2389f4", DataProtectionScope.CurrentUser);
77 | Console.WriteLine(encrypted);
78 |
79 | var decrypted = Protected.Unprotect(encrypted, "8wef5juy2389f4", DataProtectionScope.CurrentUser);
80 | Console.WriteLine(decrypted);
81 | }
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/ProtectedDataExample/Protected.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 |
25 | using System;
26 | using System.Security.Cryptography;
27 | using System.Text;
28 |
29 | namespace Pluralsight.ProtectedDataExample
30 | {
31 | public class Protected
32 | {
33 | public static string Protect(string stringToEncrypt, string optionalEntropy, DataProtectionScope scope)
34 | {
35 | byte[] encryptedData = ProtectedData.Protect(
36 | Encoding.UTF8.GetBytes(stringToEncrypt)
37 | , optionalEntropy != null ? Encoding.UTF8.GetBytes(optionalEntropy) : null
38 | , scope);
39 |
40 | return Convert.ToBase64String(encryptedData);
41 | }
42 |
43 | public static string Unprotect(string encryptedString, string optionalEntropy, DataProtectionScope scope)
44 | {
45 | byte[] decrypted = ProtectedData.Unprotect(
46 | Convert.FromBase64String(encryptedString)
47 | , optionalEntropy != null ? Encoding.UTF8.GetBytes(optionalEntropy) : null
48 | , scope);
49 |
50 | return Encoding.UTF8.GetString(decrypted);
51 | }
52 |
53 | public static byte[] Protect(byte[] stringToEncrypt, byte[] optionalEntropy, DataProtectionScope scope)
54 | {
55 | byte[] encryptedData = ProtectedData.Protect(stringToEncrypt
56 | , optionalEntropy != null ? optionalEntropy : null
57 | , scope);
58 |
59 | return encryptedData;
60 | }
61 |
62 | public static byte[] Unprotect(byte[] encryptedString, byte[] optionalEntropy, DataProtectionScope scope)
63 | {
64 | byte[] decrypted = ProtectedData.Unprotect(encryptedString,
65 | optionalEntropy != null ? optionalEntropy : null,
66 | scope);
67 |
68 | return decrypted;
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/ProtectedDataExample/ProtectedDataExample.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Pluralsight
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/RSA/NewRSA.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Security.Cryptography;
3 | using System.Text;
4 |
5 | namespace Pluralsight.Asymetric
6 | {
7 | class NewRSA
8 | {
9 | private RSA rsa;
10 |
11 | public NewRSA()
12 | {
13 | rsa = RSA.Create(2048);
14 | }
15 |
16 | public byte[] Encrypt(string dataToEncrypt)
17 | {
18 | return rsa.Encrypt(Encoding.UTF8.GetBytes(dataToEncrypt), RSAEncryptionPadding.OaepSHA256);
19 | }
20 |
21 | public byte[] Encrypt(byte[] dataToEncrypt)
22 | {
23 | return rsa.Encrypt(dataToEncrypt, RSAEncryptionPadding.OaepSHA256);
24 | }
25 |
26 | public byte[] Decrypt(byte[] dataToDecrypt)
27 | {
28 | return rsa.Decrypt(dataToDecrypt, RSAEncryptionPadding.OaepSHA256);
29 | }
30 |
31 | public byte[] ExportPrivateKey(int numberOfIterations, string password)
32 | {
33 | byte[] encryptedPrivateKey = new byte[2000];
34 |
35 | PbeParameters keyParams = new PbeParameters(PbeEncryptionAlgorithm.Aes256Cbc, HashAlgorithmName.SHA256, numberOfIterations);
36 | encryptedPrivateKey = rsa.ExportEncryptedPkcs8PrivateKey(Encoding.UTF8.GetBytes(password), keyParams);
37 |
38 | return encryptedPrivateKey;
39 | }
40 |
41 | public void ImportEncryptedPrivateKey(byte[] encryptedKey, string password)
42 | {
43 | rsa.ImportEncryptedPkcs8PrivateKey(Encoding.UTF8.GetBytes(password), encryptedKey, out _);
44 | }
45 |
46 | public byte[] ExportPublicKey()
47 | {
48 | return rsa.ExportRSAPublicKey();
49 | }
50 |
51 | public void ImportPublicKey(byte[] publicKey)
52 | {
53 | rsa.ImportRSAPublicKey(publicKey, out _);
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/RSA/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Text;
26 |
27 | namespace Pluralsight.Asymetric
28 | {
29 | static class Program
30 | {
31 | static void Main()
32 | {
33 | RsaWithRsaParameterKey();
34 |
35 | //
36 | // Not Supported on MacOS
37 | //
38 | //RsaWithCsp();
39 |
40 |
41 | NewRSAEncryptDecrypt();
42 |
43 | NewRSAEncryptDecryptWithKeyExport();
44 |
45 | Console.ReadLine();
46 | }
47 |
48 | private static void RsaWithCsp()
49 | {
50 | var rsaCsp = new RsaWithCspKey();
51 | const string original = "Text to encrypt";
52 |
53 | rsaCsp.AssignNewKey();
54 |
55 | var encryptedCsp = rsaCsp.EncryptData(Encoding.UTF8.GetBytes(original));
56 | var decryptedCsp = rsaCsp.DecryptData(encryptedCsp);
57 |
58 | rsaCsp.DeleteKeyInCsp();
59 |
60 | Console.WriteLine();
61 | Console.WriteLine("CSP Based Key");
62 | Console.WriteLine();
63 | Console.WriteLine(" Original Text = " + original);
64 | Console.WriteLine();
65 | Console.WriteLine(" Encrypted Text = " + Convert.ToBase64String(encryptedCsp));
66 | Console.WriteLine();
67 | Console.WriteLine(" Decrypted Text = " + Encoding.Default.GetString(decryptedCsp));
68 | }
69 |
70 | private static void RsaWithRsaParameterKey()
71 | {
72 | var rsaParams = new RSAWithRSAParameterKey();
73 | const string original = "Text to encrypt";
74 |
75 | rsaParams.AssignNewKey();
76 |
77 | var encryptedRsaParams = rsaParams.EncryptData(Encoding.UTF8.GetBytes(original));
78 | var decryptedRsaParams = rsaParams.DecryptData(encryptedRsaParams);
79 |
80 |
81 | Console.WriteLine("RSA Encryption Demonstration in .NET");
82 | Console.WriteLine("------------------------------------");
83 | Console.WriteLine();
84 | Console.WriteLine("In Memory Key");
85 | Console.WriteLine();
86 | Console.WriteLine(" Original Text = " + original);
87 | Console.WriteLine();
88 | Console.WriteLine(" Encrypted Text = " + Convert.ToBase64String(encryptedRsaParams));
89 | Console.WriteLine();
90 | Console.WriteLine(" Decrypted Text = " + Encoding.Default.GetString(decryptedRsaParams));
91 | Console.WriteLine();
92 | Console.WriteLine();
93 | }
94 |
95 | private static void NewRSAEncryptDecrypt()
96 | {
97 | var rsa = new NewRSA();
98 | const string original = "Text to encrypt";
99 |
100 | var encrypted = rsa.Encrypt(original);
101 | var decrypted = rsa.Decrypt(encrypted);
102 |
103 |
104 | Console.WriteLine("New RSA Encryption Demonstration in .NET");
105 | Console.WriteLine("----------------------------------------");
106 | Console.WriteLine();
107 | Console.WriteLine(" Original Text = " + original);
108 | Console.WriteLine();
109 | Console.WriteLine(" Encrypted Text = " + Convert.ToBase64String(encrypted));
110 | Console.WriteLine();
111 | Console.WriteLine(" Decrypted Text = " + Encoding.Default.GetString(decrypted));
112 | Console.WriteLine();
113 | Console.WriteLine();
114 | }
115 |
116 | private static void NewRSAEncryptDecryptWithKeyExport()
117 | {
118 | var rsa = new NewRSA();
119 | byte[] encryptedPrivateKey = rsa.ExportPrivateKey(100000, "iwf57yn783425y");
120 | byte[] publicKey = rsa.ExportPublicKey();
121 |
122 | const string original = "Text to encrypt";
123 | var encrypted = rsa.Encrypt(original);
124 |
125 | var rsa2 = new NewRSA();
126 | rsa2.ImportPublicKey(publicKey);
127 | rsa2.ImportEncryptedPrivateKey(encryptedPrivateKey, "iwf57yn783425y");
128 |
129 | var decrypted = rsa2.Decrypt(encrypted);
130 |
131 |
132 | Console.WriteLine("New RSA Encryption With Imported Key Demonstration in .NET");
133 | Console.WriteLine("----------------------------------------------------------");
134 | Console.WriteLine();
135 | Console.WriteLine(" Original Text = " + original);
136 | Console.WriteLine();
137 | Console.WriteLine(" Encrypted Text = " + Convert.ToBase64String(encrypted));
138 | Console.WriteLine();
139 | Console.WriteLine(" Decrypted Text = " + Encoding.Default.GetString(decrypted));
140 | Console.WriteLine();
141 | Console.WriteLine();
142 | }
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/RSA/RSA.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 | Pluralsight.Asymmetric
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/RSA/RSAWithCSPKey.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.Asymetric
27 | {
28 | public class RsaWithCspKey
29 | {
30 | const string ContainerName = "MyContainer";
31 |
32 | public void AssignNewKey()
33 | {
34 | var cspParams = new CspParameters(1)
35 | {
36 | KeyContainerName = ContainerName,
37 | Flags = CspProviderFlags.UseMachineKeyStore,
38 | ProviderName = "Microsoft Strong Cryptographic Provider"
39 | };
40 |
41 | var rsa = new RSACryptoServiceProvider(cspParams) { PersistKeyInCsp = true };
42 | }
43 |
44 | public void DeleteKeyInCsp()
45 | {
46 | var cspParams = new CspParameters { KeyContainerName = ContainerName };
47 | var rsa = new RSACryptoServiceProvider(cspParams) { PersistKeyInCsp = false };
48 |
49 | rsa.Clear();
50 | }
51 |
52 | public byte[] EncryptData(byte[] dataToEncrypt)
53 | {
54 | byte[] cipherbytes;
55 |
56 | var cspParams = new CspParameters { KeyContainerName = ContainerName };
57 |
58 | using (var rsa = new RSACryptoServiceProvider(2048, cspParams))
59 | {
60 | cipherbytes = rsa.Encrypt(dataToEncrypt, false);
61 | }
62 |
63 | return cipherbytes;
64 | }
65 |
66 | public byte[] DecryptData(byte[] dataToDecrypt)
67 | {
68 | byte[] plain;
69 |
70 | var cspParams = new CspParameters { KeyContainerName = ContainerName };
71 |
72 | using (var rsa = new RSACryptoServiceProvider(2048, cspParams))
73 | {
74 | plain = rsa.Decrypt(dataToDecrypt, false);
75 | }
76 |
77 | return plain;
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/RSA/RSAWithRSAParameterKey.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.Asymetric
27 | {
28 | public class RSAWithRSAParameterKey
29 | {
30 | private RSAParameters _publicKey;
31 | private RSAParameters _privateKey;
32 |
33 | public void AssignNewKey()
34 | {
35 | using (var rsa = new RSACryptoServiceProvider(2048))
36 | {
37 | rsa.PersistKeyInCsp = false;
38 | _publicKey = rsa.ExportParameters(false);
39 | _privateKey = rsa.ExportParameters(true);
40 | }
41 | }
42 |
43 | public byte[] EncryptData(byte[] dataToEncrypt)
44 | {
45 | byte[] cipherbytes;
46 |
47 | // No need to specify key size in constructor when importing a key.
48 | using (var rsa = new RSACryptoServiceProvider())
49 | {
50 | rsa.PersistKeyInCsp = false;
51 | rsa.ImportParameters(_publicKey);
52 |
53 | cipherbytes = rsa.Encrypt(dataToEncrypt, true);
54 | }
55 |
56 | return cipherbytes;
57 | }
58 |
59 | public byte[] DecryptData(byte[] dataToEncrypt)
60 | {
61 | byte[] plain;
62 |
63 | // No need to specify key size in constructor when importing a key.
64 | using (var rsa = new RSACryptoServiceProvider())
65 | {
66 | rsa.PersistKeyInCsp = false;
67 |
68 | rsa.ImportParameters(_privateKey);
69 | plain = rsa.Decrypt(dataToEncrypt, true);
70 | }
71 |
72 | return plain;
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/RandomNumber/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 |
26 | namespace Pluralsight.RandomNumber
27 | {
28 | static class Program
29 | {
30 | static void Main()
31 | {
32 | Console.WriteLine("Random Number Demonstration in .NET");
33 | Console.WriteLine("---------------------------------");
34 | Console.WriteLine();
35 |
36 | for (var i = 0; i < 10; i++)
37 | {
38 | Console.WriteLine("Random Number " + i + " : "
39 | + Convert.ToBase64String(Random.GenerateRandomNumber(32)));
40 | }
41 |
42 | Console.ReadLine();
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/RandomNumber/Random.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.Security.Cryptography;
25 |
26 | namespace Pluralsight.RandomNumber
27 | {
28 | public static class Random
29 | {
30 | public static byte[] GenerateRandomNumber(int length)
31 | {
32 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
33 | {
34 | var randomNumber = new byte[length];
35 | randomNumberGenerator.GetBytes(randomNumber);
36 |
37 | return randomNumber;
38 | }
39 | }
40 | }
41 | }
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/RandomNumber/RandomNumber.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.RandomNumber
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/TripleDES/Program.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System;
25 | using System.Text;
26 |
27 | namespace Pluralsight.TripleDES
28 | {
29 | static class Program
30 | {
31 | static void Main()
32 | {
33 | var tripleDes = new TripleDesEncryption();
34 |
35 | //var key = trippleDes.GenerateRandomNumber(24);
36 | var key = tripleDes.GenerateRandomNumber(16);
37 |
38 | var iv = tripleDes.GenerateRandomNumber(8);
39 | const string original = "Text to encrypt";
40 |
41 | var encrypted = tripleDes.Encrypt(Encoding.UTF8.GetBytes(original), key, iv);
42 | var decrypted = tripleDes.Decrypt(encrypted, key, iv);
43 |
44 | var decryptedMessage = Encoding.UTF8.GetString(decrypted);
45 |
46 | Console.WriteLine("Triple DES Encryption Demonstration in .NET");
47 | Console.WriteLine("--------------------------------------------");
48 | Console.WriteLine();
49 | Console.WriteLine("Original Text = " + original);
50 | Console.WriteLine("Encrypted Text = " + Convert.ToBase64String(encrypted));
51 | Console.WriteLine("Decrypted Text = " + decryptedMessage);
52 |
53 | Console.ReadLine();
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/TripleDES/TripleDES.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.0
6 | Pluralsight.TripleDES
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Building Secure Applications with Cryptography/TripleDES/TripleDesEncryption.cs:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2020
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | using System.IO;
25 | using System.Security.Cryptography;
26 |
27 | namespace Pluralsight.TripleDES
28 | {
29 | public class TripleDesEncryption
30 | {
31 | public byte[] GenerateRandomNumber(int length)
32 | {
33 | using (var randomNumberGenerator = new RNGCryptoServiceProvider())
34 | {
35 | var randomNumber = new byte[length];
36 | randomNumberGenerator.GetBytes(randomNumber);
37 |
38 | return randomNumber;
39 | }
40 | }
41 |
42 | public byte[] Encrypt(byte[] dataToEncrypt, byte[] key, byte[] iv)
43 | {
44 | using (var des = new TripleDESCryptoServiceProvider())
45 | {
46 | des.Mode = CipherMode.CBC;
47 | des.Padding = PaddingMode.PKCS7;
48 |
49 | des.Key = key;
50 | des.IV = iv;
51 |
52 | using (var memoryStream = new MemoryStream())
53 | {
54 | var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(),
55 | CryptoStreamMode.Write);
56 |
57 | cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
58 | cryptoStream.FlushFinalBlock();
59 |
60 | return memoryStream.ToArray();
61 | }
62 | }
63 | }
64 |
65 | public byte[] Decrypt(byte[] dataToDecrypt, byte[] key, byte[] iv)
66 | {
67 | using (var des = new TripleDESCryptoServiceProvider())
68 | {
69 | des.Mode = CipherMode.CBC;
70 | des.Padding = PaddingMode.PKCS7;
71 |
72 | des.Key = key;
73 | des.IV = iv;
74 |
75 | using (var memoryStream = new MemoryStream())
76 | {
77 | var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(),
78 | CryptoStreamMode.Write);
79 |
80 | cryptoStream.Write(dataToDecrypt, 0, dataToDecrypt.Length);
81 | cryptoStream.FlushFinalBlock();
82 |
83 | var decryptBytes = memoryStream.ToArray();
84 |
85 | return decryptBytes;
86 | }
87 | }
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 stephenhaunts
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------