admin管理员组文章数量:1574959
其实这个算法已经用PHP实现了,详细请看这篇文章:PHP自定义字典序任意长度排列算法
但是这篇文章访问量低的吓人(2年才100多)
今天我们就用C/C++来实现这个算法并更名为密码暴力破解算法(C/C++排列组合算法)
这个算法主要是进位思想,比如9 + 1 = 10,如果把 9看成z,+1比表示下一位,那么z的下一位就是aa(字典是26个字母a,b,c...x,y,z)
C++实现方法
引用库
#include <string>
#include <vector>
这里采用分文件编写的方法(可以合并到一个文件)
首先设计一个类
main.h
class StringCombination
{
private:
std::vector<std::string> stringsDict;
std::pair<std::string, int> term;
int dictLength;
ssize_t valueLength = -1;
public:
StringCombination(const std::vector<std::string> &strings, int length = -1) : stringsDict(strings), dictLength(strings.size()), valueLength(length)
{
term.first = stringsDict[0];
term.second = stringsDict[0].length();
}
std::string nextValue(std::string value);
};
然后就是函数主体
main.cpp
#include "main.h"
std::string StringCombination::nextValue(std::string value)
{
if (value.empty())
value = "";
int i = 0, dictOrder;
std::vector<std::pair<std::string, int>> nextValueCut;
std::string temp;
while (value.length() > i)
{
dictOrder = -1;
for (size_t n = 0; n < dictLength; n++)
{
term.first = stringsDict[n];
term.second = stringsDict[n].length();
temp = value.substr(i, term.second);
if (temp == term.first)
{
i += term.second - 1;
dictOrder = n;
break;
}
}
++i;
nextValueCut.push_back({temp, dictOrder});
}
bool carry = false;
bool closeCarry = false;
bool handlerEnd = false;
bool addLength = true;
ssize_t countCarry = 0;
for (ssize_t n = nextValueCut.size() - 1; n >= 0; n--)
{
if (nextValueCut[n].second == -1)
continue;
if (nextValueCut[n].second == dictLength - 1)
{
if (!handlerEnd || carry)
carry = true;
nextValueCut[n].second = 0;
}
else
{
++nextValueCut[n].second;
if (carry)
closeCarry = true;
}
if (!handlerEnd)
handlerEnd = true;
else if (!carry)
continue;
nextValueCut[n].first = stringsDict[nextValueCut[n].second];
if (closeCarry)
{
carry = false;
closeCarry = false;
}
if (!carry)
{
addLength = false;
break;
}
++countCarry;
}
if (valueLength != -1 && countCarry >= valueLength)
addLength = false;
std::string result = "";
if (addLength)
{
result += stringsDict[0];
}
for (ssize_t n = 0; n < nextValueCut.size(); n++)
{
result += nextValueCut[n].first;
}
return result;
}
示例
#include "main.cpp"
#include <iostream>
int main()
{
std::vector<std::string> dict = { "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" };
StringCombination stringCombination(dict);
std::string nowValue = "aaa";
for (size_t i = 0; i < 1000; i++)
{
nowValue = stringCombination.nextValue(nowValue);
std::cout << nowValue << "\n";
}
return(0);
}
C实现方法
使用C语言实现这个就相对来说比较复杂了
引用库
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
函数主体
可以看见这是个静态函数,你也可以删掉static关键字,使其成为普通函数
这个支持任意字符字典
static char *nextValue(char *value, const char *stringsDict, int maxLength)
{
int valueLength = strlen(value);
if (valueLength == 0)
value = "";
typedef struct EchoWord
{
char value;
int location;
} Word;
int i = 0, add = 0, dictOrder, dictLength = strlen(stringsDict);
long n;
Word term = {stringsDict[0], 0};
Word *nextValueCut = (Word *)malloc(sizeof(Word) * valueLength);
char *result = (char *)malloc(sizeof(char) * (valueLength + 1));
char *tempMalloc = NULL;
char temp;
if (!nextValueCut)
{
result[0] = '\0';
goto Clear;
}
if (!result)
{
result[0] = '\0';
goto Clear;
}
for (i = 0; i < valueLength; i++)
{
dictOrder = -1;
for (n = 0; n < dictLength; n++)
{
term.value = stringsDict[n];
temp = value[i];
if (temp == term.value)
{
dictOrder = n;
break;
}
}
nextValueCut[i].value = temp;
nextValueCut[i].location = dictOrder;
}
bool carry = false;
bool closeCarry = false;
bool handlerEnd = false;
bool addLength = true;
long countCarry = 0;
for (long n = valueLength - 1; n >= 0; n--)
{
if (nextValueCut[n].location == -1)
continue;
if (nextValueCut[n].location == dictLength - 1)
{
if (!handlerEnd || carry)
carry = true;
nextValueCut[n].location = 0;
}
else
{
++nextValueCut[n].location;
if (carry)
closeCarry = true;
}
if (!handlerEnd)
handlerEnd = true;
else if (!carry)
continue;
nextValueCut[n].value = stringsDict[nextValueCut[n].location];
if (closeCarry)
{
carry = false;
closeCarry = false;
}
if (!carry)
{
addLength = false;
break;
}
++countCarry;
}
if (maxLength != -1 && countCarry >= maxLength)
addLength = false;
if (addLength)
{
tempMalloc = (char *)malloc(sizeof(char));
if (tempMalloc)
{
result = (char *)realloc(tempMalloc, sizeof(char) * (++valueLength + 1));
}
if (!result)
goto Clear;
result[0] = stringsDict[0];
++add;
}
for (n = 0; n < valueLength - add; n++)
{
result[n + add] = nextValueCut[n].value;
}
result[valueLength] = '\0';
Clear:
free(nextValueCut);
return result;
}
示例
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
//函数放这里
int main()
{
char* dict = "abcdefg";
char* now = "aa";
long i;
for (i = 0; i < 1000; i++)
{
now = nextValue(now, dict, -1);
printf("%s\n",now);
}
return(0);
}
总结
这是用C/C++排列组合算法(支持任意字典),比较简单,有问题请在评论区留言(顺便收藏一下,万一用到了呢~)
(什么?没有注释?ᓚᘏᗢ)
差点忘了C++中的字典支持字符串数组(但是支持的不是特别好,推荐字符),C语言那个算法字典只支持字符数组
版权声明:本文标题:CC++暴力破解算法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1727779797a1129198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论