Пример #1
0
    private static void DecryptDocument(string name)
    {
        bool documentFound = false;

        foreach (var document in documents)
        {
            if (document.Name == name)
            {
                documentFound = true;

                IEncryptable encryptableDocument = document as IEncryptable;
                if (encryptableDocument != null)
                {
                    encryptableDocument.Decrypt();
                    Console.WriteLine("Document decrypted: " + name);
                }
                else
                {
                    Console.WriteLine("Document does not support decryption: " + name);
                }
            }
        }
        if (!documentFound)
        {
            Console.WriteLine("Document not found: " + name);
        }
    }
Пример #2
0
        private static void DecryptDocument(string name)
        {
            //
            bool found = false;

            foreach (var item in documents)
            {
                if (item.Name == name)
                {
                    found = true;
                    IEncryptable obj = item as IEncryptable;
                    if (obj != null)
                    {
                        obj.Decrypt();
                        Console.WriteLine("Document decrypted: {0}", name);
                    }
                    else
                    {
                        Console.WriteLine("Document does not support decryption: {0}", name);
                    }
                }
            }
            if (found == false)
            {
                Console.WriteLine("Document not found: {0}", name);
            }
        }
Пример #3
0
        /// <summary>
        /// Decrypts the data of the IEncryptable with the key and sets the IV for future decryption
        /// </summary>
        /// <param name="keyMaterial">The AES encryption key</param>
        /// <param name="encryptable">The object to be decrypted, containing encrypted data (ciphertext)</param>
        /// <returns>The object with decrypted data (plaintext)</returns>
        private static IEncryptable DecryptData(byte[] keyMaterial, IEncryptable encryptable)
        {
            var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
            var key      = provider.CreateSymmetricKey(keyMaterial);

            var plainText = WinRTCrypto.CryptographicEngine.Decrypt(key, encryptable.GetData(), encryptable.GetIV());

            encryptable.SetData(plainText);

            return(encryptable);
        }
Пример #4
0
        /// <summary>
        /// Encrypts the data of the IEncryptable with the key and sets the IV for future decryption
        /// </summary>
        /// <param name="keyMaterial">The AES encryption key</param>
        /// <param name="encryptable">The object to be encrypted, containing not encrypted data (plaintext)</param>
        /// <returns>The object with encrypted data (ciphertext)</returns>
        private static IEncryptable EncryptData(byte[] keyMaterial, IEncryptable encryptable)
        {
            var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
            var key      = provider.CreateSymmetricKey(keyMaterial);

            var iv         = WinRTCrypto.CryptographicBuffer.GenerateRandom(provider.BlockLength);
            var cipherText = WinRTCrypto.CryptographicEngine.Encrypt(key, encryptable.GetData(), iv);

            encryptable.SetData(cipherText);
            encryptable.SetIV(iv);

            return(encryptable);
        }
Пример #5
0
 public static bool Initialize(IImageHolder imageHolder, IEncryptable encryptable, ICompressible compressible)
 {
     try
     {
         _imageHolder     = imageHolder;
         _imageEncryptor  = encryptable;
         _imageComprosser = compressible;
         return(true);
     }
     catch (Exception e)
     {
         throw new ArgumentException("Arguments can't match the correct types!");
     }
 }
Пример #6
0
 private bool DecryptIncomingPacket(IEncryptable packet, EncryptionBase decrypter)
 {
     if (packet.isEncrypted)
     {
         if (decrypter.EncryptionTypeByte == packet.EncryptionMethodByte)
         {
             return(packet.Decrypt(decrypter));
         }
         else
         {
             throw new LoggableException("Failed to decrypt byte[] blob due to decryptor object being of byte: " + decrypter.EncryptionTypeByte.ToString() +
                                         " and lidgren packet encryption byte being: " + packet.EncryptionMethodByte, null, LogType.Error);
         }
     }
     else
     {
         return(false);
     }
 }
Пример #7
0
    private static void EncryptAllDocuments()
    {
        bool documentEncrypted = false;

        foreach (var document in documents)
        {
            IEncryptable encryptableDocument = document as IEncryptable;
            if (encryptableDocument != null)
            {
                documentEncrypted = true;
                encryptableDocument.Encrypt();
            }
        }

        if (documentEncrypted)
        {
            Console.WriteLine("All documents encrypted");
        }
        else
        {
            Console.WriteLine("No encryptable documents found");
        }
    }
