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: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198:
| #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <math.h> #include <string.h> #include <time.h>
int main() { printf("RSA-Verschluesselung\n"); Eingabe(); return 0; }
void Eingabe() { int p,q,n,e,d; bool b_sizeOfValue = false; int bool_temp = 0; char M[5],c; bool test = isPrimeN(8); printf("%s",test); while(b_sizeOfValue == false) { //printf("Bitte die erste Primzal eingeben:\n"); //scanf("%d",&p); //fflush(stdin);
while(bool_temp == 0) { p = CreateRandomNumber();
if(isPrimeN(p) == true) { bool_temp = 1; } }
bool_temp = 0;
while(bool_temp == 0) { q = CreateRandomNumber(); if(isPrimeN(q) == true && p!=q) { bool_temp = 1; } }
if((p*q)>200 && p!=q) b_sizeOfValue = true; } printf("Zuerst werden 2 Primzahlen benötigt, diese wurden für Sie generiert! Es sind\np = %d\nund\nq = %d\n\n(Weiter mit der Entertaste)"); while(c!="\n") { scanf("%s",c); }
n = (p*q); e = CalculateEncodingExponent(p,q); d = CalculateDecipheringExponent(p,q,e);
verschluesseln(M,n,e,d); }
bool isPrimeN(int zahl) { bool temp = true; for (int i = 2;i <= zahl-1;i++) { if(zahl%i == 0) { temp = false; } }
return temp; }
int ggt(int a, int b) { if(a<0) { a *=-1; } if(b<0) { b *=-1; } while(a!=b) { if(a<b) { b=b-a; } else if(b<a) { a=a-b; } } return a; }
int teilerfremd(int a, int b) { int x=ggt(a,b);
if(x!=1) return 0; else return 1; } int CreateRandomNumber() { int zahl; srand(time(NULL)); zahl = rand(); return zahl; } int CalculateEncodingExponent(int p, int q) { int zahl = 1; int temp = 0; int x = (p-1)*(q-1);
printf("erstelle zufaellige Primzahl...\n");
while(temp == 0) { zahl = CreateRandomNumber(); if(isPrimeN(zahl) == true && teilerfremd(zahl,x)==1) { temp = 1; } }
printf("Zufaellige Primzahl = %d\n",zahl); return zahl; }
int CalculateDecipheringExponent(int p, int q, int zweiteZ) { int zahl,mod,erg;
printf("berechne 3. Zahl...\n");
mod = (p-1)*(q-1);
for(int i = 1;i>0;i++) {
erg = (zweiteZ*i)%mod; if(erg == 1) { zahl = i; i = -1; } } printf("errechnete 3.Zahl = %d\n",zahl); return zahl; }
void verschluesseln(char nachricht[5],int n, int e,int d) { int zahl; int message[5]; int hilf; char temp; printf("\nIhre Nachricht in verschluesselter Form: "); for(int i = 0;i<=5-1;i++) { zahl = (int)nachricht[i]; if(zahl!=0) { hilf = zahl; for(int k = 0;k<=e-2;k++) { zahl *=hilf; zahl = zahl%n; } message[i] = zahl; printf("%d",zahl); } } printf("\nDecodieren...\nDecodierte Nachricht: "); for(int j = 0;j<=5-1;j++) { zahl = message[j]; hilf = zahl; for(int f = 0;f<=d-2;f++) { zahl *=hilf; zahl = zahl%n; } temp = (char)zahl; printf("%c",temp); } } |