我想要生成20个字符的wpa2键,它只包含1- 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000 C++之间的数字。每个键的输出格式必须为20个字符格式,如下所示:
00000000000000000001
00000000000000000002
00000000000000000003
00000000000000000011
12300000000099945611 诸若此类。
我有这个代码,但是:
发布于 2013-08-06 17:05:26
在这种情况下,为什么不只是有一个字符串,并且在最低的索引处增加字符,如果它喜欢( case 9‘),那么增加下一个字符。重复并冲洗至完成。
所以,就像这样:
std::string s = '000';
std::string::size_type len = s.length()
while (s != "999")
{
cout << s << endl;
s[len-1] ++;
int i = len-1;
while(s[i] > '9' && i >= 0)
{
s[i] = '0';
i--;
s[i]++;
}
}但是,如果有一台机器每秒执行1,000,000,000次上述代码的一次循环,则需要317年的时间才能在序列中运行。所以我希望你有足够的时间和健康的饮食。
发布于 2013-08-06 17:18:38
如果要将64位整数存储为数字,则编译器需要支持64位整数。它可以作为长数据类型来支持。将i更改为无符号长,并将数字文字更改为10000000000000000000ULL。请注意,不要将这些值转换为int (意外或其他),否则会丢失一些数据。
发布于 2013-08-06 17:40:22
从字面上讲,遍历所有这些数字需要数年的时间。
如果您只是想生成一个随机的20个字符的WPA2键,那么应该使用内置函数来执行类似的操作:
#include <iostream>
#include <string>
#include <chrono>
#include <random>
#include <algorithm>
std::string get_key() {
// Define all allowed characters (a WPA2 key can also contain letters).
std::string chars =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// Shuffle the characters.
std::mt19937 g(std::chrono::system_clock::now().time_since_epoch().count());
std::shuffle(std::begin(chars), std::end(chars), g);
// Return the first 20 characters.
return chars.substr(0, 20);
}
int main() {
std::cout << get_key() << std::endl;
}如果您只想要一个由数字组成的键,那么从chars中删除所有alpha字符。
https://stackoverflow.com/questions/18086046
复制相似问题