Пример #8
0
        private static void EncryptAllDocuments()
        {
            //
            bool found = false;

            foreach (var item in documents)
            {
                IEncryptable obj = item as IEncryptable;
                if (obj != null)
                {
                    found = true;
                    obj.Encrypt();
                }
            }
            if (found == true)
            {
                Console.WriteLine("All documents encrypted");
            }
            else
            {
                Console.WriteLine("No encryptable documents found");
            }
        }
Пример #9
0
 /// <summary>
 /// Decrypts data from an object with encrypted data
 /// </summary>
 /// <param name="password">The password the user specified</param>
 /// <param name="salt">The generated salt for the user</param>
 /// <param name="encryptable">The object to be decrypted, containing encrypted data (ciphertext)</param>
 /// <returns>The object with decrypted data (plaintext)</returns>
 public static IEncryptable DecryptData(string password, byte[] salt, IEncryptable encryptable) =>
 DecryptData(DeriveAESEncryptionKeyFromPassword(password, salt), encryptable);
Пример #10
0
        public static void Main(string[] args)
        {
            // create a document object
            Document doc = new Document("Test Document");

            // cast the document to the various interfaces
            IStorable isDoc = doc as IStorable;

            if (isDoc != null)
            {
                isDoc.Read();
            }
            else
            {
                Console.WriteLine("IStorable not supported");
            }

            ICompressible icDoc = doc as ICompressible;

            if (icDoc != null)
            {
                icDoc.Compress();
            }
            else
            {
                Console.WriteLine("Compressible not supported");
            }

            ILoggedCompressible ilcDoc = doc as ILoggedCompressible;

            if (ilcDoc != null)
            {
                ilcDoc.LogSavedBytes();
                ilcDoc.Compress();
                // ilcDoc.Read();
            }
            else
            {
                Console.WriteLine("LoggedCompressible not supported");
            }

            IStorableCompressible isc = doc as IStorableCompressible;

            if (isc != null)
            {
                isc.LogOriginalSize();  // IStorableCompressible
                isc.LogSavedBytes();    // ILoggedCompressible
                isc.Compress();         // ICompressible
                isc.Read();             // IStorable
            }
            else
            {
                Console.WriteLine("StorableCompressible not supported");
            }

            IEncryptable ie = doc as IEncryptable;

            if (ie != null)
            {
                ie.Encrypt();
            }
            else
            {
                Console.WriteLine("Encryptable not supported");
            }

            IStorable isDocument = (IStorable)doc;

            isDocument.Status = 0;
            isDocument.Read();

            // error!
//            IStorable doc1 = new IStorable ();

            // OK!
            IStorable doc2 = new Document("Test2 document");

            if (doc2)
            {
                doc2.Read();
            }
        }
Пример #11
0
        static void Main(string[] args)
        {
            Document doc = new Document("Hello! This is Document");

            IStorable isDoc = doc as IStorable;

            if (isDoc != null)
            {
                isDoc.Read();
            }
            else
            {
                Console.WriteLine("IStorable not support");
            }

            ICompressible icDoc = doc as ICompressible;

            if (icDoc != null)
            {
                icDoc.Compress();
            }
            else
            {
                Console.WriteLine("ICompressible not support");
            }

            ILoggedCompressible ilcDoc = doc as ILoggedCompressible;

            if (ilcDoc != null)
            {
                ilcDoc.LogSavedByte();
                ilcDoc.Compress();
            }
            else
            {
                Console.WriteLine("ILoggedCompressible not support");
            }
            IStorableCompressible iscDoc = doc as IStorableCompressible;

            if (iscDoc != null)
            {
                iscDoc.LogSavedByte();
                iscDoc.LogOriginalSize();
                iscDoc.Compress();
                iscDoc.Read();
            }
            else
            {
                Console.WriteLine("IStorableCompressible not support");
            }

            IEncryptable ieDoc = doc as IEncryptable;

            if (ieDoc != null)
            {
                ieDoc.Encrypt();
            }
            else
            {
                Console.WriteLine("Encrypt not support");
            }
            Console.ReadLine();
        }
