public void TestKeyGeneration() { Sodium.InitNativeLibrary(); var pkc1 = new PublicKeyCrypto(); pkc1.Init(); Assert.IsFalse(String.IsNullOrEmpty(Encoding.UTF8.GetString(pkc1.PublicKey))); Assert.IsFalse(String.IsNullOrEmpty(Encoding.UTF8.GetString(pkc1.PrivateKey))); }
public void TestPublicKeyEncrypt() { Sodium.InitNativeLibrary(); var pkc1 = new PublicKeyCrypto(); var pkc2 = new PublicKeyCrypto(); pkc1.Init(); pkc2.Init(); var message = "test"; var nonce = PublicKeyCrypto.GenerateNonce(); var encMessage = pkc1.EncryptWithPublicKey(Encoding.ASCII.GetBytes(message), pkc2.PublicKey, nonce); var decryptedMessage = pkc2.DecryptWithPrivateKey(encMessage, pkc1.PublicKey, nonce); Assert.IsTrue(String.Compare(message, Encoding.ASCII.GetString(decryptedMessage)) == 0); }
private static async void OnCreateKeyPair(object obj) { try { App.Context.ClosePopup(); using (var crypto = new PublicKeyCrypto()) { crypto.Init(); var dir = AppContext.GetKeyDirectory().FullName; var publicKey = Path.Combine(dir, Settings.Default.PublicKeyFile); await crypto.SavePublicKeyAsync(publicKey); var privateKey = Path.Combine(dir, Settings.Default.PrivateKeyFile); await crypto.SavePrivateKeyAsync(privateKey); await App.Context.InitKeysAsync(); await App.Context.RunAsync(); } } catch (Exception ex) { Logger.Error(ex); } }