/// <summary> /// Method for decrypting an extracted watermark. /// </summary> /// <param name="password">The password that was used during encryption.</param> public Watermark Decrypt(string password) { bytes = new Rfc2898DeriveBytes(password, salt); this.key = bytes.GetBytes(Algorithm.KeySize / 8); Algorithm.Key = key; int ivLength = BitConverter.ToInt32(cryptedData, 0); byte[] iv = new byte[ivLength]; Array.Copy(cryptedData, 4, iv, 0, ivLength); Algorithm.IV = iv; int cryptDataLength = BitConverter.ToInt32(cryptedData, 4 + ivLength); byte[] cryptedData2 = new byte[cryptDataLength]; Array.Copy(cryptedData, 4 + ivLength + 4, cryptedData2, 0, cryptDataLength); byte[] decrypted = Decrypt(cryptedData2, Algorithm); byte markType = decrypted[0]; int markDataLength = BitConverter.ToInt32(decrypted, 1); byte[] markData = new byte[markDataLength]; Array.Copy(decrypted, 5, markData, 0, markDataLength); Watermark decryptedMark = null; switch (markType) { case 1: decryptedMark = TextWatermark.LoadFromBytes(markData); break; case 2: decryptedMark = FileWatermark.LoadFromBytes(markData); break; case 3: decryptedMark = BinaryWatermark.LoadFromBytes(markData); break; case 4: decryptedMark = CompositeWatermark.LoadFromBytes(markData); break; case 9: decryptedMark = EncryptedWatermark.LoadFromBytes(markData); break; } return(decryptedMark); }
internal static CompositeWatermark LoadFromBytes(byte[] data) { CompositeWatermark comp = new CompositeWatermark(); MemoryStream ms = new MemoryStream(data); byte[] type = new byte[1]; byte[] dword = new byte[4]; byte[] markData; while (ms.Position < ms.Length) { ms.Read(type, 0, 1); ms.Read(dword, 0, 4); markData = new byte[BitConverter.ToInt32(dword, 0)]; ms.Read(markData, 0, markData.Length); Watermark mark = null; switch (type[0]) { case 1: mark = TextWatermark.LoadFromBytes(markData); break; case 2: mark = FileWatermark.LoadFromBytes(markData); break; case 3: mark = BinaryWatermark.LoadFromBytes(markData); break; case 4: mark = CompositeWatermark.LoadFromBytes(markData); break; case 9: mark = EncryptedWatermark.LoadFromBytes(markData); break; } comp.watermarks.Add(mark); } return(comp); }