コード例 #1
0
        public static void Encrypt(byte[] source)
        {
            if (!GetPublicKey())
            {
                return;
            }

            StringBuilder bits = new StringBuilder(source.ToBitsString());

            StringBuilder result = new StringBuilder();

            int rsaBlockSize = blockSize;

            int addBitsCount = rsaBlockSize - (bits.Length % rsaBlockSize);

            bits.Append(Extensions.GetRandomString(addBitsCount));

            string add = Convert.ToString(addBitsCount, 2);

            bits.Append(add.PadLeftRandom(blockSize));

            for (int i = 0; i < bits.Length; i += rsaBlockSize)
            {
                string blockMessage = bits.ToString(i, rsaBlockSize);
                result.Append(EncryptBlock(blockMessage));
            }

            byte[] encrypted = result.ToString().ToBytes().ToArray();

            CustomFile.WriteAllBytes(encrypted, "C:\\Users\\vlady\\Desktop\\result.txt");
        }
コード例 #2
0
        private static bool GetPublicKey()
        {
            byte[] key = CustomFile.OpenFile("открытого ключа");

            if (key.Length != Generate.KEY_LENGT_BYTE / 2 + 1)
            {
                Console.WriteLine("Ключ поврежден!");
                return(false);
            }

            n = new BigInteger(key.SubArray(0, Generate.KEY_LENGT_BYTE / 2 + 1));

            blockSize = (int)Math.Floor(BigInteger.Log(n, 2));
            Console.WriteLine("Выполнение...");
            return(true);
        }
コード例 #3
0
        public static void Main()
        {
            Stopwatch watch;

            while (true)
            {
                Console.Clear();
                Console.WriteLine("Алгоритм RSA\n" +
                                  "Выберите режим работы:\n" +
                                  "1 - Шифрование\n" +
                                  "2 - Дешифрование\n" +
                                  "3 - Генерация ключей\n" +
                                  "4 - Выход");
                switch (Console.ReadKey(true).KeyChar)
                {
                case '1':
                    var fileNameEncrypt = CustomFile.OpenFile("для шифрования");
                    watch = Stopwatch.StartNew();
                    Rsa.Encrypt(fileNameEncrypt);
                    ShowRunTime(watch.Elapsed);
                    Exit();
                    return;

                case '2':
                    var fileNameDecrypt = CustomFile.OpenFile("для расшифрования");
                    watch = Stopwatch.StartNew();
                    Rsa.Decrypt(fileNameDecrypt);
                    ShowRunTime(watch.Elapsed);
                    Exit();
                    return;

                case '3':
                    Console.Clear();
                    watch = Stopwatch.StartNew();
                    Rsa.Generate.GenerateKey();
                    ShowRunTime(watch.Elapsed);
                    Exit();
                    break;

                case '4':
                    return;

                default:
                    break;
                }
            }
        }
コード例 #4
0
        public static void Decrypt(byte[] source)
        {
            if (source.Length <= TRASH_BLOCK)
            {
                ShowErrorMessage();
                return;
            }

            if (!GetPrivateKey())
            {
                return;
            }

            StringBuilder result = new StringBuilder();

            int rsaBlockSize = Generate.KEY_LENGT_BITS;

            StringBuilder bits = new StringBuilder(source.ToBitsString());

            if (bits.Length % rsaBlockSize != 0)
            {
                ShowErrorMessage();
                return;
            }

            for (int i = 0; i < bits.Length; i += rsaBlockSize)
            {
                string blockMessage = bits.ToString(i, rsaBlockSize);
                result.Append(DecryptBlock(blockMessage));
            }

            int tempSize     = (int)(result.ToString().Substring(result.Length - 16).ToBigInteger());
            int addBitsCount = tempSize + blockSize;

            string resultString = result.ToString().Substring(0, result.Length - addBitsCount);

            CustomFile.WriteAllBytes(resultString.ToBytes());
        }