Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)
(시리즈 글이 10개 있습니다.)
.NET Framework: 292. RSACryptoServiceProvider의 공개키와 개인키 구분
; https://www.sysnet.pe.kr/2/0/1218

.NET Framework: 327. RSAParameters와 System.Numerics.BigInteger 이야기
; https://www.sysnet.pe.kr/2/0/1295

.NET Framework: 329. C# - Rabin-Miller 소수 생성방법을 이용하여 RSACryptoServiceProvider 의 개인키를 직접 채워보자
; https://www.sysnet.pe.kr/2/0/1300

.NET Framework: 356. (공개키를 담은) 자바의 key 파일을 닷넷의 RSACryptoServiceProvider에서 사용하는 방법
; https://www.sysnet.pe.kr/2/0/1401

.NET Framework: 383. RSAParameters의 ToXmlString과 ExportParameters의 결과 비교
; https://www.sysnet.pe.kr/2/0/1491

.NET Framework: 565. C# - Rabin-Miller 소수 생성 방법을 이용하여 RSACryptoServiceProvider의 개인키를 직접 채워보자 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/10925

.NET Framework: 566. openssl의 PEM 개인키 파일을 .NET RSACryptoServiceProvider에서 사용하는 방법
; https://www.sysnet.pe.kr/2/0/10926

.NET Framework: 638. RSAParameters와 RSA
; https://www.sysnet.pe.kr/2/0/11140

.NET Framework: 1037. openssl의 PEM 개인키 파일을 .NET RSACryptoServiceProvider에서 사용하는 방법 (2)
; https://www.sysnet.pe.kr/2/0/12598

.NET Framework: 2093. C# - PEM 파일을 이용한 RSA 개인키/공개키 설정 방법
; https://www.sysnet.pe.kr/2/0/13245




C# - Rabin-Miller 소수 생성 방법을 이용하여 RSACryptoServiceProvider의 개인키를 직접 채워보자 - 두 번째 이야기

예전에 올렸던 소스 코드를,

C# - Rabin-Miller 소수 생성 방법을 이용하여 RSACryptoServiceProvider의 개인키를 직접 채워보자
; https://www.sysnet.pe.kr/2/0/1300

개선해 보았습니다. 일단, 저 글에 공개된 소스 코드의 Main을 무한 루프로 고치고 실행하면,

static void Main(string[] args)
{
    while (true)
    {
        FillRSA(5);
    }
}

예외가 발생할 수 있는데, 이는 이미 1편에서 글을 쓴데로 P 값보다 Q 값이 더 큰 경우에 해당합니다. 제가 웬일인지 그 부분을 주석처리했는데, 다음과 같이 주석을 해제해 주시면 됩니다.

if (q > p)
{
    BigInteger temp = q;
    q = p;
    p = temp;
}

그런데, 이렇게 해도 여전히 다음과 같은 예외가 발생합니다.

byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
byte[] encryptedData;
byte[] decryptedData;

encryptedData = RSAEncrypt(dataToEncrypt, rsa.ExportParameters(false), false);
decryptedData = RSADecrypt(encryptedData, rsa.ExportParameters(true), false);

Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));

System.ArgumentNullException was unhandled
  HResult=-2147467261
  Message=Array cannot be null.
Parameter name: bytes
  ParamName=bytes
  Source=mscorlib
  StackTrace:
       at System.Text.Encoding.GetString(Byte[] bytes)
       at producePrime.Program.FillRSA(Int32 certainty) in E:\...\producePrime\Program.cs:line 159
       at producePrime.Program.Main(String[] args) in E:\...\producePrime\Program.cs:line 16
  InnerException: 

결과는 null이지만, RSADecrypt에서 예외가 발생했기 때문에 decryptedData에 null이 들어간 것입니다. 결국 최초의 예외는 다음에서 발생합니다.

System.Security.Cryptography.CryptographicException: The parameter is incorrect.

   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey,
 Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey)
   at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)
   at producePrime.Program.RSADecrypt(Byte[] DataToDecrypt, RSAParameters RSAKeyInfo, Boolean DoOAEPPadding) in E:\...\producePrime\Program.cs:line 309

이에 대한 원인이 재미있는데요. RSAParameters.Modulus의 첫 번째 바이트에 0이 들어갔기 때문입니다. 즉, P * Q의 값이 충분히 크지 않게 되면 발생하는 것인데, 가령 P와 Q의 값이 각각 64바이트 길이인데, 그 곱하기 값인 Modulus가 128바이트를 모두 채우지 않는 경우가 되면 (암호화 메서드인 Encrypt 호출은 통과하지만) 복호화 메서드인 Decrypt 호출 시 "The parameter is incorrect" 예외가 발생하는 것입니다.

따라서, 이런 조건을 갖는 P, Q의 키 값이 생성된 경우에는 그냥 버리고 다시 P, Q를 생성해야 합니다. 이렇게 해서 안정적인 버전의 소스 코드를 만들면 다음과 같습니다. ^^

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Numerics;
using System.Diagnostics;

