Esempio n. 1
0
        public static void Write(PNG_signs signs, Queue <Chunk> chunks, string fileName)
        {
            string fileDir  = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
            string filePath = Path.Combine(fileDir, fileName);

            BinaryWriter NewPicture = new BinaryWriter(File.OpenWrite(filePath));

            NewPicture.Write(signs.bytePNG_sign);
            while (chunks.Count != 0)
            {
                chunks.Dequeue().Write(NewPicture);
            }
            NewPicture.Close();
        }
Esempio n. 2
0
        public static bool Execute(PNG_signs signs, Queue <Chunk> chunks, Queue <Chunk> chunksToWrite, List <Chunk> chunksToEncrypt, string fileName, string newFileName)
        {
            string schoice;
            int    choice = 0;

            do
            {
                ProgramMenu();
                Console.Write("Choose what to do: ");
                schoice = (Console.ReadLine());
                Console.WriteLine("");
                Int32.TryParse(schoice, out choice);
            } while (choice < 1 || choice > 6);

            switch (choice)
            {
            case 1:
                Display(chunks);
                break;

            case 2:
                Write(signs, chunksToWrite, newFileName);
                break;

            case 3:
                DisplayImage(fileName);
                break;

            case 4:
                MakeFFT(fileName);
                break;

            case 5:
                EncryptDecrypt(chunksToEncrypt, signs);
                break;

            case 6:
                return(true);

            default:
                Console.WriteLine("Undefined operation\n");
                break;
            }
            return(false);
        }
Esempio n. 3
0
        public static void Main(string[] args)
        {
            bool          end             = false;
            PNG_signs     signs           = new PNG_signs();
            Queue <Chunk> chunks          = new Queue <Chunk>();
            Queue <Chunk> chunksToWrite   = new Queue <Chunk>();
            List <Chunk>  chunksToEncrypt = new List <Chunk>();

            string fileName    = ChoosePicture();
            string newFileName = "data\\test.png";

            Read(signs, chunks, chunksToWrite, chunksToEncrypt, fileName);

            while (!end)
            {
                end = Execute(signs, chunks, chunksToWrite, chunksToEncrypt, fileName, newFileName);
            }
        }
Esempio n. 4
0
        public static void Read(PNG_signs signs, Queue <Chunk> chunks, Queue <Chunk> chunksToWrite, List <Chunk> chunksToEncrypt, string fileName)
        {
            string fileDir      = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
            string filePath     = Path.Combine(fileDir, fileName);
            int    idatQuantity = 0;

            BinaryReader Picture = new BinaryReader(File.OpenRead(filePath));

            if (!(signs.IsPNG(Picture)))
            {
                Console.WriteLine("Wczytany plik nie jest obrazem PNG");
            }

            bool endOfFile = true;

            do
            {
                Chunk chunk = new Chunk();
                chunk.Read(Picture);

                if (chunk.sign == "IHDR")
                {
                    IHDR ihdr = new IHDR(chunk);
                    chunks.Enqueue(ihdr);
                    chunksToWrite.Enqueue(ihdr);
                    chunksToEncrypt.Add(ihdr);
                }
                else if (chunk.sign == "PLTE")
                {
                    PLTE plte = new PLTE(chunk);
                    chunks.Enqueue(plte);
                    chunksToWrite.Enqueue(plte);
                    chunksToEncrypt.Add(plte);
                }
                else if (chunk.sign == "IDAT")
                {
                    idatQuantity++;
                    IDAT idat = new IDAT(chunk, idatQuantity);
                    chunks.Enqueue(idat);
                    chunksToWrite.Enqueue(idat);
                    chunksToEncrypt.Add(idat);
                }
                else if (chunk.sign == "IEND")
                {
                    IEND iend = new IEND(chunk);
                    chunks.Enqueue(iend);
                    chunksToWrite.Enqueue(iend);
                    chunksToEncrypt.Add(iend);
                    endOfFile = false;
                }
                else if (chunk.sign == "gAMA")
                {
                    gAMA gama = new gAMA(chunk);
                    chunks.Enqueue(gama);
                    chunksToEncrypt.Add(gama);
                }
                else if (chunk.sign == "cHRM")
                {
                    cHRM chrm = new cHRM(chunk);
                    chunks.Enqueue(chrm);
                    chunksToEncrypt.Add(chrm);
                }
                else if (chunk.sign == "zTXt")
                {
                    zTXt ztxt = new zTXt(chunk);
                    chunks.Enqueue(ztxt);
                    chunksToEncrypt.Add(ztxt);
                }
                else if (chunk.sign == "tEXt")
                {
                    tEXt text = new tEXt(chunk);
                    chunks.Enqueue(text);
                    chunksToEncrypt.Add(text);
                }
                else
                {
                    chunks.Enqueue(chunk);
                    chunksToEncrypt.Add(chunk);
                }
            } while (endOfFile);

            Picture.Close();
        }
