如何生成单词"密码"的不同组合。它可以包括大小写和两个' S'字符可以替换为5和' o'字符可以用数字0替换
例如结果:
密码
p @ s5word
pa5sWord
pa55w0rd
PAssw0rd
等
任何帮助将不胜感激
这是我到目前为止所做的,但只返回相同的字符串" ppassword"
char[] CharList = new char[]
{'p','a','s','s','w','o','r','d','@','5','5','0','P','A','S','S','W','O','R','D'};
string password = "password";
int _base = CharList.Length;
int current = IntPasswordsGenerated++;
do
{
password = CharList[current % _base] + password;
current /= _base;
} while (current != 0);
return password;
答案 0 :(得分:1)
好的,所以我对此有所了解。我重建简单的“置换”功能,并使用自定义的对象列表而不是纯char
:
public class CharData
{
char[] _char= new char[0];
public char[] Characters { get { return _char; } set { _char= value; } }
}
// method to initialize character data
public static List<CharData> Initialize()
{
List<CharData> charData = new List<CharData>();
charData.Add(new CharData {
Characters = new char[] { 'p', 'P' }
});
charData.Add(new CharData {
Characters = new char[] { 'a', 'A' }
});
charData.Add(new CharData {
Characters = new char[] { 's', 'S', '5' }
});
charData.Add(new CharData {
Characters = new char[] { 's', 'S', '5' }
});
charData.Add(new CharData {
Characters = new char[] { 'w', 'W' }
});
charData.Add(new CharData {
Characters = new char[] { 'o', 'O', '0' }
});
charData.Add(new CharData {
Characters = new char[] { 'r', 'R' }
});
charData.Add(new CharData {
Characters = new char[] { 'd', 'D' }
});
// Add your characters in order you want them to be processed
return charData;
}
// rebuilded permutation
public static void Build(List<CharData> data, string build = "", int startingIndex = -1)
{
if( build.Length == data.Count )
{
Console.WriteLine(build);
}
else
{
for ( int i = startingIndex + 1; i < data.Count; i ++ )
{
for ( int j = 0; j < data[i].Characters.Length; j++)
{
Build(data, build + data[i].Characters[j], i);
}
}
}
}
public static void Main(string[] args)
{
Build(Initialize());
}
答案 1 :(得分:1)
旁注:正如Tinwor所说,这是一种替代而非排列的替代品。
所以,这是一种方法:
快速示例:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
private static readonly Dictionary<char, List<char>> _replacementChars = new Dictionary<char, List<char>>{};
public static void Main()
{
// Fill replacement chars collection
_replacementChars.Add('p', new List<char>{'p', 'P'});
_replacementChars.Add('a', new List<char>{'a', 'A', '4'});
_replacementChars.Add('s', new List<char>{'s', 'S', '$', '&', '5'});
_replacementChars.Add('w', new List<char>{'w', 'W'});
_replacementChars.Add('o', new List<char>{'o', 'O', '0', '@'});
_replacementChars.Add('r', new List<char>{'r', 'R', '4'});
_replacementChars.Add('d', new List<char>{'d', 'D'});
// We want to search on lower variants of each char of the source word, beacuase it's simple this way
var sourceWord = "password".ToLower();
var sourceWordChars = sourceWord.ToCharArray();
var targetWordChars = new char[sourceWord.Length];
// TODO: This will need a different implementation if we want to get all possible variations of the sourceWord rather than just a random version of it
for(var i = 0; i < sourceWordChars.Length; i++)
{
var replacement = GetReplacementChar(sourceWordChars[i]);
targetWordChars[i] = replacement;
}
var targetWord = string.Join("", targetWordChars);
Console.WriteLine(string.Format("Source word: {0}", sourceWord));
Console.WriteLine(string.Format("Target word: {0}", targetWord));
}
private static char GetReplacementChar(char charToReplace)
{
if(!_replacementChars.ContainsKey(charToReplace))
{
return charToReplace;
}
var chars = _replacementChars[charToReplace];
// Get random char from list
var rand = new Random();
var index = rand.Next(0, chars.Count);
return chars[index];
}
}
以上代码的the fiddle。