public void Encrypt_Decrypt() { //Arrange string message = "message"; dynamic json = new JObject(); json.Data = message; byte[] key = { 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; AESHelper aESHelper = new AESHelper(); byte[] data = aESHelper.Normalize(json); byte[] cipherbytes; //Act using (MemoryStream stream = new MemoryStream(data)) { byte[] block = new byte[16]; cipherbytes = new byte[0]; while (stream.Read(block, 0, 16) > 0) { Array.Resize(ref cipherbytes, cipherbytes.Length + 16); Array.Copy(serviceMock.Encrypt(block, key), 0, cipherbytes, cipherbytes.Length - 16, 16); } } byte[] plainbytes = new byte[cipherbytes.Length]; using (MemoryStream stream = new MemoryStream(cipherbytes)) { byte[] block = new byte[16]; int count = 0; while (stream.Read(block, 0, 16) > 0) { Array.Copy(serviceMock.Decrypt(block, key), 0, plainbytes, count, 16); count += 16; } } var result = aESHelper.ParseToJson(plainbytes); //Assert Assert.IsNotNull(cipherbytes); Assert.IsNotNull(plainbytes); Assert.AreEqual(cipherbytes.Length, plainbytes.Length); Assert.AreEqual(Newtonsoft.Json.JsonConvert.SerializeObject(json), Newtonsoft.Json.JsonConvert.SerializeObject(result)); }
/// <summary> /// Encrypts the message. Implements <see cref="IAESService.Encrypt(dynamic)"/>. /// </summary> /// <param name="json">The JSON object to be encrypted.</param> /// <returns>The cipherbytes.</returns> public byte[] Encrypt(dynamic json) { helperAES = new AESHelper(); byte[] message = helperAES.Normalize(json); algorithm = new Grasshopper(); using (MemoryStream stream = new MemoryStream(message)) { byte[] block = new byte[16]; byte[] cipherbytes = new byte[0]; while (stream.Read(block, 0, blockLength) > 0) { Array.Resize(ref cipherbytes, cipherbytes.Length + blockLength); Array.Copy(algorithm.Encrypt(block, Keys.GrasshopperKey), 0, cipherbytes, cipherbytes.Length - blockLength, blockLength); } return(cipherbytes); } }