从字符列表生成所有组合 [英] Generate all combinations from list of characters

查看:59
本文介绍了从字符列表生成所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于为渗透测试人员建立一个实验室,以从 4 个字母的单词创建 MD5 哈希值.我需要单词有小写和大写字母以及数字和特殊字符的组合,但我似乎不知道如何按所有顺序组合任何给定的字符.所以目前我有这个:

I am busy implementing a lab for pen testers to create MD5 hashes from 4 letter words. I need the words to have a combination of lower and uppercase letters as well as numeric and special characters, but I just do not seem to find out how to combine any given characters in all orders. So currently I have this:

my $str = 'aaaa';
print $str++, $/ while $str le 'dddd';

什么会做:

aaaa
aaab
aaac
aaad
...
...
dddd

但是我没有办法做到:

Aaaa
AAaa
aAaa
...
dddD

更不用说添加数字和特殊字符了.我真正想做的是让角色根据给定的列表创建单词.因此,如果我想使用 abeDod@# 它应该从这些字符创建所有组合.

Not even to mention adding numbers and special characters. What I really wanted to do was to make the characters to create words based on a given list. So if I feel I want to use abeDod@# it should create all combinations from those characters.

编辑以澄清.

假设我给了字符 aBc# 我需要它给它一个计数来说明每个单词最多必须有 4 个字母,并且包含所有给定字符的组合,例如:

Let's say I give the characters aBc# I need it to give it a a count to say it must have maximum of 4 letters per word and with combination of all the given characters, like:

aBc#
Bac#
caB#
#Bca
...

我希望能澄清这个问题.

I hope that clarifies the question.

推荐答案

使用属于 ASCII 代码的整数列表 对于您接受的字符,使用您最喜欢的(伪)随机数生成器从中采样.然后使用 chr 将每个转换为其字符并连接它们.

Use a list of integers that are ASCII codes for the characters you accept, to sample from it using your favorite (pseudo-)random number generator. Then convert each to its character using chr and concatenate them.

喜欢

perl -wE'$rw .= chr( 32+(int rand 126-32) ) for 1..4; say $rw'

注意事项

  • 我使用单行只是为了方便复制粘贴测试.请在脚本中很好地编写此内容

  • I use a one-liner merely for easy copy-paste testing. Write this nicely in a script, please

我使用粗略的 rand,适合稍微调整一下.如果需要,更换更好的

I use the sketchy rand, good for shuffling things a bit. Replace with a better one if needed

粘合四个(伪)随机数不会构建良好的分布;即使每个字母都有其自身的作用,整个事情也没有.但是这四个应该可以满足大多数需求.

Glueing four (pseudo-)random numbers does not build a good distribution; even as each letter on its own does, the whole thing does not. But the four should satisfy most needs.

如果没有,我认为您需要生成一个更长的列表(允许的字符范围可能重复四次)并将其随机化,然后绘制四个字母的子序列.还有很多工作

If not, I think that you'd need to produce a far longer list (range of allowed chars repeated four times perhaps) and randomize it, then draw four-letter subsequences. A lot more work

我需要使用 rand 进行一些踢踏舞以产生从 32 到 126 的(随机)整数,因为它只需要范围的末尾.此外,这需要所有这些范围,可能不是您想要的;因此,请指定要从中绘制的子范围或特定列表

I need to tap dance a little to produce (random-ish) integers from 32 to 126 using rand, since it takes only the end of range. Also, this takes all of them from that range, likely not what you want; so specify subranges, or specific lists that you want to draw from

这篇关于从字符列表生成所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