Esempio n. 5
0
        ////////////////////////////////////////////////////////////////////////////////
        /// Cryptography                                                             ///
        ////////////////////////////////////////////////////////////////////////////////

        public static void EncryptDecrypt(List <Chunk> chunksToEncrypt, PNG_signs signs)
        {
            int        blockSize = 64;
            BigInteger n, e, d;

            string fileDir      = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
            string filePath     = Path.Combine(fileDir, "data\\encrypted.png");
            string filePath2    = Path.Combine(fileDir, "data\\decrypted.png");
            string ECBFilePath  = Path.Combine(fileDir, "data\\encryptedECB.png");
            string ECBFilePath2 = Path.Combine(fileDir, "data\\decryptedECB.png");
            string CBCFilePath  = Path.Combine(fileDir, "data\\encryptedCBC.png");
            string CBCFilePath2 = Path.Combine(fileDir, "data\\decryptedCBC.png");

            byte[] encryptedData;
            byte[] decryptedData;
            byte[] encryptedECBData;
            byte[] decryptedECBData;
            byte[] encryptedCBCData;
            byte[] decryptedCBCData;

            BinaryWriter EncryptedPicture    = new BinaryWriter(File.OpenWrite(filePath));
            BinaryWriter DecryptedPicture    = new BinaryWriter(File.OpenWrite(filePath2));
            BinaryWriter ECBEncryptedPicture = new BinaryWriter(File.OpenWrite(ECBFilePath));
            BinaryWriter ECBDecryptedPicture = new BinaryWriter(File.OpenWrite(ECBFilePath2));
            BinaryWriter CBCEncryptedPicture = new BinaryWriter(File.OpenWrite(CBCFilePath));
            BinaryWriter CBCDecryptedPicture = new BinaryWriter(File.OpenWrite(CBCFilePath2));

            EncryptedPicture.Write(signs.bytePNG_sign);
            DecryptedPicture.Write(signs.bytePNG_sign);
            ECBEncryptedPicture.Write(signs.bytePNG_sign);
            ECBDecryptedPicture.Write(signs.bytePNG_sign);
            CBCEncryptedPicture.Write(signs.bytePNG_sign);
            CBCDecryptedPicture.Write(signs.bytePNG_sign);

            foreach (Chunk c in chunksToEncrypt)
            {
                if (c.sign == "IDAT")
                {
                    List <Byte[]> divided      = new List <Byte[]>();
                    List <Byte[]> encrypted    = new List <Byte[]>();
                    List <Byte[]> decrypted    = new List <Byte[]>();
                    List <Byte[]> encryptedECB = new List <Byte[]>();
                    List <Byte[]> decryptedECB = new List <Byte[]>();
                    List <Byte[]> encryptedCBC = new List <Byte[]>();
                    List <Byte[]> decryptedCBC = new List <Byte[]>();
                    byte[]        data         = c.ReturnData();

                    for (int i = 0; i < data.Length; i += blockSize)
                    {
                        List <byte> bytes = new List <byte>();
                        for (int j = i; j < i + blockSize && j < data.Length; j++)
                        {
                            bytes.Add(data[j]);
                        }
                        divided.Add(bytes.ToArray());
                    }

                    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                    {
                        n = new BigInteger(RSA.ExportParameters(false).Modulus.Reverse().ToArray(), true);
                        e = new BigInteger(RSA.ExportParameters(false).Exponent.Reverse().ToArray(), true);
                        d = new BigInteger(RSA.ExportParameters(true).D.Reverse().ToArray(), true);
                        foreach (byte[] b in divided)
                        {
                            byte[] encryptedBlock = RSAEncrypt(b, RSA.ExportParameters(false), false);
                            encrypted.Add(encryptedBlock);
                            decrypted.Add(RSADecrypt(encryptedBlock, RSA.ExportParameters(true), false));
                        }
                    }
                    encryptedData = encrypted.SelectMany(a => a).ToArray();
                    decryptedData = decrypted.SelectMany(a => a).ToArray();

                    byte[] compressedData = Compress(encryptedData, ((IDAT)c).compression.FLEVEL);

                    EncryptedPicture.Write(BitConverter.GetBytes(compressedData.Length));
                    EncryptedPicture.Write(c.byteSign);
                    EncryptedPicture.Write(compressedData);
                    EncryptedPicture.Write(c.byteCheckSum);

                    byte[] compressedDataDe = Compress(decryptedData, ((IDAT)c).compression.FLEVEL);

                    DecryptedPicture.Write(BitConverter.GetBytes(compressedDataDe.Length));
                    DecryptedPicture.Write(c.byteSign);
                    DecryptedPicture.Write(compressedDataDe);
                    DecryptedPicture.Write(c.byteCheckSum);

                    //ECB

                    foreach (byte[] b  in divided)
                    {
                        bool       test    = false;
                        BigInteger intData = new BigInteger(b);
                        if (intData.Sign == -1)
                        {
                            test = true;
                        }
                        intData = new BigInteger(b, true);
                        BigInteger intEncrypted = BigInteger.ModPow(intData, e, n);
                        BigInteger intDecrypted = BigInteger.ModPow(intEncrypted, d, n);
                        encryptedECB.Add(intEncrypted.ToByteArray());
                        byte[] temp = new byte[blockSize];
                        byte[] temp2;
                        if (test)
                        {
                            byte[] temp3 = intDecrypted.ToByteArray();
                            temp2 = new byte[temp3.Length - 1];
                            for (int i = 0; i < temp3.Length - 1; i++)
                            {
                                temp2[i] = temp3[i];
                            }
                        }
                        else
                        {
                            temp2 = intDecrypted.ToByteArray();
                        }

                        int l = b.Length - temp2.Length;

                        if (l != 0)
                        {
                            foreach (byte by in temp2)
                            {
                                temp[l - 1] = by;
                                l++;
                            }
                            decryptedECB.Add(temp);
                        }
                        else
                        {
                            decryptedECB.Add(temp2);
                        }
                    }
                    encryptedECBData = encryptedECB.SelectMany(a => a).ToArray();
                    decryptedECBData = decryptedECB.SelectMany(a => a).ToArray();

                    byte[] compressedDataECB = Compress(encryptedECBData, ((IDAT)c).compression.FLEVEL);

                    ECBEncryptedPicture.Write(BitConverter.GetBytes(compressedDataECB.Length));
                    ECBEncryptedPicture.Write(c.byteSign);
                    ECBEncryptedPicture.Write(compressedDataECB);
                    ECBEncryptedPicture.Write(c.byteCheckSum);

                    byte[] compressedDataECBde = Compress(decryptedECBData, ((IDAT)c).compression.FLEVEL);

                    ECBDecryptedPicture.Write(BitConverter.GetBytes(compressedDataECBde.Length));
                    ECBDecryptedPicture.Write(c.byteSign);
                    ECBDecryptedPicture.Write(compressedDataECBde);
                    ECBDecryptedPicture.Write(c.byteCheckSum);

                    //CBC

                    Random rnd = new Random();
                    byte[] initializationVector = new byte[blockSize];
                    rnd.NextBytes(initializationVector);
                    byte[] vector = new byte[blockSize];

                    for (int j = 0; j < divided.Count(); j++)
                    {
                        byte[] b     = divided[j];
                        byte[] xored = new byte[b.Length];
                        if (j == 0)
                        {
                            vector = initializationVector;
                        }

                        for (int k = 0; k < b.Length; k++)
                        {
                            xored[k] = (byte)(b[k] ^ vector[k]);
                        }

                        bool       test    = false;
                        BigInteger intData = new BigInteger(xored);
                        if (intData.Sign == -1)
                        {
                            test = true;
                        }
                        intData = new BigInteger(xored, true);
                        BigInteger intEncrypted = BigInteger.ModPow(intData, e, n);
                        BigInteger intDecrypted = BigInteger.ModPow(intEncrypted, d, n);

                        byte[] temp = new byte[blockSize];
                        byte[] temp2;
                        if (test)
                        {
                            byte[] temp3 = intDecrypted.ToByteArray();
                            temp2 = new byte[temp3.Length - 1];
                            for (int i = 0; i < temp3.Length - 1; i++)
                            {
                                temp2[i] = temp3[i];
                            }
                        }
                        else
                        {
                            temp2 = intDecrypted.ToByteArray();
                        }

                        int l = b.Length - temp2.Length;

                        if (l != 0)
                        {
                            foreach (byte by in temp2)
                            {
                                temp[l - 1] = by;
                                l++;
                            }
                            for (int k = 0; k < b.Length; k++)
                            {
                                temp[k] = (byte)(temp[k] ^ vector[k]);
                            }
                            decryptedCBC.Add(temp);
                        }
                        else
                        {
                            for (int k = 0; k < b.Length; k++)
                            {
                                temp2[k] = (byte)(temp2[k] ^ vector[k]);
                            }
                            decryptedCBC.Add(temp2);
                        }
                        vector = intEncrypted.ToByteArray();
                        encryptedCBC.Add(vector);
                    }
                    encryptedCBCData = encryptedCBC.SelectMany(a => a).ToArray();
                    decryptedCBCData = decryptedCBC.SelectMany(a => a).ToArray();

                    byte[] compressedDataCBC = Compress(encryptedCBCData, ((IDAT)c).compression.FLEVEL);

                    CBCEncryptedPicture.Write(BitConverter.GetBytes(compressedDataCBC.Length));
                    CBCEncryptedPicture.Write(c.byteSign);
                    CBCEncryptedPicture.Write(compressedDataCBC);
                    CBCEncryptedPicture.Write(c.byteCheckSum);

                    byte[] compressedDataCBCde = Compress(decryptedCBCData, ((IDAT)c).compression.FLEVEL);

                    CBCDecryptedPicture.Write(BitConverter.GetBytes(compressedDataCBCde.Length));
                    CBCDecryptedPicture.Write(c.byteSign);
                    CBCDecryptedPicture.Write(compressedDataCBCde);
                    CBCDecryptedPicture.Write(c.byteCheckSum);
                }
                else
                {
                    c.Write(EncryptedPicture);
                    c.Write(DecryptedPicture);
                    c.Write(ECBEncryptedPicture);
                    c.Write(ECBDecryptedPicture);
                    c.Write(CBCEncryptedPicture);
                    c.Write(CBCDecryptedPicture);
                }
            }
            EncryptedPicture.Close();
            DecryptedPicture.Close();
            ECBEncryptedPicture.Close();
            ECBDecryptedPicture.Close();
            CBCEncryptedPicture.Close();
            CBCDecryptedPicture.Close();
        }