예제 #1
0
 public void EncryptFile(string inputFilePath, string outputFilePath, TKey encryptKey)
 {
     base.outputFilePath = Path.ChangeExtension(outputFilePath, EncryptedFileExtension);
     using (FileStream inputStream = StreamMaker.MakeInputStream(inputFilePath))
         using (FileStream outputStream = StreamMaker.MakeOutputStream(base.outputFilePath + EncryptedFileExtension))
         {
             EncryptFileExtension(inputFilePath, outputStream, encryptKey);
             EqualizeFileLengthEncryption(inputStream, outputStream, encryptKey);
             EncryptUntilEndOfFile(inputStream, outputStream, encryptKey);
         }
 }
예제 #2
0
        public AffineKey TryGetEncryptKeyByOriginalExtension(string inputFilePath, string originalExtension)
        {
            if (!signatures.ContainsKey(originalExtension))
            {
                throw new ArgumentException($"Signatures dictionary does not contain extension \"{originalExtension}\". Encrypt key cannot be calculated. Add \"{originalExtension}\" extension and its signature to the signatures dictionary first.");
            }

            AffineKey encryptKey;

            using (FileStream inputFileStream = StreamMaker.MakeInputStream(inputFilePath))
            {
                encryptKey = GetEncryptKey(inputFileStream, originalExtension);
            }

            return(encryptKey);
        }
예제 #3
0
        public void DecryptFile(string inputFilePath, string outputFilePath, TKey encryptKey)
        {
            if (!IsEncryptedFile(inputFilePath))
            {
                throw new ArgumentException("Cannot decrypt file, because this file is not decrypted using corresponding encryption agorithm.");
            }

            TKey decryptKey = encryptKey.GetInverseMod(Ring.N);

            using (FileStream inputStream = StreamMaker.MakeInputStream(inputFilePath))
            {
                string decryptedFileExtension = DecryptFileExtension(inputStream, decryptKey);
                base.outputFilePath = Path.ChangeExtension(outputFilePath, decryptedFileExtension);
                using (FileStream outputStream = StreamMaker.MakeOutputStream(base.outputFilePath + decryptedFileExtension))
                {
                    EqualizeFileLengthDecryption(inputStream, outputStream, decryptKey);
                    EncryptUntilEndOfFile(inputStream, outputStream, decryptKey);
                }
            }
        }
예제 #4
0
        public void DiscoverMessage(string coverMessageFilePath, string readMessageFilePath, int hiddingStartByte)
        {
            using (FileStream coverStream = StreamMaker.MakeInputStream(coverMessageFilePath))
                using (FileStream readMessageStream = StreamMaker.MakeOutputStream(readMessageFilePath))
                {
                    if (hiddingStartByte > coverStream.Length)
                    {
                        throw new ArgumentException("Hiding start byte cannot be greater than covered message file length.");
                    }

                    coverStream.Position = hiddingStartByte;
                    int  messageCharacter, coverCharacter, messageCharacterBit;
                    bool endOfMessage = false;
                    while (!endOfMessage)
                    {
                        messageCharacter = 0;
                        for (int i = 0; i < 8; i++)
                        {
                            coverCharacter = coverStream.ReadByte();
                            if (coverCharacter == -1)
                            {
                                NotifyFinishedWork();
                                return; // End of cover file reached before end of message character found.
                            }
                            messageCharacterBit = coverCharacter & 1;
                            messageCharacter   |= messageCharacterBit << i;
                        }
                        endOfMessage = messageCharacter == endOfMessageCharacter;
                        if (!endOfMessage)
                        {
                            readMessageStream.WriteByte((byte)messageCharacter);
                            SetProgressPercentageAndCheckCancellation(coverStream);
                        }
                        else
                        {
                            NotifyFinishedWork();
                        }
                    }
                }
        }
예제 #5
0
        public void HideMessage(string messageFilePath, string inputCoverFilePath, string outputCoverFilePath, int hidingStartByte)
        {
            base.outputFilePath = outputCoverFilePath;
            using (FileStream messageStream = StreamMaker.MakeInputStream(messageFilePath))
                using (FileStream inputCoverStream = StreamMaker.MakeInputStream(inputCoverFilePath))
                    using (FileStream outputCoverStream = StreamMaker.MakeOutputStream(base.outputFilePath))
                    {
                        if (hidingStartByte > inputCoverStream.Length - messageStream.Length * 8)
                        {
                            throw new ArgumentException("Hiding start byte or message file is too large to hold all the message inside cover file.");
                        }

                        int coverCharacter;

                        // Move to hiding start byte.
                        for (int i = 0; i < hidingStartByte; i++)
                        {
                            coverCharacter = inputCoverStream.ReadByte();
                            outputCoverStream.WriteByte((byte)coverCharacter);
                            SetProgressPercentageAndCheckCancellation(inputCoverStream);
                        }

                        int  messageCharacter, messageCharacterBit;
                        bool endOfMessageFile = false;
                        while (!endOfMessageFile)
                        {
                            // End of file shouldn't appear now, because of checking hidding start byte at the beginning of the method.
                            messageCharacter = messageStream.ReadByte();
                            endOfMessageFile = messageCharacter == -1;
                            if (endOfMessageFile)
                            {
                                messageCharacter = endOfMessageCharacter;
                            }
                            for (int i = 0; i < 8; i++)
                            {
                                messageCharacterBit = messageCharacter & 1; // Obtain current message character bit.
                                coverCharacter      = inputCoverStream.ReadByte();
                                coverCharacter     &= 254;                  // Reset least significant bit from the cover file character.
                                coverCharacter     |= messageCharacterBit;  // Change least significant bit value of the cover file character to the value of the current message character bit.
                                // coverCharacter &= (254 | messageCharacterBit) for short
                                outputCoverStream.WriteByte((byte)coverCharacter);
                                messageCharacter = messageCharacter >> 1;
                            }
                            SetProgressPercentageAndCheckCancellation(inputCoverStream);
                        }

                        // Rewrite rest of the cover file.
                        bool endOfCoverFile = false;
                        while (!endOfCoverFile)
                        {
                            coverCharacter = inputCoverStream.ReadByte();
                            endOfCoverFile = coverCharacter == -1;
                            if (!endOfCoverFile)
                            {
                                outputCoverStream.WriteByte((byte)coverCharacter);
                                SetProgressPercentageAndCheckCancellation(inputCoverStream);
                            }
                            else
                            {
                                NotifyFinishedWork();
                            }
                        }
                    }
        }