Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            string plaintext = "0110000101101100011000010110110101100001011010110110111101110100";
            string key1      = "0000000100000010000000110000010000000101000001100000011100001000";

            // MainMenu();
            int[] text = new int[64];
            for (int i = 0; i < 64; i++)
            {
                text[i] = (int)(plaintext[i] - '0');
            }

            int[] key = new int[64];
            for (int i = 0; i < 64; i++)
            {
                key[i] = (int)(key1[i] - '0');
            }
            DES2 ag = new DES2();

            ag.assignPlainTextAndKey(text, key);
            ag.startEncryption();
            int[] testkey = ag.getEnrypction();

            for (int i = 0; i < 64; i++)
            {
                Console.Write(testkey[i]);
            }

            Console.ReadKey();
            MainMenu();
        }
Ejemplo n.º 2
0
        public static string EncodeString(string input, int[] key)
        {
            string finalInput  = input;
            string finalOutput = "";
            int    howManyAdd  = 0;

            if (input.Length % 8 != 0)
            {
                howManyAdd = 8 - (input.Length % 8);
            }

            for (int i = 0; i < howManyAdd; i++)
            {
                finalInput = finalInput + " ";
            }

            int currentIndex = 0;

            for (currentIndex = 0; currentIndex < finalInput.Length; currentIndex = currentIndex + 8)
            {
                byte[] currentSubString = StringToByteArray(finalInput.Substring(currentIndex, 8));

                int[] textToEncrypt = new int[64];

                int indxOfInput = 0;
                for (int i = 0; i < 8; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        int valueOfBit = ((currentSubString[i] & (1 << j)) == 0) ? 0 : 1;

                        textToEncrypt[indxOfInput] = valueOfBit;
                        indxOfInput++;
                    }
                }

                DES2 algorytmDes = new DES2();
                algorytmDes.assignPlainTextAndKey(textToEncrypt, key);
                algorytmDes.startEncryption();
                int[] outputEncrypted = algorytmDes.getEnrypction();

                indxOfInput = 0;
                for (int j = 0; j < 8; j++)
                {
                    byte outByte = 0;

                    for (int i = 0; i < 8; i++)
                    {
                        if (outputEncrypted[indxOfInput] == 1)
                        {
                            outByte = (byte)(outByte | (1 << i));
                        }
                        indxOfInput++;
                    }

                    //bw.Write(outByte);
                    finalOutput = finalOutput + Convert.ToChar(outByte);
                }
            }
            return(finalOutput);
        }
Ejemplo n.º 3
0
        public static void EncryptFromBinFileToBinFile(string inputFileName, string outputFileName, int[] _key, int howManyBytes = -1)
        {
            using (BinaryWriter bw = new BinaryWriter(File.Open(outputFileName, FileMode.Create)))
                using (BinaryReader br = new BinaryReader(File.Open(inputFileName, FileMode.Open)))
                {
                    int   pos = 0, i, j;
                    int   length        = (int)br.BaseStream.Length;
                    int   outputLength  = (int)br.BaseStream.Length;
                    int[] textToEncrypt = new int[64];
                    byte  v;
                    bool  areFullBlocks        = true;
                    int   numOfAdditionalBytes = 0;



                    while (pos < length)
                    {
                        int indxOfInput = 0;
                        for (j = 0; j < 8; j++) //8 bajtów bo 8 bajtów po 8 bitów to 8*8=64 bity
                        {
                            if (pos < length)
                            {
                                v = br.ReadByte();

                                for (i = 7; i >= 0; i--)
                                {
                                    int valueOfBit = ((v & (1 << i)) == 0) ? 0 : 1;

                                    textToEncrypt[indxOfInput] = valueOfBit;
                                    indxOfInput++;
                                }
                            }
                            else //w przypadku gdy skoczyl sie plik ale mamy zaczety blok - dopelnij blok zerami
                            {
                                areFullBlocks = false;
                                for (i = 7; i >= 0; i--)
                                {
                                    int valueOfBit = 0;

                                    textToEncrypt[indxOfInput] = valueOfBit;
                                    indxOfInput++;
                                }
                                numOfAdditionalBytes++;
                            }
                            pos += sizeof(byte);
                        }

                        if (!areFullBlocks)
                        {
                            Console.WriteLine("Twój plik wejściowy nie miał pełnych 64-bitowych bloków - dopełniono " + numOfAdditionalBytes + " bajtami");
                        }

                        DES2 algorytmDes = new DES2();
                        algorytmDes.assignPlainTextAndKey(textToEncrypt, _key);
                        algorytmDes.startEncryption();
                        int[] outputEncrypted = algorytmDes.getEnrypction();

                        indxOfInput = 0;
                        for (j = 0; j < 8; j++)
                        {
                            byte outByte = 0;

                            for (i = 7; i >= 0; i--)
                            {
                                if (outputEncrypted[indxOfInput] == 1)
                                {
                                    outByte = (byte)(outByte | (1 << i));
                                }
                                indxOfInput++;
                            }

                            bw.Write(outByte);
                        }
                    }
                }
            Console.WriteLine("Zapisano wyniki w pliku \"{0}\".", outputFileName);
        }