/// <summary> /// Copies certain characteristics of this instance to the <paramref name="cipher"/> parameter. /// The goal is to produce a cipher with the same encryption/decryption behavior but saving the key encryption and decryption ceremony and overhead if possible. /// Here it creates a <see cref="SymmetricAlgorithm"/> object identical to the current <see cref="Symmetric"/> and assigns it to <paramref name="cipher"/>'s <see cref="Symmetric"/>. /// </summary> /// <param name="cipher">The cipher that gets the identical symmetric algorithm object.</param> protected virtual void CopyTo( SymmetricKeyCipherBase cipher) { if (cipher == null) { throw new ArgumentNullException(nameof(cipher)); } if (!IsSymmetricKeyInitialized) { return; } cipher._symmetric = SymmetricAlgorithm.Create(Symmetric.GetType().FullName); cipher.Symmetric.Mode = Symmetric.Mode; cipher.Symmetric.Padding = Symmetric.Padding; cipher.Symmetric.BlockSize = Symmetric.BlockSize; cipher.Symmetric.FeedbackSize = Symmetric.FeedbackSize; cipher.Symmetric.KeySize = Symmetric.KeySize; cipher.Symmetric.Key = (byte[])Symmetric.Key.Clone(); cipher.Symmetric.IV = (byte[])Symmetric.IV.Clone(); cipher.IsSymmetricKeyInitialized = true; cipher.ShouldEncryptIV = ShouldEncryptIV; }
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); } }
public void Constructor_WithModeInvalidKeySizeAndValidSalt_Throws( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode, [Values(0, 1024)] int keySize) { string salt = SymmetricTestsHelper.DEFAULT_SALT; Symmetric symmetricHelper = new Symmetric(mode, keySize, salt); }
public void TestProtectVerifyIntegrity() { var key = "eda8506c1fb0bbcc3f62626fef074bbf2d09a8c7c608f3fa1482c9a625d00f75".ToByteArray(); var message = Encoding.ASCII.GetBytes("hoy, how are you?"); var plaintextAndTag = Symmetric.ProtectIntegrity(key, message); var retrievedMessage = Symmetric.VerifyIntegrity(key, plaintextAndTag); if (!message.SequenceEqual(retrievedMessage)) { throw new Exception("Verify did not work"); } // Tamper plaintextAndTag[plaintextAndTag.Length - 1] ^= 1; bool tamperDetected; try { Symmetric.VerifyIntegrity(key, plaintextAndTag); tamperDetected = false; } catch (Exception) { tamperDetected = true; } if (!tamperDetected) { throw new Exception("Verify did not work"); } }
public void DivTest() { var perm = Symmetric.Create(new int[] { 1, 2, 3, 0 }); Assert.Equal(new[] { 2, 3, 4, 1 }, new[] { 1, 2, 3, 4 } / perm); Assert.Equal(new[] { 1, 2, 3, 4 }, new[] { 1, 2, 3, 4 } / (perm / perm)); }
public void MulTest() { var perm = Symmetric.Create(new int[] { 1, 2, 3, 0 }); Assert.Equal(new[] { 4, 1, 2, 3 }, new[] { 1, 2, 3, 4 } *perm); Assert.Equal(new[] { 3, 4, 1, 2 }, new[] { 1, 2, 3, 4 } *(perm * perm)); }
public void Constructor_DefaultConstructor_InitializesWithNewKeyAndIV() { Symmetric symmetricHelper = new Symmetric(); Assert.IsNotNull(symmetricHelper.Key); Assert.IsNotNull(symmetricHelper.IV); }
protected virtual async Task BeforeWriteEncryptedAsync( Stream encryptedStream) { if (encryptedStream == null) { throw new ArgumentNullException(nameof(encryptedStream)); } if (!encryptedStream.CanWrite) { throw new ArgumentException(Resources.StreamNotWritable, nameof(encryptedStream)); } if (!IsSymmetricKeyInitialized) { throw new InvalidOperationException(Resources.UninitializedSymmetricKey); } Symmetric.GenerateIV(); var encryptedIV = EncryptIV(); // write the length and the contents of the IV in the encrypted stream await encryptedStream.WriteAsync(BitConverter.GetBytes(encryptedIV.Length), 0, sizeof(int)); await encryptedStream.WriteAsync(encryptedIV, 0, encryptedIV.Length); }
private void Decrypt(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.Decrypt(new Data(settings.Password.FromBase64()), _symmetricKey).ToString(); } catch (FormatException) { Logger.WarnFormat(Constants.ClientNameFormat, settings.Name, "Failed to decrypt password... loading clear value."); } catch (CryptographicException) { Logger.WarnFormat(Constants.ClientNameFormat, settings.Name, "Failed to decrypt password... loading clear value."); } } }
public string encrypt(string data) { var sym = new Symmetric(_p); sym.Key.Text = _k; return(sym.Encrypt(new Encryption.Data(data)).ToHex()); }
public void GenerateIV_Defaults() { var iv = Symmetric.GenerateIV("GenerateIV_Defau"); var ivB64 = Convert.ToBase64String(iv); Assert.AreEqual("R2VuZXJhdGVJVl9EZWZhdQ==", ivB64); }
public void SerializeDeserialize() { byte[] serializedBytes = Symmetric.Serialize(plainText); string deserializedValue = Symmetric.Deserialize <string>(serializedBytes); Assert.AreEqual(plainText, deserializedValue); }
public void Constructor_WithByteArray() { var key = Symmetric.GenerateKey("Constructor_WithByteArray", "salt"); var sut = new Symmetric(key); Assert.AreEqual(key, sut.Key); }
public void Constructor_DefaultConstructor_InitializesWithNewKeyAndIV() { Symmetric symmetricHelper = new Symmetric(); Assert.IsNotNull(symmetricHelper.Key); Assert.IsNotNull(symmetricHelper.IV); }
public void GenerateKey_Defaults() { var key = Symmetric.GenerateKey("GenerateKey_Defaults", "salt"); var keyB64 = Convert.ToBase64String(key); Assert.AreEqual("XMIqI9XOfHYsg0+y7vtLvGReLaWs1z6QwM+9faMjSXw=", keyB64); }
public void Constructor_WithBase64String() { var key = Convert.ToBase64String(Symmetric.GenerateKey("Constructor_WithByteArray", "salt")); var sut = new Symmetric(key); Assert.AreEqual(key, Convert.ToBase64String(sut.Key)); }
public void GenerateKey_KeySize128() { var key = Symmetric.GenerateKey("GenerateKey_KeySize128", "salt", keySize: KeySize.Bits128); var keyB64 = Convert.ToBase64String(key); Assert.AreEqual("MPkpMHyGEYhK9ymWaVSMfg==", keyB64); }
public void GenerateKey_MD5() { var key = Symmetric.GenerateKey("GenerateKey_MD5", "salt", TNT.Cryptography.Enumerations.HashAlgorithm.MD5); var keyB64 = Convert.ToBase64String(key); Assert.AreEqual("oaVJMDQueZox8TP9E9Wrz0Fwxquezw3Sw7JjmdRIBHs=", keyB64); }
public void GenerateKey_KeySize192() { var key = Symmetric.GenerateKey("GenerateKey_KeySize192", "salt", keySize: KeySize.Bits192); var keyB64 = Convert.ToBase64String(key); Assert.AreEqual("eJmnbYPLuu1cUx4WRfUgH/PXzhQI7NLp", keyB64); }
public void Constructor_WithModeValidKeySizeAndEmptySalt_Throws( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode, [Values(64, 128, 48, 192)] int keySize) { string salt = String.Empty; Symmetric symmetricHelper = new Symmetric(mode, keySize, salt); }
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(); }
public void Constructor_WithMode_InitializesWithKeyAndIV( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode) { Symmetric symmetricHelper = new Symmetric(mode); Assert.IsNotNull(symmetricHelper.Key); Assert.IsNotNull(symmetricHelper.IV); }
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 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); }
/// <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 PropertyKey_EmptyByteArray_SetsNullKey() { Symmetric symmetricHelper = new Symmetric(); symmetricHelper.Key = new byte[] { }; Assert.IsNull(symmetricHelper.Key); }
//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 void PropertyKey_NullValue_SetsNullKey() { Symmetric symmetricHelper = new Symmetric(); symmetricHelper.Key = null; Assert.IsNull(symmetricHelper.Key); }
public void EncryptToString_WithEmptyMessage_ReturnsEmptyString() { string message = String.Empty; Symmetric symmetricHelper = new Symmetric(); string actual = symmetricHelper.EncryptToString(message); Assert.IsNullOrEmpty(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); }
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 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 DeriveKeyStringIsDeterministicTest() { const string PASSWORD = "******"; const string SALT = "44071f6d181561670bda728d43fb79b443bb805afdebaf98622b5165e01b15fb"; var val1 = Symmetric.DeriveKey(PASSWORD, Utilities.HexToBinary(SALT)); var val2 = Symmetric.DeriveKey(PASSWORD, Utilities.HexToBinary(SALT)); CollectionAssert.AreEqual(val1, val2); }
/// <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 Constructor_FullConstructorWithEmptySalt_Throws( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode, [Values(64, 128, 48, 192)] int keySize) { string salt = String.Empty; byte[] iv = SymmetricTestsHelper.GetDefaultIV(mode); byte[] key = SymmetricTestsHelper.CreateKey(keySize); Symmetric symmetricHelper = new Symmetric(mode, salt, iv, key); }
public void Constructor_FullConstructorWithInvalidKeyLength_Throws( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode, [Values(16, 1024)] int keySize) { string salt = SymmetricTestsHelper.DEFAULT_SALT; byte[] iv = SymmetricTestsHelper.GetDefaultIV(mode); byte[] key = SymmetricTestsHelper.CreateKey(keySize); Symmetric symmetricHelper = new Symmetric(mode, salt, iv, key); }
public static string Decrypt(String strEncText, string strKey24) { Symmetric sym = new Symmetric(Symmetric.Provider.TripleDES, true); EncryptionClassLibrary.Encryption.Data key = new EncryptionClassLibrary.Encryption.Data(strKey24); EncryptionClassLibrary.Encryption.Data encryptedData = new EncryptionClassLibrary.Encryption.Data(); encryptedData.Base64 = strEncText; EncryptionClassLibrary.Encryption.Data decryptedData = new EncryptionClassLibrary.Encryption.Data(); decryptedData = sym.Decrypt(encryptedData, key); return decryptedData.ToString(); }
public static string Decrypt(String strEncText, string strKey24) { var sym = new Symmetric(Symmetric.Provider.TripleDES, true); var key = new Data(strKey24); var encryptedData = new Data(); encryptedData.Base64 = strEncText; var decryptedData = new Data(); decryptedData = sym.Decrypt(encryptedData, key); return decryptedData.ToString(); }
public void Constructor_FullConstructorWithEmptyIV_Throws( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode, [Values(64, 128, 48, 192)] int keySize) { string salt = SymmetricTestsHelper.DEFAULT_SALT; byte[] iv = new byte[] { }; byte[] key = SymmetricTestsHelper.CreateKey(keySize); Symmetric symmetricHelper = new Symmetric(mode, salt, iv, key); }
/// <summary> /// Decrypt encrypted text via Rijndael symmetric-key algorithm. Returns the plain text /// </summary> /// <param name="encryptedText">Base64 string representation of the encrypted text</param> /// <returns></returns> public string Decrypt(string encryptedText) { try { string eKey = this._key; var sym = new Symmetric(Symmetric.Provider.Rijndael); var key = new Data(eKey); var encryptedData = new Data {Base64 = encryptedText}; return sym.Decrypt(encryptedData, key).ToString(); } catch(Exception) { return null; } }
public void PropertyKey_ValidKey_SetsKey() { byte[] key = SymmetricTestsHelper.CreateKey(64); Symmetric symmetricHelper = new Symmetric(); symmetricHelper.Key = key; Assert.AreEqual(key, symmetricHelper.Key); }
public void Constructor_WithModeValidKeySizeAndNullSalt_Throws( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode, [Values(64, 128, 48, 192)] int keySize) { Symmetric symmetricHelper = new Symmetric(mode, keySize, null); }
public void Constructor_WithMode_InitializesWithKeyAndIV( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode) { Symmetric symmetricHelper = new Symmetric(mode); Assert.IsNotNull(symmetricHelper.Key); Assert.IsNotNull(symmetricHelper.IV); }
public void Decrypt_WithDefaultSecretString_ReturnsEmptyString( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode) { string secret = SymmetricTestsHelper.GetDefaultSecretString(mode); string expected = SymmetricTestsHelper.DEFAULT_MESSAGE; Symmetric symmetricHelper = new Symmetric(mode); symmetricHelper.Salt = SymmetricTestsHelper.DEFAULT_SALT; symmetricHelper.IV = SymmetricTestsHelper.GetDefaultIV(mode); symmetricHelper.Key = SymmetricTestsHelper.GetDefaultKey(mode); string actual = symmetricHelper.Decrypt(secret); Assert.AreEqual(expected, actual); }
public void Decrypt_WithNullSecret_ReturnsEmptyString() { byte[] secret = null; Symmetric symmetricHelper = new Symmetric(); string actual = symmetricHelper.Decrypt(secret); Assert.IsNullOrEmpty(actual); }
public void PropertyIV_NullValue_DoesntChange() { byte[] newIV = null; Symmetric symmetricHelper = new Symmetric(); byte[] oldIV = symmetricHelper.IV; byte[] oldKey = symmetricHelper.Key; symmetricHelper.IV = newIV; Assert.AreEqual(oldIV, symmetricHelper.IV); Assert.AreEqual(oldKey, symmetricHelper.Key); }
public void EncryptToString_WithNullMessage_ReturnsEmptyString() { string message = null; Symmetric symmetricHelper = new Symmetric(); string actual = symmetricHelper.EncryptToString(message); Assert.IsNullOrEmpty(actual); }
public void PropertyKey_NullValue_SetsNullKey() { Symmetric symmetricHelper = new Symmetric(); symmetricHelper.Key = null; Assert.IsNull(symmetricHelper.Key); }
public void PropertyKey_KeyWithInvalidLength_Throws([Values(16, 1024)] int keySize) { Symmetric symmetricHelper = new Symmetric(); symmetricHelper.Key = SymmetricTestsHelper.CreateKey(keySize); }
public void Constructor_WithModeValidKeySizeAndEmptySalt_Throws( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode, [Values(64, 128, 48, 192)] int keySize) { string salt = String.Empty; Symmetric symmetricHelper = new Symmetric(mode, keySize, salt); }
public void PropertyIV_DefaultIV_ChangesKeyAndIV( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode) { byte[] newIV = SymmetricTestsHelper.GetDefaultIV(mode); Symmetric symmetricHelper = new Symmetric(mode); byte[] oldIV = symmetricHelper.IV; byte[] oldKey = symmetricHelper.Key; symmetricHelper.IV = newIV; Assert.AreNotEqual(oldIV, symmetricHelper.IV); Assert.AreNotEqual(oldKey, symmetricHelper.Key); }
public void PropertyIV_EmptyByteArray_DoesntChange() { byte[] newIV = new byte[] { }; Symmetric symmetricHelper = new Symmetric(); byte[] oldIV = symmetricHelper.IV; byte[] oldKey = symmetricHelper.Key; symmetricHelper.IV = newIV; Assert.AreEqual(oldIV, symmetricHelper.IV); Assert.AreEqual(oldKey, symmetricHelper.Key); }
public void Constructor_FullConstructorWithNullKey_Throws( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode) { string salt = SymmetricTestsHelper.DEFAULT_SALT; byte[] iv = SymmetricTestsHelper.GetDefaultIV(mode); byte[] key = null; Symmetric symmetricHelper = new Symmetric(mode, salt, iv, key); }
public void Decrypt_WithNullString_ReturnsEmptyString( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode) { string secret = null; Symmetric symmetricHelper = new Symmetric(mode); symmetricHelper.Salt = SymmetricTestsHelper.DEFAULT_SALT; symmetricHelper.IV = SymmetricTestsHelper.GetDefaultIV(mode); symmetricHelper.Key = SymmetricTestsHelper.GetDefaultKey(mode); string actual = symmetricHelper.Decrypt(secret); Assert.IsNullOrEmpty(actual); }
public void Encrypt_WithNullMessage_ReturnsNull() { string message = null; Symmetric symmetricHelper = new Symmetric(); byte[] actual = symmetricHelper.Encrypt(message); Assert.IsNull(actual); }
public void PropertyKey_EmptyByteArray_SetsNullKey() { Symmetric symmetricHelper = new Symmetric(); symmetricHelper.Key = new byte[] { }; Assert.IsNull(symmetricHelper.Key); }
public void Constructor_DefaultConstructor_InitializesWithDefaultAlgorithm() { Symmetric symmetricHelper = new Symmetric(); Assert.AreEqual(Symmetric.DEFAULT_ALGORITHM, symmetricHelper.Mode); }
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 Constructor_WithModeInvalidKeySizeAndValidSalt_Throws( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode, [Values(0, 1024)] int keySize) { string salt = SymmetricTestsHelper.DEFAULT_SALT; Symmetric symmetricHelper = new Symmetric(mode, keySize, salt); }
public void Constructor_WithModeValidKeySizeAndEmptySalt_InitializesWithSaltKeyAndIV( [Values(Algorithm.DES, Algorithm.TripleDES, Algorithm.RC2, Algorithm.Rijndael)] Algorithm mode, [Values(64, 128, 48, 192)] int keySize) { string salt = SymmetricTestsHelper.DEFAULT_SALT; Symmetric symmetricHelper = new Symmetric(mode, keySize, salt); Assert.AreEqual(mode, symmetricHelper.Mode); Assert.AreEqual(keySize, symmetricHelper.KeySize); Assert.AreEqual(salt, symmetricHelper.Salt); Assert.IsNotNull(symmetricHelper.Key); Assert.IsNotNull(symmetricHelper.IV); }