Advertisement
mgostih

C++ Password Generator

Jul 14th, 2016
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <Windows.h>
  4. #include <chrono>
  5. #include <random>
  6. int main(){
  7.     int length = 0;
  8.     char* file = "Password_Output.txt";
  9.     typedef std::chrono::high_resolution_clock myclock;
  10.     myclock::duration c_time = myclock::now().time_since_epoch();
  11.     std::mt19937 generator(c_time.count() ^ GetCurrentProcessId());
  12.     std::cout << "Write number of charachters the password must have: ";
  13.     std::cin >> length;
  14.     if (!std::cin.good()) {
  15.         std::cout << "Not a valid input." << std::endl;
  16.         system("PAUSE>NUL");
  17.         return 0;
  18. }
  19.     char*password = new char[length + 1];
  20.     for (int i = 0; i < length; i++) password[i] = (generator() % 95) + 32;
  21.     password[length] = 0;
  22.     std::ofstream myfile;
  23.     myfile.open(file);
  24.     if (myfile.is_open()){
  25.         myfile << password;
  26.         myfile.close();
  27.         std::cout << "Password written to file: " << file << std::endl;
  28.     }
  29.     else{
  30.         std::cout << "Failed to create and open file " << file << std::endl;
  31.     }
  32.     delete[] password;
  33.     system("PAUSE>NUL");
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement