public void TestRandomString(int?numChars) { byte[] cryptoKey, authKey; SymmetricAuthenticated_AES_HMACSHA256.GenerateKeys(out cryptoKey, out authKey); string input = null; if (numChars.HasValue) { input = GetRandomString(numChars.Value, numChars.Value); } byte[] encrypted; string decrypted; using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256()) { encrypted = algorithm.EncryptString(input, cryptoKey, authKey); } using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256()) { decrypted = algorithm.DecryptBytes(encrypted, cryptoKey, authKey).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 TestUnicodeString() { byte[] cryptoKey, authKey; SymmetricAuthenticated_AES_HMACSHA256.GenerateKeys(out cryptoKey, out authKey); string input = Convert.ToBase64String(SecureRandomizer.GetRandomBytes(100)) + "\u01e2\u01f0\u020e\u0229"; byte[] encrypted; string decrypted; using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256()) { encrypted = algorithm.EncryptString(input, cryptoKey, authKey); } using (var algorithm = new SymmetricAuthenticated_AES_HMACSHA256()) { Assert.IsTrue(encrypted.Length > 100, "encrypted.Length"); decrypted = algorithm.DecryptBytes(encrypted, cryptoKey, authKey).AsString(); } Assert.AreEqual(input, decrypted, string.Format("{0} | {1}", input, decrypted)); }