2015-05-13 20:22:35 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2015-05-13 17:11:55 +00:00
|
|
|
|
using xClient.Core.Encryption;
|
|
|
|
|
using xClient.Core.Helper;
|
|
|
|
|
|
2015-05-13 17:17:00 +00:00
|
|
|
|
namespace xClient.Tests.Core.Encryption
|
2015-05-13 17:11:55 +00:00
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class AESTests
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
2015-05-13 20:22:35 +00:00
|
|
|
|
public void EncryptAndDecryptStringTest()
|
2015-05-13 17:11:55 +00:00
|
|
|
|
{
|
|
|
|
|
var input = Helper.GetRandomName(100);
|
|
|
|
|
var password = Helper.GetRandomName(50);
|
|
|
|
|
var encrypted = AES.Encrypt(input, password);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(encrypted);
|
|
|
|
|
Assert.AreNotEqual(encrypted, input);
|
|
|
|
|
|
|
|
|
|
var decrypted = AES.Decrypt(encrypted, password);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(input == decrypted);
|
|
|
|
|
}
|
2015-05-13 20:22:35 +00:00
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void EncryptAndDecryptByteArrayTest()
|
|
|
|
|
{
|
|
|
|
|
var input = Helper.GetRandomName(100);
|
|
|
|
|
var inputByte = GetBytes(input);
|
|
|
|
|
|
|
|
|
|
var password = Helper.GetRandomName(50);
|
|
|
|
|
var passwordByte = GetBytes(password);
|
|
|
|
|
var encrypted = AES.Encrypt(inputByte, passwordByte);
|
|
|
|
|
|
|
|
|
|
Assert.IsNotNull(encrypted);
|
|
|
|
|
Assert.AreNotEqual(encrypted, input);
|
|
|
|
|
|
|
|
|
|
var decrypted = AES.Decrypt(encrypted, passwordByte);
|
|
|
|
|
|
|
|
|
|
//The decryption method is adding on 9 blank bytes.
|
|
|
|
|
var realDecrypted = decrypted.Take(200).ToArray();
|
|
|
|
|
|
|
|
|
|
CollectionAssert.AreEqual(inputByte,realDecrypted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static byte[] GetBytes(string str)
|
|
|
|
|
{
|
|
|
|
|
byte[] bytes = new byte[str.Length * sizeof(char)];
|
|
|
|
|
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-13 17:11:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|