Пример #12
0
 public DataEncoder(IEncryptable encryptor)
 {
     this.encryptor = encryptor;
 }
 public Encryptor(IEncryptable encryptionStrategy)
 {
     _encryptionStrategy = encryptionStrategy;
 }
Пример #14
0
        static void Main()
        {
            // 创建Document对象
            Document doc = new Document("Test Document");

            // 将Document转换为各种接口
            IStorable isDoc = doc as IStorable;

            if (isDoc != null)
            {
                isDoc.Read();
            }
            else
            {
                Console.WriteLine("IStorable noe supported");
            }

            IComparable icDoc = doc as ICompressible;

            if (icDoc != null)
            {
                icDoc.Compress();
            }
            else
            {
                Console.WriteLine("Compressible not supported");
            }

            ILoggedCompressible ilcDoc = doc as ILoggedCompressible;

            if (ilcDoc != null)
            {
                ilcDoc.LogSavedBytes();
                ilcDoc.Compress();
            }
            else
            {
                Console.WriteLine("LoggedCompressible not supported");
            }

            IStorableCompressible isc = doc as IStorableCompressible;

            if (isc != null)
            {
                isc.LogOriginalSize();
                isc.LogSavedBytes();
                isc.Compress();
                isc.Read();
            }
            else
            {
                Console.WriteLine("StorableCompressible not supported");
            }

            IEncryptable ie = doc as IEncryptable;

            if (ie != null)
            {
                ie.Encrypt();
            }
            else
            {
                Console.WriteLine("Encryptable not supported");
            }
        }
        static void Main()
        {
            // create a document object
            Document doc = new Document("Test Document");

            // cast the document to the various interfaces
            IStorable isDoc = doc as IStorable;

            if (isDoc != null)
            {
                isDoc.Read();
            }
            else
            {
                Console.WriteLine("IStorable not supported");
            }

            ICompressible icDoc = doc as ICompressible;

            if (icDoc != null)
            {
                icDoc.Compress();
            }
            else
            {
                Console.WriteLine("Compressible not supported");
            }

            ILoggedCompressible ilcDoc = doc as ILoggedCompressible;

            if (ilcDoc != null)
            {
                ilcDoc.LogSavedBytes();
                ilcDoc.Compress();
                // ilcDoc.Read();
            }
            else
            {
                Console.WriteLine("LoggedCompressible not supported");
            }

            IStorableCompressible isc = doc as IStorableCompressible;

            if (isc != null)
            {
                isc.LogOriginalSize(); // IStorableCompressible
                isc.LogSavedBytes();   // ILoggedCompressible
                isc.Compress();        // ICompressible
                isc.Read();            // IStorable
            }
            else
            {
                Console.WriteLine("StorableCompressible not supported");
            }

            IEncryptable ie = doc as IEncryptable;

            if (ie != null)
            {
                ie.Encrypt();
            }
            else
            {
                Console.WriteLine("Encryptable not supported");
            }
        }
Пример #16
0
        static void Main()
        {
            //pravi objekat dokumenta
            Document doc = new Document("Test document");

            //pretvara dokument za razlicita sucelja
            IStorable isDoc = doc as IStorable;

            if (isDoc != null)
            {
                isDoc.Read();
            }
            else
            {
                Console.WriteLine("IStorable not supported");
            }

            ICompressible icDoc = doc as ICompressible;

            if (icDoc != null)
            {
                icDoc.Compress();
            }
            else
            {
                Console.WriteLine("Compressible not supported");
            }

            ILoggedCompressible ilcDoc = doc as ILoggedCompressible;

            if (ilcDoc != null)
            {
                ilcDoc.LogSavedBytes();
                ilcDoc.Compress();
                //ilcDoc.Read(); //ne moze pozvati metodu read
            }
            else
            {
                Console.WriteLine("LoggedCompressible not supported");
            }

            IStorableCompressible isc = doc as IStorableCompressible;

            if (isc != null)
            {
                isc.LogOriginalSize();  //IStorableCompressible
                isc.LogSavedBytes();    //ILoggedCompressible
                isc.Compress();         //ICompressible
                isc.Read();             //IStorable
            }
            else
            {
                Console.WriteLine("StorableCompressible not supported");
            }

            IEncryptable ie = doc as IEncryptable;

            if (ie != null)
            {
                ie.Encrypt();
            }
            else
            {
                Console.WriteLine("Encrytable not supported");
            }
        }