namespace producePrime
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                FillRSA(5);
            }
        }

        static void FillRSA(int certainty)
        {
            int modulusSize = 1024;
            int modulusByteSize = modulusSize / 8 / 2;
            RSAParameters rsaParam = new RSAParameters();

            // P, Q 값 생성하고,
            BigInteger p, q;

            Stopwatch st = new Stopwatch();
            st.Start();
            ProducePQ(modulusByteSize, out p, certainty);
            ProducePQ(modulusByteSize, out q, certainty);
            st.Stop();

            Console.WriteLine("Elapsed: " + st.ElapsedMilliseconds + "(ms)");

            if (q > p)
            {
                BigInteger temp = q;
                q = p;
                p = temp;
            }

            // N 값 구하고,
            BigInteger n = p * q;

            // E 값 생성하고,
            int publicExponent = 65537;

            // Φ 값 구하고,
            BigInteger rp = (p - 1) * (q - 1);

            // D 값 구하고,
            BigInteger d = GetExtendedGcd(publicExponent, rp).Item2;

            if (d < 0)
            {
                BigInteger tMultiplier = BigInteger.Min(publicExponent, rp);
                BigInteger tDivisor = BigInteger.Max(publicExponent, rp);

                d = tDivisor + d;
                Console.WriteLine("d < 0");
                Console.WriteLine();

                BigInteger dMod = BigInteger.Remainder(d * tMultiplier, tDivisor);

                if (dMod.IsOne == false)
                {
                    Console.WriteLine("[Failed] d * e mod rp == " + dMod);
                    Console.WriteLine();
                    return;
                }
            }

            // InverseQ 값 구하고,
            BigInteger inverseQ = GetExtendedGcd(q, p).Item2;

            if (inverseQ < 0)
            {
                BigInteger tMultiplier = BigInteger.Min(q, p);
                BigInteger tDivisor = BigInteger.Max(q, p);

                inverseQ = tDivisor + inverseQ;
                Console.WriteLine("inverseQ < 0");
                Console.WriteLine();

                BigInteger qMod = BigInteger.Remainder(inverseQ * tMultiplier, tDivisor);
                if (qMod.IsOne == false)
                {
                    Console.WriteLine("[Failed] inverseQ * q mod p == " + qMod);
                    Console.WriteLine();
                    return;
                }
            }

            // DP, DQ 값 구하고,
            BigInteger dp = BigInteger.Remainder(d, (p - 1));
            BigInteger dq = BigInteger.Remainder(d, (q - 1));

            ToBigEndian(p, ref rsaParam.P, modulusByteSize);
            ToBigEndian(q, ref rsaParam.Q, modulusByteSize);
            ToBigEndian(n, ref rsaParam.Modulus, modulusByteSize * 2);

            if (rsaParam.Modulus[0] == 0) 
            {
                return; 
            }  

            ToBigEndian(publicExponent, ref rsaParam.Exponent, 3);
            ToBigEndian(d, ref rsaParam.D, modulusByteSize * 2);
            ToBigEndian(dp, ref rsaParam.DP, modulusByteSize);
            ToBigEndian(dq, ref rsaParam.DQ, modulusByteSize);
            ToBigEndian(inverseQ, ref rsaParam.InverseQ, modulusByteSize);

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(rsaParam);

            // RSACryptoServiceProvider로 암/복호화 테스트
            UnicodeEncoding ByteConverter = new UnicodeEncoding();

            byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
            byte[] encryptedData;
            byte[] decryptedData;

            encryptedData = RSAEncrypt(dataToEncrypt, rsa.ExportParameters(false), false);
            decryptedData = RSADecrypt(encryptedData, rsa.ExportParameters(true), false);

            Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
        }

        private static void ToBigEndian(BigInteger bytes, ref byte[] target, int byteSize)
        {
            List<byte> list = new List<byte>();
            list.AddRange(bytes.ToByteArray());

            for (int i = list.Count; i < byteSize; i ++)
            {
                list.Add(0);
            }

            list.Reverse();

            target = new byte[list.Count];

            Array.Copy(list.ToArray(), target, list.Count);
        }

        private static Tuple<BigInteger, BigInteger> GetExtendedGcd(BigInteger num1, BigInteger num2)
        {
            if (num2 > num1)
            {
                return GetExtendedGcd(num2, num1);
            }

            BigInteger x = 0;
            BigInteger lastx = 1;
            BigInteger y = 1;
            BigInteger lasty = 0;

            BigInteger quotient = 0;

            BigInteger tempNum2, tempx, tempy;

            while (num2 != 0)
            {
                quotient = num1 / num2;

                tempNum2 = num2;
                num2 = num1 % num2;
                num1 = tempNum2;

                tempx = lastx - quotient * x;
                lastx = x;
                x = tempx;

                tempy = lasty - quotient * y;
                lasty = y;
                y = tempy;
            }

            return new Tuple<BigInteger, BigInteger>(lastx, lasty);
        }

        private static void ProducePQ(int bytes, out BigInteger p, int certainty)
        {
            Random rand = new Random();
            byte[] pRand = new byte[bytes];

            while (true)
            {
                rand.NextBytes(pRand);
                p = new BigInteger(pRand);

                if (p.IsProbablePrime(certainty) == true)
                {
                    break;
                }
            }
        }

        public static bool IsPrime(BigInteger candidate)
        {
            if ((candidate & 1) == 0)
            {
                if (candidate == 2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            for (BigInteger i = 3; (i * i) <= candidate; i += 2)
            {
                if ((candidate % i) == 0)
                {
                    return false;
                }
            }

            return candidate != 1;
        }

        static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
        {
            try
            {
                byte[] encryptedData;
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    RSA.ImportParameters(RSAKeyInfo);
                    encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
                }
                return encryptedData;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);

                return null;
            }

        }

        static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
        {
            try
            {
                byte[] decryptedData;
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    RSA.ImportParameters(RSAKeyInfo);
                    decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
                }
                return decryptedData;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.ToString());

                return null;
            }

        }

    }
}

