Guest User

Untitled

a guest
Jun 3rd, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. /*
  2. Tag 8
  3. Erstellen Sie schrittweise ein Programm,
  4. das einen Benutzernamen und ein Passwort abfragt,
  5. verschlüsselt, speichert und überprüft.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. void gen_key(char *, int);
  12. void create_account(char *, char *);
  13. void encrypt(char *, char *);
  14. void save_to_file(char *, char *);
  15. void login_input(char *, char *);
  16. int check(char *, char *);
  17.  
  18. int main()
  19. {
  20. char user[10]; // Login-Name kann max 10 Zeichen enthalten
  21. char pass[9]; // Passwort kann max 8 zeichen enthalten
  22. char key[9]; // Schluessel fuer XOR-Verschluesselung
  23. int key_len = sizeof(key)/sizeof(char);
  24. char input_user[10];
  25. char input_pass[9];
  26. int user_correct;
  27. int pass_correct;
  28.  
  29. gen_key(key, key_len);
  30.  
  31. create_account(user, pass);
  32.  
  33. printf("\nuser: %s, clear text password: %s", user, pass);
  34.  
  35. encrypt(pass, key);
  36.  
  37. printf("\nuser: %s, encrypted password: %s", user, pass);
  38.  
  39. save_to_file(user, pass);
  40.  
  41. do {
  42. login_input(input_user, input_pass);
  43.  
  44. encrypt(input_pass, key);
  45.  
  46. user_correct = check(user, input_user);
  47. pass_correct = check(pass, input_pass);
  48. if (user_correct == 1 && pass_correct == 1) {
  49. printf("access allowed\n");
  50. } else {
  51. printf("access denied\n");
  52. }
  53. } while (user_correct == 0 || pass_correct == 0);
  54.  
  55. return 0;
  56. }
  57.  
  58. void gen_key(char * key_poi, int l) {
  59. int i = key_poi + l - 1;
  60. for ( ; key_poi < i; key_poi++) {
  61. *key_poi = rand() % 127; // generiert ein ASCII-Wert (127 bedeutet der Anzahl der mögliche ASCII Werte) eigentlich 128
  62. }
  63. *key_poi = '\0'; // setzte der letze Wert in dem Array auf NULL
  64. }
  65.  
  66. void create_account(char *user_poi, char *pass_poi) {
  67. puts("\n*** create an account ***");
  68. printf("user: ");
  69. scanf("%s", user_poi);
  70. printf("password: ");
  71. scanf("%s", pass_poi);
  72. }
  73.  
  74. void encrypt(char * pass_poi, char * key_poi) {
  75. for ( ; *pass_poi != '\0'; pass_poi++, key_poi++) {
  76. *pass_poi = *pass_poi ^ *key_poi;
  77. }
  78. }
  79.  
  80. void save_to_file(char *user_poi, char *pass_poi) {
  81. FILE *doc;
  82. doc = fopen("pass_liste.dat", "w"); // spaeter a
  83. fprintf(doc, "\n");
  84. for ( ; *user_poi != '\0'; user_poi++) {
  85. fprintf(doc, "%c", *user_poi);
  86. }
  87. fprintf(doc, "\n");
  88. for ( ; *pass_poi != '\0'; pass_poi++) {
  89. fprintf(doc, "%c", *pass_poi);
  90. }
  91. }
  92.  
  93. void login_input(char *input_user_poi, char *input_pass_poi) {
  94. puts("\n\n*** login ***");
  95. printf("user: ");
  96. scanf("%s", input_user_poi);
  97. printf("password: ");
  98. scanf("%s", input_pass_poi);
  99. }
  100.  
  101. int check(char *saved_poi, char *input_poi) {
  102. int i;
  103. int gleich = 1;
  104. int l = strlen(saved_poi);
  105. for(i = 0; (gleich == 1 && i < l); saved_poi++, input_poi++, i++ ) {
  106. if (*saved_poi != *input_poi) {
  107. gleich = 0;
  108. printf("user name or password is wrong\n");
  109. }
  110. }
  111. return gleich;
  112. }
Add Comment
Please, Sign In to add comment