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 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); }