Exemplo n.º 1
0
        /// <summary>
        /// Task 2. RSA Encryption and decryption
        /// </summary>
        /// <param name="keys">keys</param>
        /// <param name="fileName"></param>
        /// <param name="keyLength">Key length</param>
        private static void RsaCrypt(KeysHolder keys, string fileName, int keyLength)
        {
            var plainText     = FileWorker.GetBinaryFile(fileName);
            var encodedText   = new List <short>();
            var decryptedText = new List <short>();
            var rsaEncoder    = new RsaEncoder();

            // encrypting
            encodedText = Encode(keys.publicKey, keys.cypherModule, plainText, rsaEncoder, keyLength / 4, keyLength, false);
            // saving encrypted message
            FileWorker.CreateTextFileForBinaryNumber(encodedText, "crypted.txt");
            Console.WriteLine(String.Format("Encryption has finished. Check crypted.txt"));

            // decrypting
            decryptedText = Encode(keys.privateKey, keys.cypherModule, encodedText, rsaEncoder, keyLength, keyLength / 4, true);
            // saving decrypted message
            FileWorker.GetNEWFile(decryptedText, "decrypted_" + fileName);
            Console.WriteLine(String.Format("DEcryption has finished. Check decrypted{0}", fileName));
            Console.WriteLine("-------------------------------------------------------------");
        }
Exemplo n.º 2
0
        /// <summary>
        /// RSA Encryption and decryption. Initial message will be separated on blocks with length toTakeLength
        /// Encrypted (decrypted message) are written as blocks with length toPutLength
        /// </summary>
        /// <param name="key">key (either public or private)</param>
        /// <param name="cypherModule">cypher module</param>
        /// <param name="text">message</param>
        /// <param name="rsaEncoder">RSA instance</param>
        /// <param name="toTakeLength"></param>
        /// <param name="toPutLength"></param>
        /// <returns>binary number placed in list int</returns>
        private static List <short> Encode(BigInteger key, BigInteger cypherModule, List <short> text, RsaEncoder rsaEncoder, int toTakeLength, int toPutLength, bool isDecryption)
        {
            var toReturn = new List <short>();

            while (text.Count != 0)
            {
                // separate message on blocks
                var oneBlock        = text.Take(toTakeLength);
                var oneBlockNumeric = MathHelper.GetNumberFromList(oneBlock.ToList());

                // encrypt(decrypt) one block
                var encodedNumber      = rsaEncoder.Encode(oneBlockNumeric, key, cypherModule);
                var encodedBlockBinary = MathHelper.GetBinaryFromNumber(encodedNumber);
                if (encodedBlockBinary[0] == 0)
                {
                    encodedBlockBinary.RemoveAt(0); // delete first bit responsible for number sign
                }

                // in case decryption there is no need to fill last block with insignificant zeros
                if (!(isDecryption && text.Count == toTakeLength))
                {
                    encodedBlockBinary = Helper.BringingToNeededBinaryFormat(encodedBlockBinary, toPutLength);
                }

                toReturn.AddRange(encodedBlockBinary);

                // remove block which has already been en(-de)crypted
                text.RemoveRange(0, oneBlock.Count());
            }

            return(toReturn);
        }