我想要插入函数来生成字符序列,但是无法编写或搜索它。
我尝试使用https://www.tutorialspoint.com/cplusplus-program-to-generate-a-sequence-of-n-characters-for-a-given-specific-case,但是它对我不起作用,并且出现“段错误”。
我已经使用过类似的基金,但是上面的解决方案却给我带来了段错误。
string = salt,b_salt,outTrip,buffer;
string dict = ".012345678A:;<=>?@BCDEFGHIJKLMNOPQRSTUVWXY[\\]^_`ZabcdefghijklmnopqrstuvwxyzT"
for(i = 0; i < n; i++)
{
buffer = generateSeq(dict)
b_salt = buffer;
b_salt += "...";
salt = b_salt.substr(1,2);
s_test = crypt(buffer.c_str(),salt.c_str());
outTrip = s_test;
cout << seq << "\t\t\t" << outTrip << endl;
我的代码:
#include <iostream>
#include <unistd.h>
#include <cstring>
using namespace std;
void GenSequence(char str[],int m,int len,char *seq)
{
int i,j=0,k,index,r;
for(i = 0; i < m; i++)
{
if(j == 0) {
seq[j++] = str[rand()%len];}
else
{
h:
index = rand()%len;
for(k = 0; k < j; k++)
{
if(str[index] == seq[k])
goto h;
}
seq[j++] = str[index];
}
}
seq[j] = '\0';
}
int main()
{
int n,m,len,i;
char str[] = {46,48,49,50,51,52,53,54,55,56,65,58,59,60,61,62,63,64,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,91,92,93,94,95,96,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122};
n = 1000;
m = 7;
len = strlen(str);
string trip,match = "shi",buf,salt;
char seq[m];
for(i = 0; i < n; i++)
{
outTrip = "";
GenSequence(str,seq);
b_salt = seq;
b_salt += "...";
salt = b_salt.substr(1,2);
outTrip = crypt(seq,salt.c_str());
cout << seq << "\t\t\t" << outTrip << endl;
}
}
解决方案: 我删除了一些错误的字符,即 字符串字典=“ .0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz”; 对我有用。