public void DecryptMessageCorrectTest() { var decryptedMessage = SymetricProvider.DecryptString(FakeDataGenerator.DefaultDto.BasicPassword, FakeDataGenerator.DefaultDto.BasicEncryptedMessage); Assert.Equal(FakeDataGenerator.DefaultDto.BasicMessage, decryptedMessage); }
public void EmptyKeyFailTest() { string message = "key can not be null or empty."; var exception = Assert.Throws <ArgumentException>(() => { var decryptedMessage = SymetricProvider.DecryptString("", FakeDataGenerator.DefaultDto.BasicEncryptedMessage); }); Assert.Contains(message, exception.Message); }
public void WrongCipherTextFailTest() { string message = "pad block corrupted"; var exception = Assert.Throws <Org.BouncyCastle.Crypto.InvalidCipherTextException>(() => { var decryptedMessage = SymetricProvider.DecryptString(FakeDataGenerator.DefaultDto.BasicPassword, FakeDataGenerator.DefaultDto.BasicEncryptedMessageChanged); }); Assert.Contains(message, exception.Message); }
public void WrongShapeCipherTextFailTest() { string message = "cipherText is not valid. It must be Base64 string"; var exception = Assert.Throws <ArgumentException>(() => { var decryptedMessage = SymetricProvider.DecryptString(FakeDataGenerator.DefaultDto.BasicPassword, FakeDataGenerator.DefaultDto.BasicEncryptedMessageWrongShape); }); Assert.Contains(message, exception.Message); }
private string DecryptEncryptedKey(BitcoinSecret mainSecret) { string key = string.Empty; if (!string.IsNullOrEmpty(ESKey)) { key = SymetricProvider.DecryptString(SecurityUtils.ComputeSha256Hash(mainSecret.PrivateKey.ToString()), ESKey); } else if (!string.IsNullOrEmpty(EKey)) { key = ECDSAProvider.DecryptStringWithPrivateKey(EKey, mainSecret.PrivateKey); // TODO: some preprocessor directive for run just in old version under .NETStandard2.1 } return(key); }
public override string LoadAccountKey(string wallet, string address, string key, IDbConnectorService dbservice, string pubkey = "", string password = "", string name = "", bool storeInDb = true, bool isItMainAccountKey = false, bool alreadyEncrypted = false, EncryptionKeyType type = EncryptionKeyType.BasicSecurity) { try { if (EconomyMainContext.Wallets.TryGetValue(wallet, out var w)) { if (w.Accounts.TryGetValue(address, out var account)) { if (isItMainAccountKey) { // todo load already encrypted key account.AccountKey = new Security.EncryptionKey(key, password); account.AccountKey.RelatedItemId = account.Id; account.AccountKey.Type = Security.EncryptionKeyType.AccountKey; account.AccountKeyId = account.AccountKey.Id; account.AccountKey.PublicKey = account.Address; if (!string.IsNullOrEmpty(password)) { account.AccountKey.PasswordHash = Security.SecurityUtil.HashPassword(password); } account.AccountKey.Name = name; if (EconomyMainContext.WorkWithDb) { dbservice.SaveKey(account.AccountKey); } account.AccountKeyId = account.AccountKey.Id; if (EconomyMainContext.WorkWithDb) { dbservice.SaveAccount(account); } return("OK"); } else { EncryptionKey k = null; if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(pubkey)) { // validate the key pair if it is correct combination of RSA keys try { if (type != EncryptionKeyType.AccountKey) { if (alreadyEncrypted) { var kd = SymetricProvider.DecryptString(password, key); if (kd != null) { key = kd; } } var m = AsymmetricProvider.EncryptString("test", pubkey); var r = AsymmetricProvider.DecryptString(m, key); if (r != "test") { throw new Exception("Key pair is not valid RSA key pair!"); } } k = new EncryptionKey(key, password); } catch (Exception ex) { throw new Exception("Key pair is not valid RSA key pair!"); } } else if (!string.IsNullOrEmpty(key) && string.IsNullOrEmpty(pubkey)) { if (alreadyEncrypted) { var kd = SymetricProvider.DecryptString(password, key); if (kd != null) { k = new Security.EncryptionKey(kd, password); } } else { k = new Security.EncryptionKey(key, password); k.LoadNewKey(key, fromDb: true); } } else if (!string.IsNullOrEmpty(pubkey) && string.IsNullOrEmpty(key)) { // create enc object k = new Security.EncryptionKey("passtest", password, true); // this can be used for testing the password k.PublicKey = pubkey; } else if (string.IsNullOrEmpty(key) && string.IsNullOrEmpty(pubkey)) { // obtain new RSA key pair var keypair = Security.AsymmetricProvider.GenerateNewKeyPair(); // create enc object k = new EncryptionKey(keypair.PrivateKey, password); k.PublicKey = keypair.PublicKey; } else { throw new Exception("Strange input!"); } k.RelatedItemId = account.Id; k.Type = Security.EncryptionKeyType.BasicSecurity; if (!string.IsNullOrEmpty(password)) { k.PasswordHash = Security.SecurityUtil.HashPassword(password); } k.Name = name; account.AccountKeys.Add(k); if (EconomyMainContext.WorkWithDb) { dbservice.SaveKey(k); } if (isItMainAccountKey) { account.AccountKeyId = account.AccountKey.Id; if (EconomyMainContext.WorkWithDb) { dbservice.SaveAccount(account); } } return("OK"); } } } } catch (Exception ex) { log.Error("Cannot load key to the account!", ex); } return("Load Account Key - ERROR"); }