public string encrypt(string data) { var sym = new Symmetric(_p); sym.Key.Text = _k; return(sym.Encrypt(new Encryption.Data(data)).ToHex()); }
internal static string EncryptValue(string value) { if (String.IsNullOrWhiteSpace(value)) { return(String.Empty); } using (var symmetricProvider = new Symmetric(SymmetricProvider.Rijndael, false)) { string result; try { symmetricProvider.IntializationVector = IV; result = symmetricProvider.Encrypt(new Data(value), SymmetricKey).Bytes.ToBase64(); } catch (CryptographicException) { // return the value as is result = value; } return(result); } }
/// <summary> /// Encrypt plain text via Rijndael symmetric-key algorithm. Returns Base64 string representation of the encrypted text /// </summary> /// <param name="plainText">the plain text</param> /// <returns></returns> public string Encrypt(string plainText) { string eKey = this._key; var sym = new Symmetric(Symmetric.Provider.Rijndael); var key = new Data(eKey); var encryptedText = sym.Encrypt(new Data(plainText), key); return encryptedText.ToBase64(); }
public void Decrypt_WithBase64CipherAndIV() { var symmetric = new Symmetric(key); var cipher = symmetric.Encrypt(plainText, iv); var decryptedText = symmetric.Decrypt(Convert.ToBase64String(cipher.EncryptedContent), iv); Assert.AreEqual(plainText, decryptedText); }
public void EncryptDecryptString_AppendIV() { var symmetric = new Symmetric(key); var cipher = symmetric.Encrypt(plainText, iv); var decryptedText = symmetric.Decrypt(cipher); Assert.AreEqual(plainText, Symmetric.Deserialize <string>(decryptedText)); }
public static string Encrypt(string strPlainText, string strKey24) { var sym = new Symmetric(Symmetric.Provider.TripleDES, true); var key = new Data(strKey24); Data encryptedData; encryptedData = sym.Encrypt(new Data(strPlainText), key); return encryptedData.ToBase64(); }
//private static readonly string embededKey = "MHA234@@$AV^&*^(ere5MHcc"; public static string Encrypt(string strPlainText, string strKey24) { Symmetric sym = new Symmetric(Symmetric.Provider.TripleDES, true); EncryptionClassLibrary.Encryption.Data key = new EncryptionClassLibrary.Encryption.Data(strKey24); EncryptionClassLibrary.Encryption.Data encryptedData; encryptedData = sym.Encrypt(new EncryptionClassLibrary.Encryption.Data(strPlainText), key); return encryptedData.ToBase64(); }
public static string Encrypt(string value) { Symmetric sym = new Symmetric(); CPM.Business.Global.Account.Encryption.Data encryptedData = default(CPM.Business.Global.Account.Encryption.Data); encryptedData = sym.Encrypt(new CPM.Business.Global.Account.Encryption.Data(value)); return(encryptedData.Base64); }
public void Encrypt_WithEmptyMessage_ReturnsNull() { string message = String.Empty; Symmetric symmetricHelper = new Symmetric(); byte[] actual = symmetricHelper.Encrypt(message); Assert.IsNull(actual); }
public void Encrypt() { var plainText = "this is my plain text"; var passPhrase = "this is my pass phrase"; var encrypted = Symmetric.Encrypt(plainText, passPhrase); Assert.AreNotEqual(plainText, encrypted); }
/// <summary> /// Encrypt plain text via Rijndael symmetric-key algorithm. Returns Base64 string representation of the encrypted text /// </summary> /// <param name="plainText">the plain text</param> /// <returns></returns> public string Encrypt(string plainText) { string eKey = this.Key; var sym = new Symmetric(Symmetric.Provider.Rijndael); var key = new Data(eKey); var encryptedText = sym.Encrypt(new Data(plainText), key); return(encryptedText.ToBase64()); }
public void BasicCanDecryptTest() { const string PLAINTEXT = "SmartEncryption Sym Test"; var key = Symmetric.GenerateKey(); var cipher = Symmetric.Encrypt(Encoding.UTF8.GetBytes(PLAINTEXT), key); var plain = Symmetric.Decrypt(cipher, key); Assert.AreEqual(PLAINTEXT, Encoding.UTF8.GetString(plain)); }
public void TestNonceSize() { var key = "eda8506c1fb0bbcc3f62626fef074bbf2d09a8c7c608f3fa1482c9a625d00f75".ToByteArray(); var plaitext = Encoding.ASCII.GetBytes("hello, how are you?"); var ciphertext = Symmetric.Encrypt(key, plaitext); if (ciphertext.Length != 19 + 16 + 24) { throw new Exception("Length of this ciphertext should be 19B(PT) + 16B(TAG) + 24B(NONCE)"); } }
public IActionResult Encrypt(string plaintext) { string key = _configuration["Symmetric:Key"]; string iv = _configuration["Symmetric:IV"]; Symmetric s = new Symmetric(key, iv); byte[] encryptedBytes = s.Encrypt(plaintext); ViewBag.Plaintext = plaintext; ViewBag.EncryptedBytes = Encoding.UTF8.GetString(encryptedBytes); ViewBag.Ciphertext = Convert.ToBase64String(encryptedBytes); return(View()); }
public void EncryptTest() { var message = "Attack at dawn!"; var password = "******"; // Encrypt the message using our password to get a cipher text var cipher = Symmetric.Encrypt(message, password); // Decode the cipher text using the same password to get the original message Assert.AreEqual(message, Symmetric.Decrypt(cipher, password)); // CONDENSED EXAMPLE WITH BYTES byte[] messageBytes = { 0x25, 0x9F, 0xB3 }; byte[] passwordBytes = { 0x12, 0x34, 0x56, 0x78 }; Assert.AreEqual(messageBytes.ToBase64(), Symmetric.Decrypt(Symmetric.Encrypt(messageBytes, passwordBytes), passwordBytes).ToBase64()); }
public void Encrypt_WithMessage_ReturnsSecret( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode) { string message = SymmetricTestsHelper.DEFAULT_MESSAGE; byte[] expected = SymmetricTestsHelper.GetDefaultSecret(mode); Symmetric symmetricHelper = new Symmetric(mode); symmetricHelper.Salt = SymmetricTestsHelper.DEFAULT_SALT; symmetricHelper.IV = SymmetricTestsHelper.GetDefaultIV(mode); symmetricHelper.Key = SymmetricTestsHelper.GetDefaultKey(mode); byte[] actual = symmetricHelper.Encrypt(message); Assert.AreEqual(expected, actual); }
public void Decrypt_NoCipherIV() { try { var symmetric = new Symmetric(key); var cipher = symmetric.Encrypt(plainText, iv); cipher.IV = null; Assert.IsFalse(cipher.HasIV); var decryptedText = symmetric.Decrypt(cipher); Assert.AreEqual(plainText, decryptedText); } catch (Exception ex) { Assert.AreEqual("Initialization vector must be supplied", ex.Message); throw; } }
public void TestEncryptDecrypt() { var key = "eda8506c1fb0bbcc3f62626fef074bbf2d09a8c7c608f3fa1482c9a625d00f75".ToByteArray(); var plaintexs = new[] { "", "a", "ab", "abc", "abcd", "short", "hello, how are you?", "this is very short", "this is very long though, like, very very long, should we test very very long things here?", "Domestic cats are similar in size to the other members of the genus Felis, typically weighing " + "between 4 and 5 kg (9 and 10 lb).[36] Some breeds, such as the Maine Coon, can occasionally " + "exceed 11 kg (24 lb). Conversely, very small cats, less than 2 kg (4 lb), have been reported.[59] " + "The world record for the largest cat is 21 kg(50 lb).[60][self - published source] " + "The smallest adult cat ever officially recorded weighed around 1 kg(2 lb).[60] " + "Feral cats tend to be lighter, as they have more limited access to food than house cats." + "The Boston Cat Hospital weighed trapped feral cats, and found the average feral adult " + "male to weigh 4 kg(9 lb), and average adult female 3 kg(7 lb).[61] Cats average about " + "23–25 cm(9–10 in) in height and 46 cm(18 in) in head / body length(males being larger than females), " + "with tails averaging 30 cm(12 in) in length;[62] feral cats may be smaller on average.ats have seven" + " cervical vertebrae, as do almost all mammals; 13 thoracic vertebrae(humans have 12); seven lumbar" + " vertebrae(humans have five); three sacral vertebrae like most mammals(humans have five);" + " and a variable number of caudal vertebrae in the tail(humans have only vestigial caudal" + " vertebrae, fused into an internal coccyx).[63]:11 The extra lumbar and thoracic vertebrae" + " account for the cat's spinal mobility and flexibility. Attached to the spine are 13 ribs," + " the shoulder, and the pelvis.[63] :16 Unlike human arms, cat forelimbs are attached to the" + " shoulder by free-floating clavicle bones which allow them to pass their body through any" + " space into which they can fit their head.[64]", }; foreach (var plaintextString in plaintexs) { var plaintex = Encoding.ASCII.GetBytes(plaintextString); var ciphertext = Symmetric.Encrypt(key, plaintex); var decrypted = Symmetric.Decrypt(key, ciphertext); if (!plaintex.SequenceEqual(decrypted)) { throw new Exception("Decrypt did not work"); } } }
private void Encrypt(IEnumerable <ClientSettings> value) { var symetricProvider = new Symmetric(SymmetricProvider.Rijndael, false) { IntializationVector = _iv }; foreach (var settings in value) { if (settings.Password.Length == 0) { continue; } try { settings.Password = symetricProvider.Encrypt(new Data(settings.Password), _symmetricKey).Bytes.ToBase64(); } catch (CryptographicException) { Logger.WarnFormat(Constants.ClientNameFormat, settings.Name, "Failed to encrypt password... saving clear value."); } } }
public void Encrypt_WithNullMessage_ReturnsNull() { string message = null; Symmetric symmetricHelper = new Symmetric(); byte[] actual = symmetricHelper.Encrypt(message); Assert.IsNull(actual); }