Esempio n. 1
0
        public static string DecodeString(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.assignCipherTextAndKey(textToEncrypt, key);
                algorytmDes.startDecryption();
                int[] outputEncrypted = algorytmDes.getDecryption();


                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);
        }
Esempio n. 2
0
        public static void DecryptFromBinFileToBinFile(string inputFileName, string outputFileName, int[] _key, int howManyBytesToRemove = -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[] textToDecrypt = 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;

                                    textToDecrypt[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;

                                    textToDecrypt[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.assignCipherTextAndKey(textToDecrypt, _key);
                        algorytmDes.startDecryption();
                        int[] outputDecrypted = algorytmDes.getDecryption();


                        if (pos < length || howManyBytesToRemove <= 0)
                        {
                            indxOfInput = 0;
                            for (j = 0; j < 8; j++)
                            {
                                byte outByte = 0;

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

                                bw.Write(outByte);
                            }
                        }
                        else if (howManyBytesToRemove > 0)
                        {
                            indxOfInput = 0;
                            for (j = 0; j < 8 - howManyBytesToRemove; j++)
                            {
                                byte outByte = 0;

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

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