Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. private void addUserButton_Click(object sender, RoutedEventArgs e)
  2. {
  3. CryptoApiRandomGenerator randomGenerator = new CryptoApiRandomGenerator();
  4. SecureRandom secureRandom = new SecureRandom(randomGenerator);
  5. var keyGenerationParameters = new KeyGenerationParameters(secureRandom, RsaKeySize);
  6.  
  7. var keyPairGenerator = new RsaKeyPairGenerator();
  8. keyPairGenerator.Init(keyGenerationParameters);
  9. AsymmetricCipherKeyPair keyPair = keyPairGenerator.GenerateKeyPair();
  10. var password = Encoding.ASCII.GetBytes(pass.Password);
  11. var userName = user.Text;
  12.  
  13. Stream out1 = File.Create(@".\prywatne\" + userName);
  14. Stream out2 = File.Create(@".\publiczne\" + userName);
  15.  
  16. ExportKeyPair(out1, out2, keyPair.Public, keyPair.Private, userName, password, true);
  17.  
  18. out1.Close();
  19. out2.Close();
  20.  
  21. user.Text = "";
  22. pass.Password = "";
  23. this.Close();
  24. }
  25. private static void ExportKeyPair(
  26. Stream secretOut,
  27. Stream publicOut,
  28. AsymmetricKeyParameter publicKey,
  29. AsymmetricKeyParameter privateKey,
  30. string identity,
  31. byte[] passPhrase,
  32. bool armor)
  33. {
  34.  
  35. SHA256 hashMachine = SHA256Managed.Create();
  36. byte[] passHash = hashMachine.ComputeHash(passPhrase);
  37. PgpSecretKey secretKey = new PgpSecretKey(
  38. PgpSignature.DefaultCertification,
  39. PublicKeyAlgorithmTag.RsaGeneral,
  40. publicKey,
  41. privateKey,
  42. DateTime.UtcNow,
  43. identity,
  44. SymmetricKeyAlgorithmTag.Blowfish,
  45. Encoding.ASCII.GetChars(passHash),
  46. null,
  47. null,
  48. new SecureRandom()
  49. );
  50.  
  51. secretKey.Encode(secretOut);
  52.  
  53. if (armor)
  54. {
  55. secretOut.Close();
  56. publicOut = new ArmoredOutputStream(publicOut);
  57. }
  58.  
  59. PgpPublicKey key = secretKey.PublicKey;
  60.  
  61. key.Encode(publicOut);
  62.  
  63. if (armor)
  64. {
  65. publicOut.Close();
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement