Gremlin92
@Gremlin92
Целеустремленный

Расшифровка RSA?

RSA алгоритм
n = 517815623413379     e = 12371
ив = 12788138155374656626484111508435839351545312207685619856968320863251613810

ив-шифр текст, p*q=n,нужен алгоритм факторизации,еще нужен тип данных как в си шарпе BigInteger, скорее всего писать на си шарпе,phi=(q-1)(p-1), d-обратный элемент по модулю phi, d*e%phi, далее разбиваем ив на блоки по 15 символов
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace RSA
{
    class Program
    {
        static String codes2string(String str)
        {
            StringBuilder text = new StringBuilder();
            Byte[] bytes = new Byte[str.Length / 2];
            for(int i=0;i<str.Length/2;i++)
            {
                String code = str.Substring(2 * i, 2);
                bytes[i] = Byte.Parse(code);
            }
            return ASCIIEncoding.ASCII.GetString(bytes);
        }
        static void Main(string[] args)
        {
            BigInteger e = 12371;
            //BigInteger e = 12341;
            BigInteger n = 517815623413379;
            //BigInteger n = 565570077646207;
            //Факторизация n: в гугле Wolphram alpha factor(n)
            BigInteger p = 2432363;
            //BigInteger p = 1546379;
            BigInteger q = 212885833;
            //BigInteger q = 365738333;
            BigInteger phi = (p - 1) * (q - 1);
            Console.WriteLine("phi");
            Console.WriteLine("{0}", phi);
            
            //Обратный элемент d в кольце e,phi https://planetcalc.ru/3311/
            BigInteger d = 81621537934331;
            //BigInteger d = 143672396238821;
            Console.WriteLine("{0}", (d * e) % phi);//Здесь должна выводится единица

            String[] c_text = "127881381553746 566264841115084 358393515453122 076856198569683 20863251613810".Split(' ');
            //String[] c_text = "277140870674302 260217431481485 329310844916399 448964498705119".Split(' ');
            StringBuilder text = new StringBuilder();
            StringBuilder c_text_ = new StringBuilder();

            foreach(String c_str in c_text)
            {
                BigInteger c_msg = UInt64.Parse(c_str);
                BigInteger msg = BigInteger.ModPow(c_msg, d, n);
                text.Append(msg.ToString());

                BigInteger c_msg_ = BigInteger.ModPow(msg, e, n);
                c_text_.Append(c_msg_.ToString());
            }
            Console.WriteLine(c_text_);

            Console.WriteLine(codes2string(text.ToString()));
            
            Console.ReadKey();
        }
    }
}

Видимо дело в разбиении шифр текста на слова не точно,нужно по другому разбить
Для точности алгоритма продемонстрирую другой вариант исходных данных
n = 565570077646207     e = 12341
ив = 277140870674302260217431481485329310844916399448964498705119

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace RSA
{
    class Program
    {
        static String codes2string(String str)
        {
            StringBuilder text = new StringBuilder();
            Byte[] bytes = new Byte[str.Length / 2];
            for(int i=0;i<str.Length/2;i++)
            {
                String code = str.Substring(2 * i, 2);
                bytes[i] = Byte.Parse(code);
            }
            return ASCIIEncoding.ASCII.GetString(bytes);
        }
        static void Main(string[] args)
        {
            //BigInteger e = 12371;
            BigInteger e = 12341;
            //BigInteger n = 517815623413379;
            BigInteger n = 565570077646207;
            //Факторизация n: в гугле Wolphram alpha factor(n)
            //BigInteger p = 22432363;
            BigInteger p = 1546379;
            //BigInteger q = 212885833;
            BigInteger q = 365738333;
            BigInteger phi = (p - 1) * (q - 1);
            Console.WriteLine("phi");
            Console.WriteLine("{0}", phi);
            
            //Обратный элемент d в кольце e,phi https://planetcalc.ru/3311/
            //BigInteger d = 1433315859233483;
            BigInteger d =143672396238821;
            Console.WriteLine("{0}", (d * e) % phi);//Здесь должна выводится единица

            //String[] c_text = "127881381553746 566264841115084 358393515453122 076856198569683 20863251613810".Split(' ');
            String[] c_text = "277140870674302 260217431481485 329310844916399 448964498705119".Split(' ');
            StringBuilder text = new StringBuilder();
            StringBuilder c_text_ = new StringBuilder();

            foreach(String c_str in c_text)
            {
                BigInteger c_msg = UInt64.Parse(c_str);
                BigInteger msg = BigInteger.ModPow(c_msg, d, n);
                text.Append(msg.ToString());

                BigInteger c_msg_ = BigInteger.ModPow(msg, e, n);
                c_text_.Append(c_msg_.ToString());
            }
            Console.WriteLine(c_text_);

            Console.WriteLine(codes2string(text.ToString()));
            
            Console.ReadKey();
        }
    }
}
  • Вопрос задан
  • 386 просмотров
Решения вопроса 1
Gremlin92
@Gremlin92 Автор вопроса
Целеустремленный
Да все дело было в переменной c_text ,а точнее в ее разбиении на подстроки пробелами вот код расшифровывающий все как надо
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace RSA
{
    class Program
    {
        static String codes2string(String str)
        {
            StringBuilder text = new StringBuilder();
            Byte[] bytes = new Byte[str.Length / 2];
            for(int i=0;i<str.Length/2;i++)
            {
                String code = str.Substring(2 * i, 2);
                bytes[i] = Byte.Parse(code);
            }
            return ASCIIEncoding.ASCII.GetString(bytes);
        }
        static void Main(string[] args)
        {
            BigInteger e = 12371;
            //BigInteger e = 12341;
            BigInteger n = 517815623413379;
            //BigInteger n = 565570077646207;
            //Факторизация n: в гугле Wolphram alpha factor(n)
            BigInteger p = 2432363;
            //BigInteger p = 1546379;
            BigInteger q = 212885833;
            //BigInteger q = 365738333;
            BigInteger phi = (p - 1) * (q - 1);
            Console.WriteLine("phi");
            Console.WriteLine("{0}", phi);
            
            //Обратный элемент d в кольце e,phi https://planetcalc.ru/3311/
            BigInteger d = 81621537934331;
            //BigInteger d = 143672396238821;
            Console.WriteLine("{0}", (d * e) % phi);//Здесь должна выводится единица

            String[] c_text = "127881381553746 56626484111508 435839351545312 207685619856968 320863251613810".Split(' ');
            //String[] c_text = "277140870674302 260217431481485 329310844916399 448964498705119".Split(' ');
            StringBuilder text = new StringBuilder();
            StringBuilder c_text_ = new StringBuilder();

            foreach(String c_str in c_text)
            {
                BigInteger c_msg = UInt64.Parse(c_str);
                BigInteger msg = BigInteger.ModPow(c_msg, d, n);
                text.Append(msg.ToString());

                BigInteger c_msg_ = BigInteger.ModPow(msg, e, n);
                c_text_.Append(c_msg_.ToString());
            }
            Console.WriteLine(c_text_);

            Console.WriteLine(codes2string(text.ToString()));
            
            Console.ReadKey();
        }
    }
}
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
shai_hulud
@shai_hulud
дефолтный CSP RSA, RSACng, RSAEngine не подошли? В последнем есть исходники реализации.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы