Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void addUserButton_Click(object sender, RoutedEventArgs e)
- {
- CryptoApiRandomGenerator randomGenerator = new CryptoApiRandomGenerator();
- SecureRandom secureRandom = new SecureRandom(randomGenerator);
- var keyGenerationParameters = new KeyGenerationParameters(secureRandom, RsaKeySize);
- var keyPairGenerator = new RsaKeyPairGenerator();
- keyPairGenerator.Init(keyGenerationParameters);
- AsymmetricCipherKeyPair keyPair = keyPairGenerator.GenerateKeyPair();
- var password = Encoding.ASCII.GetBytes(pass.Password);
- var userName = user.Text;
- Stream out1 = File.Create(@".\prywatne\" + userName);
- Stream out2 = File.Create(@".\publiczne\" + userName);
- ExportKeyPair(out1, out2, keyPair.Public, keyPair.Private, userName, password, true);
- out1.Close();
- out2.Close();
- user.Text = "";
- pass.Password = "";
- this.Close();
- }
- private static void ExportKeyPair(
- Stream secretOut,
- Stream publicOut,
- AsymmetricKeyParameter publicKey,
- AsymmetricKeyParameter privateKey,
- string identity,
- byte[] passPhrase,
- bool armor)
- {
- SHA256 hashMachine = SHA256Managed.Create();
- byte[] passHash = hashMachine.ComputeHash(passPhrase);
- PgpSecretKey secretKey = new PgpSecretKey(
- PgpSignature.DefaultCertification,
- PublicKeyAlgorithmTag.RsaGeneral,
- publicKey,
- privateKey,
- DateTime.UtcNow,
- identity,
- SymmetricKeyAlgorithmTag.Blowfish,
- Encoding.ASCII.GetChars(passHash),
- null,
- null,
- new SecureRandom()
- );
- secretKey.Encode(secretOut);
- if (armor)
- {
- secretOut.Close();
- publicOut = new ArmoredOutputStream(publicOut);
- }
- PgpPublicKey key = secretKey.PublicKey;
- key.Encode(publicOut);
- if (armor)
- {
- publicOut.Close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement