public void TestRandomString(int?numChars) { string passphrase = Guid.NewGuid().ToString("N"); string input = null; if (numChars.HasValue) { input = GetRandomString(numChars.Value, numChars.Value); } byte[] encrypted; string decrypted; using (var algorithm = new Symmetric_AES_ECB_Passphrase()) { encrypted = algorithm.EncryptString(input, passphrase); } using (var algorithm = new Symmetric_AES_ECB_Passphrase()) { decrypted = algorithm.DecryptBytes(encrypted, passphrase).AsString(); } if (input == null) { Assert.IsNull(encrypted, "encrypted"); Assert.IsNull(decrypted, "decrypted"); } else { Assert.IsTrue(encrypted.Length >= input.Length, "encrypted.Length"); Assert.IsTrue(decrypted.SequenceEqual(input), "Decrypted does not match original."); } }
public void TestIncorrectKeyFails() { string passphrase = Guid.NewGuid().ToString("N"); byte[] input = GetRandomBytes(1000, 5000); byte[] encrypted; using (var algorithm = new Symmetric_AES_ECB_Passphrase()) { encrypted = algorithm.EncryptBytes(input, passphrase); } passphrase = Guid.NewGuid().ToString("N"); using (var algorithm = new Symmetric_AES_ECB_Passphrase()) { algorithm.DecryptBytes(encrypted, passphrase); } }
public void TestUnicodeString() { string passphrase = Guid.NewGuid().ToString("N"); string input = Convert.ToBase64String(SecureRandomizer.GetRandomBytes(100)) + "\u01e2\u01f0\u020e\u0229"; byte[] encrypted; string decrypted; using (var algorithm = new Symmetric_AES_ECB_Passphrase()) { encrypted = algorithm.EncryptString(input, passphrase); } using (var algorithm = new Symmetric_AES_ECB_Passphrase()) { Assert.IsTrue(encrypted.Length > 100, "encrypted.Length"); decrypted = algorithm.DecryptBytes(encrypted, passphrase).AsString(); } Assert.AreEqual(input, decrypted, string.Format("{0} | {1}", input, decrypted)); }