Finishing touches to my first project C++

I have been slightly over-ambitious with my first project and need a little bit of help if that's ok. I need to convert the returnChar function to be able to return a string of values; at the moment it works on individual characters. It is the deciphering process of an RSA encryption and works perfectly when enciphering and deciphering one letter at a time, this is very frustrating as I just can't do it, I have managed for the encryption portion! Thanks for any help in advance, sorry about the trivial question.

**Only the returnChar function is the problem, but I'm including all of my code in case anyone is curious**

(P.S p and q are tiny just to make testing easy I know that it's not secure at this size)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  #include <iostream>
#include <math.h>

using namespace std;

unsigned long long int  modinv(unsigned long long int  u, unsigned long long int  v)
{
    unsigned long long int  inv, u1, u3, v1, v3, t1, t3, q;
    unsigned long long int  iter;

    u1 = 1;
    u3 = u;
    v1 = 0;
    v3 = v;

    iter = 1;

    while (v3 != 0)
    {

        q = u3 / v3;
        t3 = u3 % v3;
        t1 = u1 + q * v1;

        u1 = v1; v1 = t1; u3 = v3; v3 = t3;
        iter = -iter;
    }

    if (u3 != 1)
        return 0;
    if (iter < 0)
        inv = v - u1;
    else
        inv = u1;
    return inv;
}

int modExp(int b, int e, int m)
{
int remainder;
int x = 1;

while (e != 0)
{
remainder = e % 2;
e= e/2;

if (remainder == 1)
x = (x * b) % m;
b= (b * b) % m; // New base equal b^2 % m
}
return x;
}

char returnChar (unsigned long long int x)
{
    return (char) x + 87;
}

int main()
{ long unsigned int p = 17;
unsigned long long int q = 23;
unsigned long long int phi = (p-1)*(q-1);
unsigned long long int   e = 7;
unsigned long long int  c = 327;
unsigned long long int  n = p*q;
unsigned long long int  d = modinv (e,phi);
    {
         cout <<  returnChar (modExp (c, d, n));
    }
    return 0;
}
closed account (iAk3T05o)
What's a, b, c, d, e, u1, u2, v, etc. supposed to be? Use descriptive names.
closed account (zybCM4Gy)
unsigned long long is just going to cause problems. Especially on 32-bit machines. Consider something smaller.

Since you can encrypt one at a time, then in theory your code is sound. If you're having trouble with larger amounts you're probably running out of something.
the u1, u2, v portion is the extended Euclidean algorithm so using "algebraic" terms made it A LOT easier, I didn't plan on sharing it and so I take on board that changing it in future would be wise. The code is fine except for the returnChar function, at the moment it can produce "a", "b", "m" etc but I can't work out how to make it able to produce "ab", "words" etc I can only output single letters. I am using long long unsigned as before I had this problem it was working with 1024bit numbers, which require unsigned long long int.
Topic archived. No new replies allowed.