c++-密码生成小程序

这篇博客介绍了一个C++程序,用于生成复杂的密码并将其存储到文件中。程序使用了随机数生成器来确保密码的随机性和安全性,同时可以根据日期和时间定制密码描述,方便记忆。密码生成器考虑了大小写字母、数字和特殊字符的组合,增强了密码强度。
摘要由CSDN通过智能技术生成

老是需要改密码, 所以自己写个生成器, 会生成很复杂的密码, 并记录在文件里面

输出

在这里插入图片描述

代码

#include <iostream>
#include <fstream>
#include <random>
#include <unistd.h>
using namespace std;

static std::string base = "qwertyuiopasdfghjklzxcvbnm1234567890%-+=,./?:;'@!^$&()[]{}";
const int MAX_PATH = 250;
const int SIZE = 30;
const int BS = SIZE / 3;

std::string getCurrentDir() {
    char buffer[MAX_PATH];
    getcwd(buffer, MAX_PATH);
    return buffer;
}

long long getDate() {
    time_t t = time(nullptr);
    char tmp[64];
    strftime( tmp, sizeof(tmp), "%Y%m%d",localtime(&t) );
    return strtol(tmp, nullptr, 10);
}

void generate2(int n, long long date, int time, const char* description = "默认") {
    std::string dir = getCurrentDir() + "/passwd";
    std::string path = dir + "_" + to_string(date) + "_" + to_string(time);
    cout << path << endl;
    ofstream fs(path);
    random_device rd;
    mt19937 mt(rd());
    uniform_int_distribution<int> dis(0, base.size() - 1);
    std::string pd;
    for (int i = 0; i < n; i++) {
        char c = base[dis(mt)];
        if (dis(mt) % BS == 0) {
            c = (char)toupper(c);
        }
        pd.push_back(c);
    }
    if (description) {
        fs << "描述: " << description << endl;
        cout << "描述: " << description << endl;
    }
    cout << "密码: " << pd << endl;
    fs << "密码: " << pd;
}

int main() {
    generate2(SIZE, getDate(), time(nullptr), "王者荣耀密码");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值