public void ExportTest() { IOpenPgpKeyManagement keys = new OpenPgpKeyManagement(); var passPhrase = "pass phrase 1234"; var key = keys.GenerateKeyPair("*****@*****.**", passPhrase); // Public key var originalFingerPrint = key.PublicKey.GetFingerprint(); var exportedPublicKey = keys.Export(key.PublicKey); var reimportedKey = keys.LoadPublicKey(exportedPublicKey); var reimportedFingerprint = reimportedKey.GetFingerprint(); Assert.Equal(originalFingerPrint.Length, reimportedFingerprint.Length); for (int i = 0; i < originalFingerPrint.Length; i++) { Assert.Equal(originalFingerPrint[i], reimportedFingerprint[i]); } // Private key var exportedSecretKey = keys.Export(key); var reimportedSecretKey = keys.LoadSecretKey(exportedSecretKey); // Imported key should allow to decrypt message IOpenPgpEncryption pgp = new OpenPgpEncryption(); var encrypted = pgp.Encrypt("Hello", key.PublicKey); var decrypted = pgp.Decrypt(encrypted, reimportedSecretKey, passPhrase); Assert.Equal("Hello", decrypted); }
public void EncryptAndDecryptTest() { IOpenPgpEncryption pgp = new OpenPgpEncryption(); IOpenPgpKeyManagement keys = new OpenPgpKeyManagement(); var passPhrase = "pass phrase 1234"; var keyPair = keys.GenerateKeyPair("*****@*****.**", passPhrase); // We encrypt the message using the public key string message = "This message is very secret"; var encrypted = pgp.Encrypt(message, keyPair.PublicKey); Assert.NotNull(encrypted); Assert.NotEqual(message, encrypted); // Now we can decrypt it with the private key var decrypted = pgp.Decrypt(encrypted, keyPair, passPhrase); Assert.Equal(message, decrypted); // If we try to decrypt without private key or with bad password, we should have an Exception var badPassPhrase = "pass phrase 5678"; Assert.ThrowsAny <Exception>(() => pgp.Decrypt(encrypted, keyPair, badPassPhrase)); var badKey = keys.GenerateKeyPair("*****@*****.**", badPassPhrase); Assert.ThrowsAny <Exception>(() => pgp.Decrypt(encrypted, badKey, badPassPhrase)); }
public static IServiceCollection AddWebKeyDirectory(this IServiceCollection services, string path) { var keyManagement = new OpenPgpKeyManagement(); var publicKeys = Directory.GetFiles(path).Select(file => keyManagement.LoadPublicKey(File.OpenRead(file))); services.AddSingleton(LoadPublicKeys(publicKeys)); return(services); }
public static IServiceCollection AddWebKeyDirectory(this IServiceCollection services, IDirectoryContents directory) { var keyManagement = new OpenPgpKeyManagement(); var publicKeys = directory.Select(file => keyManagement.LoadPublicKey(file.CreateReadStream())); services.AddSingleton(LoadPublicKeys(publicKeys)); return(services); }
public void GenerateKeyPairTest() { IOpenPgpKeyManagement keys = new OpenPgpKeyManagement(); var keyPair = keys.GenerateKeyPair("*****@*****.**", "pass phrase 1234"); Assert.NotNull(keyPair); var otherKeyPair = keys.GenerateKeyPair("*****@*****.**", "pass phrase 1234"); Assert.NotEqual(keyPair.KeyId, otherKeyPair.KeyId); }
private static WkdSavedKeys LoadPublicKeys(IEnumerable <PgpPublicKey> publicKeys, OpenPgpKeyManagement?keyManagement = null) { var context = new WkdSavedKeys(); var keySearch = new OpenPgpKeySearch(); if (keyManagement == null) { keyManagement = new OpenPgpKeyManagement(); } foreach (var key in publicKeys) { foreach (string userId in key.GetUserIds()) { MailAddress mailAddress; try { mailAddress = new MailAddress(userId); } catch (FormatException) { // UserId is not a mail adress, we don't save it continue; } var host = mailAddress.Host; Dictionary <string, string> hostDic; if (!context.PublicKeys.TryGetValue(host, out hostDic)) { hostDic = new Dictionary <string, string>(); context.PublicKeys.Add(host, hostDic); } var user = keySearch.GetHashedUserId(mailAddress.User); if (hostDic.ContainsKey(user)) { // we already have a public key for this e-mail address continue; } var keyString = keyManagement.Export(key); hostDic.Add(user, keyString); } } return(context); }
public void ImportTest() { IOpenPgpKeyManagement keys = new OpenPgpKeyManagement(); var publicKeyBlock = @"-----BEGIN PGP PUBLIC KEY BLOCK----- Version: ProtonMail xsBNBFYE8tYBCACnH05FAKnejlh2Pvpk7KRXT8SchyNNpfw8dcduVCJNOE+D YvJF/3tPKujvODES6WDy2qrWU7ypC49ZS7wxpoEgIP4zfo/EqMNjJRvq2T7I dIoj3eP8T0bQLbIkF5YexUqDXkD+ogEFXRcuFIq0dWAgAqeHtgQKKPlM18Qt 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5i9Jd+5BCACGWfkdvtkR+gktydg6vF+H/d8ubGu4D9w+fHQDdgCA o3JFyW/72iGCrYbJ4ubVvY74R7dyViCSRlY90yfDA32ltNK66mK+ndhsrW3o kC9YXR5KFyBLo7zDegUxNC82bISDl+H/+6UU7jNJeKh1fkx6isSBvTcZK3Dz wp3ro0WRnlN9FWank5Mtge7kOwfCz8E9YLm8aadzNpflj0aXcbpffk5q7vp7 VDqPPxFbAf1kpFDu3dGslDMklzbHyDgv5IfezYpr40m2jVI9hawn2hY60JNl l7kkkgh8anR4kuki/pMoweSNxnOIORdQwMqqhe4GFmMfv8HC9wO1zGD8W2G9 =Ycp8 -----END PGP PUBLIC KEY BLOCK-----"; var publicKey = keys.LoadPublicKey(publicKeyBlock); Utils.CheckFingerprint(publicKey, "4805b5106ca0eab809e16b798bfc4819e62f4977"); }