Advertisement
D3NCE

DATEIEN VERSCHLÜSSELN IN C#

Dec 23rd, 2018
1,711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.64 KB | None | 0 0
  1. Video 1 URL: https://www.youtube.com/watch?v=_KHECgSTF5U
  2. Video 2 URL: https://www.youtube.com/watch?v=o7jjpQYRo5g
  3. Video 3 URL: https://www.youtube.com/watch?v=5oQsIoPWdl8
  4. Video 4 URL: https://www.youtube.com/watch?v=KULKmPhzifo
  5. Video 5 URL: https://www.youtube.com/watch?v=OQfDMv-jw5s
  6.  
  7. Hier findet ihr den Code der init Region:
  8.  
  9. #region init
  10. CspParameters cspp = new CspParameters();
  11. RSACryptoServiceProvider rsa;
  12.  
  13. const string EncrFolder = @"c:\AESDocs\Encrypt\";
  14. const string DecrFolder = @"c:\AESDocs\Decrypt\";
  15. const string SrcFolder = @"c:\AESDocs\docs\";
  16.  
  17. const string PubKeyFile = @"c:\AESDocs\encrypt\rsaPublicKey.txt";
  18.  
  19. const string keyName = "key01";
  20. #endregion
  21.  
  22.  
  23.  
  24. Hier findet ihr das bttn_verschlussel_click Event:
  25.  
  26. #region verschlüsseln
  27. private void bttn_verschlusseln_Click(object sender, EventArgs e)
  28. {
  29. brow_ver.Refresh();
  30. if (rsa == null)
  31. MessageBox.Show("Schlüssel nicht gesetzt!");
  32. else
  33. {
  34. openFileDialog1.InitialDirectory = SrcFolder;
  35. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  36. {
  37. string fName = openFileDialog1.FileName;
  38. if (fName != null)
  39. {
  40. FileInfo fInfo = new FileInfo(fName);
  41. string name = fInfo.FullName;
  42. EncryptFile(name);
  43. }
  44. }
  45. }
  46. }
  47. #endregion
  48.  
  49. Hier der Code zur EncryptFile() Methode:
  50.  
  51. #region verschlüsselung core
  52. private void EncryptFile(string inFile)
  53. {
  54. RijndaelManaged rjndl = new RijndaelManaged();
  55. rjndl.KeySize = 256;
  56. rjndl.BlockSize = 256;
  57. rjndl.Mode = CipherMode.CBC;
  58. ICryptoTransform transform = rjndl.CreateEncryptor();
  59.  
  60. byte[] keyEncrypted = rsa.Encrypt(rjndl.Key, false);
  61.  
  62. byte[] LenK = new byte[4];
  63. byte[] LenIV = new byte[4];
  64.  
  65. int lKey = keyEncrypted.Length;
  66. LenK = BitConverter.GetBytes(lKey);
  67. int lIV = rjndl.IV.Length;
  68. LenIV = BitConverter.GetBytes(lIV);
  69.  
  70. int startFileName = inFile.LastIndexOf("\\") + 1;
  71. string outFile = EncrFolder + inFile.Substring(startFileName, inFile.LastIndexOf(".") – startFileName) + ".d3";
  72.  
  73. using (FileStream outFs = new FileStream(outFile, FileMode.Create))
  74. {
  75.  
  76. outFs.Write(LenK, 0, 4);
  77. outFs.Write(LenIV, 0, 4);
  78. outFs.Write(keyEncrypted, 0, lKey);
  79. outFs.Write(rjndl.IV, 0, lIV);
  80.  
  81. using (CryptoStream outStreamEncrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
  82. {
  83. int count = 0;
  84. int offset = 0;
  85.  
  86. int blockSizeBytes = rjndl.BlockSize / 8;
  87. byte[] data = new byte[blockSizeBytes];
  88. int bytesRead = 0;
  89.  
  90. using (FileStream inFs = new FileStream(inFile, FileMode.Open))
  91. {
  92. do
  93. {
  94. count = inFs.Read(data, 0, blockSizeBytes);
  95. offset += count;
  96. outStreamEncrypted.Write(data, 0, count);
  97. bytesRead += blockSizeBytes;
  98. }
  99. while (count > 0);
  100. inFs.Close();
  101. }
  102. outStreamEncrypted.FlushFinalBlock();
  103. outStreamEncrypted.Close();
  104. }
  105. outFs.Close();
  106. }
  107.  
  108. }
  109. #endregion
  110.  
  111. Hier der Code für die EncryptFile() Methode:
  112.  
  113. #region verschlüsselung core
  114. private void EncryptFile(string inFile)
  115. {
  116. RijndaelManaged rjndl = new RijndaelManaged();
  117. rjndl.KeySize = 256;
  118. rjndl.BlockSize = 256;
  119. rjndl.Mode = CipherMode.CBC;
  120. ICryptoTransform transform = rjndl.CreateEncryptor();
  121.  
  122. byte[] keyEncrypted = rsa.Encrypt(rjndl.Key, false);
  123.  
  124. byte[] LenK = new byte[4];
  125. byte[] LenIV = new byte[4];
  126.  
  127. int lKey = keyEncrypted.Length;
  128. LenK = BitConverter.GetBytes(lKey);
  129. int lIV = rjndl.IV.Length;
  130. LenIV = BitConverter.GetBytes(lIV);
  131.  
  132. int startFileName = inFile.LastIndexOf("\\") + 1;
  133. string outFile = EncrFolder + inFile.Substring(startFileName, inFile.LastIndexOf(".") – startFileName) + ".d3";
  134.  
  135. using (FileStream outFs = new FileStream(outFile, FileMode.Create))
  136. {
  137.  
  138. outFs.Write(LenK, 0, 4);
  139. outFs.Write(LenIV, 0, 4);
  140. outFs.Write(keyEncrypted, 0, lKey);
  141. outFs.Write(rjndl.IV, 0, lIV);
  142.  
  143. using (CryptoStream outStreamEncrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
  144. {
  145. int count = 0;
  146. int offset = 0;
  147.  
  148. int blockSizeBytes = rjndl.BlockSize / 8;
  149. byte[] data = new byte[blockSizeBytes];
  150. int bytesRead = 0;
  151.  
  152. using (FileStream inFs = new FileStream(inFile, FileMode.Open))
  153. {
  154. do
  155. {
  156. count = inFs.Read(data, 0, blockSizeBytes);
  157. offset += count;
  158. outStreamEncrypted.Write(data, 0, count);
  159. bytesRead += blockSizeBytes;
  160. }
  161. while (count > 0);
  162. inFs.Close();
  163. }
  164. outStreamEncrypted.FlushFinalBlock();
  165. outStreamEncrypted.Close();
  166. }
  167. outFs.Close();
  168. }
  169.  
  170. }
  171. #endregion
  172.  
  173. Hier der Code für die DecryptFile() Methode:
  174.  
  175. #region entschlüsselung core
  176.  
  177. private void DecryptFile(string inFile)
  178. {
  179. RijndaelManaged rjndl = new RijndaelManaged();
  180. rjndl.KeySize = 256;
  181. rjndl.BlockSize = 256;
  182. rjndl.Mode = CipherMode.CBC;
  183.  
  184. byte[] LenK = new byte[4];
  185. byte[] LenIV = new byte[4];
  186.  
  187. string outFile = DecrFolder + inFile.Substring(0, inFile.LastIndexOf(".")) + ".txt";
  188.  
  189. using (FileStream inFs = new FileStream(EncrFolder + inFile, FileMode.Open))
  190. {
  191.  
  192. inFs.Seek(0, SeekOrigin.Begin);
  193. inFs.Seek(0, SeekOrigin.Begin);
  194. inFs.Read(LenK, 0, 3);
  195. inFs.Seek(4, SeekOrigin.Begin);
  196. inFs.Read(LenIV, 0, 3);
  197.  
  198. int lenK = BitConverter.ToInt32(LenK, 0);
  199. int lenIV = BitConverter.ToInt32(LenIV, 0);
  200.  
  201. int startC = lenK + lenIV + 8;
  202. int lenC = (int)inFs.Length – startC;
  203.  
  204. byte[] KeyEncrypted = new byte[lenK];
  205. byte[] IV = new byte[lenIV];
  206.  
  207. inFs.Seek(8, SeekOrigin.Begin);
  208. inFs.Read(KeyEncrypted, 0, lenK);
  209. inFs.Seek(8 + lenK, SeekOrigin.Begin);
  210. inFs.Read(IV, 0, lenIV);
  211. Directory.CreateDirectory(DecrFolder);
  212.  
  213. byte[] KeyDecrypted = rsa.Decrypt(KeyEncrypted, false);
  214.  
  215. ICryptoTransform transform = rjndl.CreateDecryptor(KeyDecrypted, IV);
  216.  
  217. using (FileStream outFs = new FileStream(outFile, FileMode.Create))
  218. {
  219.  
  220. int count = 0;
  221. int offset = 0;
  222.  
  223. int blockSizeBytes = rjndl.BlockSize / 8;
  224. byte[] data = new byte[blockSizeBytes];
  225.  
  226. inFs.Seek(startC, SeekOrigin.Begin);
  227. using (CryptoStream outStreamDecrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
  228. {
  229. do
  230. {
  231. count = inFs.Read(data, 0, blockSizeBytes);
  232. offset += count;
  233. outStreamDecrypted.Write(data, 0, count);
  234.  
  235. }
  236. while (count > 0);
  237.  
  238. outStreamDecrypted.FlushFinalBlock();
  239. outStreamDecrypted.Close();
  240. }
  241. outFs.Close();
  242. }
  243. inFs.Close();
  244. }
  245.  
  246. }
  247. #endregion
  248. --------------------------------------------------------------------------------------
  249. Hier findet ihr noch einmal den gesamten Code:
  250.  
  251. using System;
  252. using System.Collections.Generic;
  253. using System.ComponentModel;
  254. using System.Data;
  255. using System.Drawing;
  256. using System.IO;
  257. using System.Security.Cryptography;
  258. using System.Windows.Forms;
  259. using MaterialSkin;
  260. using MaterialSkin.Controls;
  261.  
  262. namespace d3nce_encmaster
  263. {
  264. public partial class Form1 : MaterialForm
  265. {
  266. #region init
  267. CspParameters cspp = new CspParameters();
  268. RSACryptoServiceProvider rsa;
  269.  
  270. const string EncrFolder = @"c:\AESDocs\Encrypt\";
  271. const string DecrFolder = @"c:\AESDocs\Decrypt\";
  272. const string SrcFolder = @"c:\AESDocs\docs\";
  273.  
  274. const string PubKeyFile = @"c:\AESDocs\encrypt\rsaPublicKey.txt";
  275.  
  276. const string keyName = "key01";
  277. #endregion
  278.  
  279. public Form1()
  280. {
  281. InitializeComponent();
  282. var materialSkinManager = MaterialSkinManager.Instance;
  283. materialSkinManager.AddFormToManage(this);
  284. materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
  285. materialSkinManager.ColorScheme = new ColorScheme(Primary.Indigo800, Primary.Indigo900, Primary.Indigo500, Accent.Indigo200, TextShade.WHITE);
  286. }
  287.  
  288. private void Form1_Load(object sender, EventArgs e)
  289. {
  290.  
  291. }
  292.  
  293. #region verschlüsseln
  294. private void bttn_verschlusseln_Click(object sender, EventArgs e)
  295. {
  296. brow_ver.Refresh();
  297. if (rsa == null)
  298. MessageBox.Show("Schlüssel nicht gesetzt!");
  299. else
  300. {
  301. openFileDialog1.InitialDirectory = SrcFolder;
  302. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  303. {
  304. string fName = openFileDialog1.FileName;
  305. if (fName != null)
  306. {
  307. FileInfo fInfo = new FileInfo(fName);
  308. string name = fInfo.FullName;
  309. EncryptFile(name);
  310. }
  311. }
  312. }
  313. }
  314. #endregion
  315.  
  316. #region entschlüsseln
  317. private void bttn_entschlusseln_Click(object sender, EventArgs e)
  318. {
  319. brow_ent.Refresh();
  320. if (rsa == null)
  321. MessageBox.Show("Schlüssel nicht gesetzt!");
  322. else
  323. {
  324. openFileDialog2.InitialDirectory = EncrFolder;
  325. if (openFileDialog2.ShowDialog() == DialogResult.OK)
  326. {
  327. string fName = openFileDialog2.FileName;
  328. if (fName != null)
  329. {
  330. FileInfo fi = new FileInfo(fName);
  331. string name = fi.Name;
  332. DecryptFile(name);
  333. }
  334. }
  335. }
  336. }
  337. #endregion
  338.  
  339. #region public key
  340. private void bttn_pubexp_Click(object sender, EventArgs e)
  341. {
  342. Directory.CreateDirectory(EncrFolder);
  343. StreamWriter sw = new StreamWriter(PubKeyFile, false);
  344. sw.Write(rsa.ToXmlString(false));
  345. sw.Close();
  346. }
  347.  
  348. private void bttn_pubinp_Click(object sender, EventArgs e)
  349. {
  350. StreamReader sr = new StreamReader(PubKeyFile);
  351. cspp.KeyContainerName = keyName;
  352. rsa = new RSACryptoServiceProvider(cspp);
  353. string keytxt = sr.ReadToEnd();
  354. rsa.FromXmlString(keytxt);
  355. rsa.PersistKeyInCsp = true;
  356. if (rsa.PublicOnly == true)
  357. MessageBox.Show("Key: " + cspp.KeyContainerName + " – Public Only");
  358. else
  359. MessageBox.Show("Key: " + cspp.KeyContainerName + " – Full Key Pair");
  360. sr.Close();
  361. }
  362.  
  363. private void bttn_asmkey_Click(object sender, EventArgs e)
  364. {
  365. cspp.KeyContainerName = keyName;
  366. rsa = new RSACryptoServiceProvider(cspp);
  367. rsa.PersistKeyInCsp = true;
  368. if (rsa.PublicOnly == true)
  369. MessageBox.Show("Key: " + cspp.KeyContainerName + " – Public Only");
  370. else
  371. MessageBox.Show("Key: " + cspp.KeyContainerName + " – Full Key Pair");
  372.  
  373. }
  374. #endregion
  375.  
  376. #region private key
  377. private void bttn_privkey_Click(object sender, EventArgs e)
  378. {
  379. cspp.KeyContainerName = keyName;
  380.  
  381. rsa = new RSACryptoServiceProvider(cspp);
  382. rsa.PersistKeyInCsp = true;
  383.  
  384. if (rsa.PublicOnly == true)
  385. MessageBox.Show("Key: " + cspp.KeyContainerName + " – Public Only");
  386. else
  387. MessageBox.Show("Key: " + cspp.KeyContainerName + " – Full Key Pair");
  388.  
  389. }
  390. #endregion
  391.  
  392. #region verschlüsselung core
  393. private void EncryptFile(string inFile)
  394. {
  395. RijndaelManaged rjndl = new RijndaelManaged();
  396. rjndl.KeySize = 256;
  397. rjndl.BlockSize = 256;
  398. rjndl.Mode = CipherMode.CBC;
  399. ICryptoTransform transform = rjndl.CreateEncryptor();
  400.  
  401. byte[] keyEncrypted = rsa.Encrypt(rjndl.Key, false);
  402.  
  403. byte[] LenK = new byte[4];
  404. byte[] LenIV = new byte[4];
  405.  
  406. int lKey = keyEncrypted.Length;
  407. LenK = BitConverter.GetBytes(lKey);
  408. int lIV = rjndl.IV.Length;
  409. LenIV = BitConverter.GetBytes(lIV);
  410.  
  411. int startFileName = inFile.LastIndexOf("\\") + 1;
  412. string outFile = EncrFolder + inFile.Substring(startFileName, inFile.LastIndexOf(".") – startFileName) + ".d3";
  413.  
  414. using (FileStream outFs = new FileStream(outFile, FileMode.Create))
  415. {
  416.  
  417. outFs.Write(LenK, 0, 4);
  418. outFs.Write(LenIV, 0, 4);
  419. outFs.Write(keyEncrypted, 0, lKey);
  420. outFs.Write(rjndl.IV, 0, lIV);
  421.  
  422. using (CryptoStream outStreamEncrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
  423. {
  424. int count = 0;
  425. int offset = 0;
  426.  
  427. int blockSizeBytes = rjndl.BlockSize / 8;
  428. byte[] data = new byte[blockSizeBytes];
  429. int bytesRead = 0;
  430.  
  431. using (FileStream inFs = new FileStream(inFile, FileMode.Open))
  432. {
  433. do
  434. {
  435. count = inFs.Read(data, 0, blockSizeBytes);
  436. offset += count;
  437. outStreamEncrypted.Write(data, 0, count);
  438. bytesRead += blockSizeBytes;
  439. }
  440. while (count > 0);
  441. inFs.Close();
  442. }
  443. outStreamEncrypted.FlushFinalBlock();
  444. outStreamEncrypted.Close();
  445. }
  446. outFs.Close();
  447. }
  448.  
  449. }
  450. #endregion
  451.  
  452. #region entschlüsselung core
  453.  
  454. private void DecryptFile(string inFile)
  455. {
  456. RijndaelManaged rjndl = new RijndaelManaged();
  457. rjndl.KeySize = 256;
  458. rjndl.BlockSize = 256;
  459. rjndl.Mode = CipherMode.CBC;
  460.  
  461. byte[] LenK = new byte[4];
  462. byte[] LenIV = new byte[4];
  463.  
  464. string outFile = DecrFolder + inFile.Substring(0, inFile.LastIndexOf(".")) + ".txt";
  465.  
  466. using (FileStream inFs = new FileStream(EncrFolder + inFile, FileMode.Open))
  467. {
  468.  
  469. inFs.Seek(0, SeekOrigin.Begin);
  470. inFs.Seek(0, SeekOrigin.Begin);
  471. inFs.Read(LenK, 0, 3);
  472. inFs.Seek(4, SeekOrigin.Begin);
  473. inFs.Read(LenIV, 0, 3);
  474.  
  475. int lenK = BitConverter.ToInt32(LenK, 0);
  476. int lenIV = BitConverter.ToInt32(LenIV, 0);
  477.  
  478. int startC = lenK + lenIV + 8;
  479. int lenC = (int)inFs.Length – startC;
  480.  
  481. byte[] KeyEncrypted = new byte[lenK];
  482. byte[] IV = new byte[lenIV];
  483.  
  484. inFs.Seek(8, SeekOrigin.Begin);
  485. inFs.Read(KeyEncrypted, 0, lenK);
  486. inFs.Seek(8 + lenK, SeekOrigin.Begin);
  487. inFs.Read(IV, 0, lenIV);
  488. Directory.CreateDirectory(DecrFolder);
  489.  
  490. byte[] KeyDecrypted = rsa.Decrypt(KeyEncrypted, false);
  491.  
  492. ICryptoTransform transform = rjndl.CreateDecryptor(KeyDecrypted, IV);
  493.  
  494. using (FileStream outFs = new FileStream(outFile, FileMode.Create))
  495. {
  496.  
  497. int count = 0;
  498. int offset = 0;
  499.  
  500. int blockSizeBytes = rjndl.BlockSize / 8;
  501. byte[] data = new byte[blockSizeBytes];
  502.  
  503. inFs.Seek(startC, SeekOrigin.Begin);
  504. using (CryptoStream outStreamDecrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write))
  505. {
  506. do
  507. {
  508. count = inFs.Read(data, 0, blockSizeBytes);
  509. offset += count;
  510. outStreamDecrypted.Write(data, 0, count);
  511.  
  512. }
  513. while (count > 0);
  514.  
  515. outStreamDecrypted.FlushFinalBlock();
  516. outStreamDecrypted.Close();
  517. }
  518. outFs.Close();
  519. }
  520. inFs.Close();
  521. }
  522.  
  523. }
  524. #endregion
  525. }
  526. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement