Thread 1: EXC_BAD_ACCESS (code=2, address=0x16f603ff8)

I'm currently writing a code to achieve the RSA encryption via C++, and I stumbled across this error message when I was running it.

The first part of my code is to generate random prime numbers based on the digit from the input for the encryption. For example, if you enter 6 then the code will generate two prime numbers, M and m, between 100000 to 999999. The "fin" variable equals (M-1)*(m-1), and based on "fin" I will create an int array the size of [fin+1] in the "Random e" section. However, if I enter a number greater than 3 ( which means "fin" will go beyond 6 digits) the crash will occur and the error message will show.

I can't seem to find the problem anywhere, and if my input is no greater than 3 then the code will work exactly as I wanted it to. Is there something I missed? Thank you very much.

Xcode [14.3.1]


#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;
    

    long long int gcd(long long int x, long long int y)
    {
        
        for (long long int t; y!=0; x=t)
        {
            t=y;
            y=x%y;
        }
        return x;
    }

    long long int mod(long long int x, long long int y, long long int n)
    {
        if (x%n==0)
        {
            return 0;
        }
        
        long long int result;
        for (result=1; y>1; x=(x*x)%n)
        {
            if (y%2==1)
            {
                result = (result * x)%n;
                y=(y-1)/2;
            }
            else if (y%2==0)
            {
                y=y/2;
            }
        }
        x=(x*result)%n;
        return x;
    }

    int main()
    {
        // Random Prime Numbers------------------------------//
       
        cout << "Input digits" << endl;
        int k;
        cin >> k;
        
        cout << "Input letter for encryption" << endl;
        char f;
        cin >> f;
        
        long long int M=pow(10, k)-1;
        long long int m=pow(10, k-1);
        bool prime[M+1];
        memset(prime, true, sizeof(prime));
    
        
        for(long long int p=2; p*p<=M; p=p+1 )
        {
            if (prime[p] == true)
            {
               for (long long int i=p*p; i<=M; i=i+p)
                    prime[i]=false;
            }
        }
        
        
        int prime2[M+1];
        for (long long int t=0; t<=M; t=t+1)
        {
            prime2[t]=1;
        }
        

        for (long long int p=m, t=1; p<=M; p=p+1)
        {
            if(prime[p]==true)
            {
                prime2[t]=p;
                t=t+1;
            }
        }
        

        long long int h = count(prime, prime+M+1, true);
        long long int l = count(prime, prime+m, true);
        h=h-l;
        
      
        srand(time(NULL));
        M=rand()%h+1;
        m=rand()%h+1;
        if (M==m)
        {
            m=(M*1000)%h+1;
        }
        
        
        if (M>h)
        {
            cout << "a=" << M << " error!" << endl;
        }
        else if (m>h)
        {
            cout << "b=" << m << " error!" << endl;
        }
        else
        {
            M=prime2[M];
            m=prime2[m];
        }
        
        
        
        
        
        // Generating RSA keys----------------------------//
        
        
        long long int n = M*m;
        long long int fin = (M-1)*(m-1);
       

      
            // Generating random e for the public key {e, n}--------//

            long long int number[fin+1];
            for (long long int i=0; i<=fin; i=i+1)
            {
                number[i]=0;
            }
            

            for (long long int i=2, k=1; i<=fin; i=i+1)
            {
                if (gcd(i, fin)==1)
                {
                    number[k]=i;
                    k=k+1;
                }
            }
            

            long long int z = fin+1-count(number, number+fin+1, 0);
        
            srand(time(NULL));
            
            long long int r = rand()%z +1;
            long long int e = number[r];
            
            
    
        long long int e2 = e;
        
        long long int d=0;
        for (long long int x=1, y=0, k=1, t=1; e2%fin!=0; k=1, d=y)
        {
            t=y;
            y=x-y*(e2/fin);
            x=t;
            
            k=fin;
            fin=e2%fin;
            e2=k;
        }

        cout << endl << "The public key is {" << e << " , " << n << "}" << endl;
        cout << "The private key is {" << d << " , " << n << "}" << endl;
        
        // e、d、n  acquired
        
        // Encryption begins----------------------------//

        long long int f2 = (int)f;
        f2 = mod(f2, e, n);
        cout << "The cyphertext is " << f2 << endl;
        
        f2 = mod(f2, d, n);
        f = (char)f2;
        cout << f << endl;
        
        
        
        
    }

Answered by DTS Engineer in 760836022

I'm currently writing a code to achieve the RSA encryption via C++

To what end? Is this just an exercise in learning how RSA works? Or do you plan to deploy this?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

If the number of digits is greater than 3, then you're going to need a lot of space on the stack for your local variable number, and the stack is of limited size. It's likely that you're exceeding the stack size, and this is supported by the backtrace on the left, which shows that you crashed in ___chkstk_darwin, which you can read as "check stack".

If the number of digits is too big, then you're never going to be able to find enough memory to create an array this size. If the number of digits is greater than 3 but not too big, then you'll likely have to (e.g.) malloc memory instead of using a local variable.

Accepted Answer

I'm currently writing a code to achieve the RSA encryption via C++

To what end? Is this just an exercise in learning how RSA works? Or do you plan to deploy this?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thread 1: EXC_BAD_ACCESS (code=2, address=0x16f603ff8)
 
 
Q