(첨부된 파일은 이 글의 예제 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/26/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 46  47  48  49  50  51  52  [53]  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12296정성태8/24/202010020.NET Framework: 933. C# - ETW 관련 Win32 API 사용 예제 코드 (2) NT Kernel Logger파일 다운로드1
12295정성태8/24/20209365오류 유형: 639. Bitvise - Address is already in use; bind() in ListeningSocket::StartListening() failed: Windows error 10013: An attempt was made to access a socket ,,,
12293정성태8/24/202010721Windows: 171. "Administered port exclusions" 설명
12292정성태8/20/202011972.NET Framework: 932. C# - ETW 관련 Win32 API 사용 예제 코드 (1)파일 다운로드2
12291정성태8/15/202010914오류 유형: 638. error 1297: Device driver does not install on any devices, use primitive driver if this is intended.
12290정성태8/11/202011623.NET Framework: 931. C# - IP 주소에 따른 국가별 위치 확인 [8]파일 다운로드1
12289정성태8/6/20209070개발 환경 구성: 502. Portainer에 윈도우 컨테이너를 등록하는 방법
12288정성태8/5/20209041오류 유형: 637. WCF - The protocol 'net.tcp' does not have an implementation of HostedTransportConfiguration type registered.
12287정성태8/5/20209539오류 유형: 636. C# - libdl.so를 DllImport로 연결 시 docker container 내에서 System.DllNotFoundException 예외 발생
12286정성태8/5/202010427개발 환경 구성: 501. .NET Core 용 container 이미지 만들 때 unzip이 필요한 경우
12285정성태8/4/202010746오류 유형: 635. 윈도우 10 업데이트 - 0xc1900209 [2]
12284정성태8/4/202010137디버깅 기술: 169. Hyper-V의 VM에 대한 메모리 덤프를 뜨는 방법
12283정성태8/3/202010667디버깅 기술: 168. windbg - 필터 드라이버 확인하는 확장 명령어(!fltkd) [2]
12282정성태8/2/20209433디버깅 기술: 167. windbg 디버깅 사례: AppDomain 간의 static 변수 사용으로 인한 crash (2)
12281정성태8/2/202011948개발 환경 구성: 500. (PDB 연결이 없는) DLL의 소스 코드 디버깅을 dotPeek 도구로 해결하는 방법
12280정성태8/2/202011076오류 유형: 634. 오라클 (평생) 무료 클라우드 VM 생성 후 SSH 접속 시 키 오류 발생 [2]
12279정성태7/29/202011907개발 환경 구성: 499. 닷넷에서 접근해보는 InterSystems의 Cache 데이터베이스파일 다운로드1
12278정성태7/23/20209318VS.NET IDE: 149. ("Binary was not built with debug information" 상태로) 소스 코드 디버깅이 안되는 경우
12277정성태7/23/202010730개발 환경 구성: 498. DEVPATH 환경 변수의 사용 예 - .NET Reflector의 (PDB 연결이 없는) DLL의 소스 코드 디버깅
12276정성태7/23/20209947.NET Framework: 930. 개발자를 위한 닷넷 어셈블리 바인딩 - DEVPATH 환경 변수
12275정성태7/22/202012497개발 환경 구성: 497. 닷넷에서 접근해보는 InterSystems의 IRIS Data Platform 데이터베이스파일 다운로드1
12274정성태7/21/202011858개발 환경 구성: 496. Azure - Blob Storage Account의 Location 이전 방법 [1]파일 다운로드1
12273정성태7/18/202013482개발 환경 구성: 495. Azure - Location이 다른 웹/DB 서버의 경우 발생하는 성능 하락
12272정성태7/16/20208514.NET Framework: 929. (StrongName의 버전 구분이 필요 없는) .NET Core 어셈블리 바인딩 규칙 [2]파일 다운로드1
12271정성태7/16/202010543.NET Framework: 928. .NET Framework의 Strong-named 어셈블리 바인딩 (2) - 런타임에 바인딩 리디렉션파일 다운로드1
12270정성태7/16/202011330오류 유형: 633. SSL_CTX_use_certificate_file - error:140AB18F:SSL routines:SSL_CTX_use_certificate:ee key too small
... 46  47  48  49  50  51  52  [53]  54  55  56  57  58  59  60  ...