我有以下字符、数字和特殊字符:
012345789
!@#$%
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
我想要一个 bash 脚本来创建一个 txt 文件,其中包含上述所有可能的 8 位数字组合,例如:
aK@05B!8
等等。你能给我推荐一个脚本吗?
答案1
下面的插图演示了如何回答您提出的问题:
s='1 2 3 4 5' # Space separated alphabet of characters
for a in $s
do
for b in $s
do
for c in $s
do
printf "%s%s%s\n" "$a" "$b" "$c"
done
done
done
与您的 66 个字符(您已从数字范围中省略)相比,此代码的字母表只有 5 个字符,6
并且仅使用三个迭代器而不是您的八个。它生成 125 个输出:
111
112
113
114
115
121
122
...
555
使用你的字母表但仍然只使用三个迭代器将产生 287496 种组合。您可以通过将字母表中的字符数计算为迭代器数的幂来确定组合数。例如,5 3 = 125 和 66 3 = 287496,您的目标要求将是 66 8 ≈ 3.6x10 14组合。
答案2
使用这个 C++ 程序。这需要太多时间所以你需要耐心
// C++ program to generate all passwords for given characters (length 8-10)
#include <bits/stdc++.h>
using namespace std;
void generate(char* arr, int i, string s, int len)
{
// base case
if (i == 0) // when len has been reached
{
// print it out
cout << s << "\n";
// cnt++;
return;
}
// iterate through the array
for (int j = 0; j < len; j++) {
// Create new string with next character
// Call generate again until string has
// reached its len
string appended = s + arr[j];
generate(arr, i - 1, appended, len);
}
return;
}
// function to generate all possible passwords
void crack(char* arr, int len)
{
// call for all required lengths
for (int i = 8; i <= 8; i++) {
generate(arr, i, "", len);
}
}
// driver function
int main()
{
char arr[] = {'0','1','2','3','4','5','7','8','9','!','@','#','$','%','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int len = sizeof(arr) / sizeof(arr[0]);
crack(arr, len);
//cout << cnt << endl;
return 0;
}
// This code is edited by RASAL RAHIM.