E
E
Evgeny Petryaev2019-03-21 16:39:17
C++ / C#
Evgeny Petryaev, 2019-03-21 16:39:17

RSA decryption?

RSA algorithm

n = 517815623413379     e = 12371
ив = 12788138155374656626484111508435839351545312207685619856968320863251613810

iv-cipher text, p*q=n, we need a factorization algorithm, we also need a data type like in BigInteger C Sharp, most likely to write in C Sharp, phi=(q-1)(p-1), d-inverse element by module phi, d*e%phi, then we break willows into blocks of 15 characters
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();
        }
    }
}

Apparently the point is in splitting the cipher text into words is not accurate, you need to split it differently.
For the accuracy of the algorithm, I will demonstrate another version of the initial data
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();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Petryaev, 2019-03-21
@Gremlin92

Yes, the whole thing was in the c_text variable, or rather, in its division into substrings with spaces, here is the code that decrypts everything as it should

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();
        }
    }
}

S
shai_hulud, 2019-03-21
@shai_hulud

default CSP RSA , RSACng , RSAEngine didn't fit? The latter contains the source code for the implementation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question