Пример #17
0
        private static void ListDocuments()
        {
            //
            if (documents.Count == 0)
            {
                Console.WriteLine("No documents found");
            }
            foreach (var item in documents)
            {
                if (item is IEncryptable)
                {
                    IEncryptable obj = item as IEncryptable;
                    if (obj.IsEncrypted == true)
                    {
                        Console.WriteLine("{0}[encrypted]", obj.GetType().Name);
                        continue;
                    }
                }

                Type          type = item.GetType();
                StringBuilder sb   = new StringBuilder();
                sb.Append(type.Name + "[");
                if (type.Name == "TextDocument")
                {
                    TextDocument doc = item as TextDocument;
                    if (doc.Charset != null)
                    {
                        sb.AppendFormat("charset={0};", doc.Charset);
                    }
                    if (doc.Content != null)
                    {
                        sb.AppendFormat("content={0};", doc.Content);
                    }
                    sb.AppendFormat("name={0}", doc.Name);
                }
                else if (type.Name == "PDFDocument")
                {
                    PDFDocument doc = item as PDFDocument;
                    if (doc.Content != null)
                    {
                        sb.AppendFormat("content={0};", doc.Content);
                    }
                    sb.AppendFormat("name={0}", doc.Name);
                    if (doc.Pages != null)
                    {
                        sb.AppendFormat(";pages={0}", doc.Pages);
                    }
                    if (doc.Size != null)
                    {
                        sb.AppendFormat(";size={0}", doc.Size);
                    }
                }
                else if (type.Name == "WordDocument")
                {
                    WordDocument doc = item as WordDocument;
                    if (doc.Chars != null)
                    {
                        sb.AppendFormat("chars={0};", doc.Chars);
                    }
                    if (doc.Content != null)
                    {
                        sb.AppendFormat("content={0};", doc.Content);
                    }
                    sb.AppendFormat("name={0}", doc.Name);
                    if (doc.Size != null)
                    {
                        sb.AppendFormat(";size={0}", doc.Size);
                    }
                    if (doc.Version != null)
                    {
                        sb.AppendFormat(";version={0}", doc.Version);
                    }
                }
                else if (type.Name == "ExcelDocument")
                {
                    ExcelDocument doc = item as ExcelDocument;
                    if (doc.Cols != null)
                    {
                        sb.AppendFormat("cols={0};", doc.Cols);
                    }
                    if (doc.Content != null)
                    {
                        sb.AppendFormat("content={0};", doc.Content);
                    }
                    sb.AppendFormat("name={0}", doc.Name);
                    if (doc.Rows != null)
                    {
                        sb.AppendFormat(";rows={0}", doc.Rows);
                    }
                    if (doc.Size != null)
                    {
                        sb.AppendFormat(";size={0}", doc.Size);
                    }
                    if (doc.Version != null)
                    {
                        sb.AppendFormat(";version={0}", doc.Version);
                    }
                }
                else if (type.Name == "AudioDocument")
                {
                    AudioDocument doc = item as AudioDocument;
                    if (doc.Content != null)
                    {
                        sb.AppendFormat("content={0};", doc.Content);
                    }
                    if (doc.Length != null)
                    {
                        sb.AppendFormat("length={0};", doc.Length);
                    }
                    sb.AppendFormat("name={0}", doc.Name);
                    if (doc.Samplerate != null)
                    {
                        sb.AppendFormat(";samplerate={0}", doc.Samplerate);
                    }
                    if (doc.Size != null)
                    {
                        sb.AppendFormat(";size={0}", doc.Size);
                    }
                }
                else if (type.Name == "VideoDocument")
                {
                    VideoDocument doc = item as VideoDocument;
                    if (doc.Content != null)
                    {
                        sb.AppendFormat("content={0};", doc.Content);
                    }
                    if (doc.Framerate != null)
                    {
                        sb.AppendFormat("framerate={0};", doc.Framerate);
                    }
                    sb.AppendFormat("name={0}", doc.Name);
                    if (doc.Length != null)
                    {
                        sb.AppendFormat(";length={0}", doc.Length);
                    }
                    if (doc.Size != null)
                    {
                        sb.AppendFormat(";size={0}", doc.Size);
                    }
                }
                sb.Append("]");
                Console.WriteLine(sb.ToString());
            }
        }