My C++ code gives an exception when I execute it, what is the problem?

-2

I had a problem with my C++ code where I try to execute them with TBB but for some reason when I use tbb::parallel_for() it gives me the exception thrown at 0x00a2223c in rsa_tbb.exe: 0xc0000005: access violation writing location 0x017f8ccf. occurred. I don't know what kind of solution should I take. I am using Visual Studio 2017 for coding.

char* rsa_decrypt_without_private_key(const long long* message, const unsigned long message_size, const struct public_key_class* pub)
{
    tbb::task_group g;
    struct private_key_class unknownpriv[1];
    long long p, q, phi_max, e, d;
    p = z;
    q = pub->modulus / p;
    phi_max = (p - 1) * (q - 1);
    e = powl(2, 8) + 1;
    g.run([&] { d = ExtEuclid(phi_max, e); });
    while (d < 0) {
        d = d + phi_max;
    }
    unknownpriv->modulus = pub->modulus;
    unknownpriv->exponent = d;

    if (message_size % sizeof(long long) != 0) {
        g.run([&] { fprintf(stderr, "Error: message_size is not divisible by %d, so cannot be output of 
        rsa_encrypt\n", (int)sizeof(long long)); });
        return NULL;
    }
    char *decrypted;
    g.run([&] { decrypted = (char*) malloc(message_size / sizeof(long long)); });
    char *temp;
    g.run([&] { temp = (char*) malloc(message_size); });
    if ((decrypted == NULL) || (temp == NULL)) {
        fprintf(stderr, "Error: Heap allocation failed.\n");
        return NULL;
    }
    long long i = 0;
    tbb::parallel_for(int(0), int(message_size / 8), 1, [&](int i) { g.run([&] { temp[i] 
    = rsa_modExp(message[i], unknownpriv->exponent, unknownpriv->modulus); }); });

    tbb::parallel_for(int(0), int(message_size / 8), 1, [&](int i) { decrypted[i] = temp[i]; });
    g.run([&] {free(temp); });
    return decrypted;
}
long long rsa_modExp(long long b, long long e, long long m)
{
    if (b < 0 || e < 0 || m <= 0) {
        exit(1);
    }
    b = b % m;
    if (e == 0) return 1;
    if (e == 1) return b;
    if (e % 2 == 0) {
        return (rsa_modExp(b * b % m, e / 2, m) % m);
    }
    if (e % 2 == 1) {
        return (b * rsa_modExp(b, (e - 1), m) % m);
    }
}
c++
intel
tbb

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0