public void Decrypt_works_as_expected() { // arrange var privateKey = "5f349a6bf95d692830a8930aa657b5d553073e589564925f153c018b5c27c8b6"; var ejson = @"{ ""_public_key"": ""749d901c694890ee91b7d2c366c2d59dc7b6b8a386d0a5be73431e622b91d117"", ""passwords"": { ""database_password"": ""EJ[1:EtX9E9y07M9ppTIeiLgdMysdWgmWNPvdNRBMtHKuDQo=:ZQqM6wGxUUiRy2kXYVinPKEFIyMRubvL:OR5Owty2fMOPTownI2/xngsWISvD]"" } }"; var expectedDecrypted = @"{ ""_public_key"": ""749d901c694890ee91b7d2c366c2d59dc7b6b8a386d0a5be73431e622b91d117"", ""passwords"": { ""database_password"": ""hello"" } }"; var crypto = new JObjectCrypto(); // act var actualDecrypted = crypto.DecryptJson(ejson, privateKey); // assert Assert.Equal(JObjectTools.NormalizeJson(expectedDecrypted), JObjectTools.NormalizeJson(actualDecrypted)); }
private JObject GetEncryptedJObject(string json) { var jObject = JObjectTools.GetJObject(json); var publicKey = GetPublicKey(jObject); _jObjectCrypto.Encrypt(jObject, publicKey); return(jObject); }
private JObject GetDecryptJObject(string json, IPrivateKeyProvider keyProvider) { var jObject = JObjectTools.GetJObject(json); var publicKey = GetPublicKey(jObject); keyProvider = keyProvider ?? new DefaultPrivateKeyProvider(); if (keyProvider.TryGetPrivateKey(publicKey, out string privateKey)) { _jObjectCrypto.Decrypt(jObject, privateKey); return(jObject); } throw new InvalidOperationException($"Could not find private key for: {publicKey}"); }
public void Encrypt_and_Decrypt_works_as_expected() { // arrange var publicKey = "749d901c694890ee91b7d2c366c2d59dc7b6b8a386d0a5be73431e622b91d117"; var privateKey = "5f349a6bf95d692830a8930aa657b5d553073e589564925f153c018b5c27c8b6"; var json = @"{ ""passwords"": { ""database_password"": ""hello"" } }"; var crypto = new JObjectCrypto(); // act var encrypted = crypto.EncryptJson(json, publicKey); var decrypted = crypto.DecryptJson(encrypted, privateKey); // assert Assert.Equal(JObjectTools.NormalizeJson(decrypted), JObjectTools.NormalizeJson(json)); }
public string GetEncryptedJson(string json) { var jObject = GetEncryptedJObject(json); return(JObjectTools.GetJson(jObject)); }
public string GetDecryptedJson(string json, IPrivateKeyProvider keyProvider = null) { var jObject = GetDecryptJObject(json, keyProvider); return(JObjectTools.GetJson(jObject)); }