RSA Verschlüsslung

  • Na jaaa :D Geht besser uns schneller. Aber war nur ein Test. Den ich aber gerne hier mit euch Teile.

    Spoiler anzeigen

    #include <stdio.h>
    #include <stdlib.h>
    #include <Windows.h>
    #include <limits.h>
    #include <math.h>

    #define DATENTYP long long

    short eratosthenes(char *nListe, DATENTYP nGrenze)
    {
    unsigned long long i, nPrimteiler = 2, q;

    if (nGrenze < 2) return 0;

    for (i = 2; i < nGrenze; i++)
    {
    nListe[i] = 1;
    }

    while (nPrimteiler*nPrimteiler < nGrenze)
    {
    q = 2;
    while (q*nPrimteiler < nGrenze)
    {
    nListe[q*nPrimteiler] = 0;
    q++;
    }

    do
    {
    nPrimteiler++;
    } while (nListe[nPrimteiler] == 0);
    }

    return 1;
    }

    short IsPrime(DATENTYP x){DATENTYP i;if(x<2){return 0;}for(i=2;i<x-1;i++){if(!(x % i)) {return 0;}}return 1;}

    DATENTYP EuklidggT(DATENTYP a, DATENTYP b)
    {
    DATENTYP x = a, y = b, Qxy = x / y, Rxy = x%y;
    if (a == 0) return 0;
    if (b == 0) return 0;
    if (a == b) return 0;
    while (Rxy != 0)
    {
    x = y;
    y = Rxy;
    Qxy = x / y;
    Rxy = x%y;
    }
    return y;
    }

    short ErwEuklidAlg(DATENTYP a, DATENTYP b, DATENTYP *ggT, DATENTYP *s, DATENTYP *t)
    {
    DATENTYP nTemp,nMax = (a < b ? a : b),x,y,r,m,nSalt,i;

    DATENTYP *q = (DATENTYP *)malloc(nMax * sizeof(DATENTYP));

    if (a == 0) return 0;
    if (b == 0) return 0;
    if (a == b) return 0;
    if (a < b)
    {
    nTemp = b;
    b = a;
    a = nTemp;
    }

    m = 0;

    x = a;
    y = b;
    q[m] = x / y;
    r = x%y;
    while (r != 0)
    {
    m++;
    x = y;
    y = r;
    q[m] = x / y;
    r = x%y;
    }

    *ggT = y;

    if (m == 0)
    {
    *s = 1;
    *t = -(a / b) + 1;
    return 1;
    }

    *s = 1;
    *t = - q[m-1];
    for (i = m - 2; i >= 0; i--)
    {
    nSalt = *s;
    *s = *t;
    *t = nSalt - *t * q[i];
    }

    free(q);
    return 1;

    }

    double get_time()
    {
    LARGE_INTEGER t, f;
    QueryPerformanceCounter(&t);
    QueryPerformanceFrequency(&f);
    return (double)t.QuadPart / (double)f.QuadPart;
    }

    int random_number(int min_num, int max_num)
    {
    int result = 0, low_num = 0, hi_num = 0;
    if (min_num<max_num)
    {
    low_num = min_num;
    hi_num = max_num + 1; // this is done to include max_num in output.
    }
    else{
    low_num = max_num + 1;// this is done to include max_num in output.
    hi_num = min_num;
    }
    srand(time(NULL));
    result = (rand() % (hi_num - low_num)) + low_num;
    return result;
    }

    unsigned DATENTYP powNewMod(DATENTYP base, DATENTYP exponet, DATENTYP mod)
    {
    unsigned DATENTYP r = 1; // remainder

    for (int i = 0; i < exponet; ++i)
    r = (r * base) % mod;

    return r;
    }

    int main(int nArgs,char **szArgs) {


    DATENTYP
    nAnzahl = 2000,
    nCounter = 0,
    N,
    EulerN,
    e,
    nPPrim = 11,
    _nPPrim,
    nQPrim = 13,
    _nQPrim,
    nggT = 0,
    nS = 0,
    nT = 0,
    m,
    c = 0;

    unsigned int nRandom;
    short bIsOk = 0,berwEuk;

    char *pnListe = (char*)malloc((nAnzahl+100) * sizeof(char));

    eratosthenes(pnListe, nAnzahl+100);

    do
    {
    _nPPrim = random_number(2, nAnzahl);
    } while (pnListe[_nPPrim] == 0);
    nPPrim = _nPPrim;

    do
    {
    _nQPrim = random_number(nPPrim+1, nAnzahl + 100);
    } while (pnListe[_nQPrim] == 0);
    nQPrim = _nQPrim;


    N = nPPrim * nQPrim;
    EulerN = (nPPrim - 1)*(nQPrim - 1);
    do
    {
    do
    {
    nRandom = random_number(1000, EulerN - 1);

    e = EuklidggT(nRandom, EulerN);

    if (e == 1)
    {
    e = nRandom;
    }

    } while (e != nRandom && e < EulerN);

    berwEuk = ErwEuklidAlg(e, EulerN, &nggT, &nS, &nT);


    } while (nT <= 0);
    if (berwEuk == 0)
    {
    printf_s("Error\n");
    }

    m = 7;

    c = powNewMod(m, e,N);

    printf_s("%lld verschluesselt: %lld\n",m,c);
    m = 0;
    m = powNewMod(c, nT,N);
    printf_s("%lld entschluesselt: %lld\n",c, m);


    free(pnListe);

    }