-
利用排列函數,解密碼
aaaa
static void Main()
{
Stopwatch l_cc_stopWatch = new Stopwatch();
l_cc_stopWatch.Start();
string l_str_password = "icde";
/**//*
排列:从n个不同元素中,任取m(m≤n)个元素,按照一定的顺序排成一列,叫做从n个不同元素中取出m个元素的一个排列.
從排列的公式中可以看出,從1,2,3中取出2位,結果為:123,132,213,231,321,312,然而用戶填寫的密碼可能是11,22,33
故:本破解密碼程序只能破解不包含相同元素的密碼。
*/
Dictionary<int, string> l_cc_dictionary = new Dictionary<int, string>();
l_cc_dictionary.Add(1, "1");
l_cc_dictionary.Add(2, "2");
l_cc_dictionary.Add(3, "3");
l_cc_dictionary.Add(4, "4");
l_cc_dictionary.Add(5, "5");
l_cc_dictionary.Add(6, "6");
l_cc_dictionary.Add(7, "7");
l_cc_dictionary.Add(8, "8");
l_cc_dictionary.Add(9, "9");
l_cc_dictionary.Add(10, "a"); //用10代表字符串"a"
l_cc_dictionary.Add(11, "b"); //
l_cc_dictionary.Add(12, "c");
l_cc_dictionary.Add(13, "d");
l_cc_dictionary.Add(14, "e");
l_cc_dictionary.Add(15, "f");
l_cc_dictionary.Add(16, "g");
l_cc_dictionary.Add(17, "h");
l_cc_dictionary.Add(18, "i");
l_cc_dictionary.Add(19, "j");
l_cc_dictionary.Add(20, "k");
l_cc_dictionary.Add(21, "l");
l_cc_dictionary.Add(22, "m");
l_cc_dictionary.Add(23, "n");
l_cc_dictionary.Add(24, "o");
l_cc_dictionary.Add(25, "p");
l_cc_dictionary.Add(26, "q");
l_cc_dictionary.Add(27, "r");
l_cc_dictionary.Add(28, "s");
l_cc_dictionary.Add(29, "t");
l_cc_dictionary.Add(30, "u");
l_cc_dictionary.Add(31, "v");
l_cc_dictionary.Add(32, "w");
l_cc_dictionary.Add(33, "z");
l_cc_dictionary.Add(34, "y");
l_cc_dictionary.Add(35, "z");
//可以繼續往下加判斷的字符,如大寫字母A,B,C,!,@,^
int l_int_length = l_cc_dictionary.Count;
for (int i = 1; i <= l_int_length; i++)
{
bool l_bool_IsRegex = false;
int[,] source=Combinatorics.Arrange(i, l_int_length);
int b1 = source.GetUpperBound(0), b2 = source.GetUpperBound(1);
for (int a = 0; a <= b2; a++) //对各返回排列循环
{
string l_str_getPassword = null;
for (int b = 0; b <= b1; b++)
{
l_str_getPassword += l_cc_dictionary[(int)source[b, a]]; //用序数指针获取原元素的值
}
Console.WriteLine(l_str_getPassword);
if (l_str_getPassword == l_str_password)
{
l_bool_IsRegex = true;
break;
}
}
if (l_bool_IsRegex)
{
break;
}
}
l_cc_stopWatch.Stop();
Console.WriteLine("{0}:seconds", l_cc_stopWatch.Elapsed.TotalSeconds);
}
using System;
using System.Collections;
using System.Data;
/**//// <summary>
/// 组合数学函数集
/// </summary>
public class Combinatorics
{
公共函数#region 公共函数
/**//// <summary>
/// 把二维整形数组转换为数据表
/// </summary>
public static DataTable TwoDemisionIntArrayToDataTable(int[,] source)
{
DataTable dt = new DataTable();
DataRow dr;
int i, j;
int b1 = source.GetUpperBound(0), b2 = source.GetUpperBound(1); //获取二维表的各维长度
for (i = 0; i <= b1; i++) //以第二维长度创建数据表的各字段
dt.Columns.Add(i.ToString(), System.Type.GetType("System.Int32"));
for (i = 0; i <= b2; i++) //对各返回排列循环
{
dr = dt.NewRow(); //准备插入新行
for (j = 0; j <= b1; j++) //在新行中逐个填入返回排列的各元素次序
dr[j.ToString()] = source[j, i]; //用序数指针获取原元素的值
dt.Rows.Add(dr); //插入新行
}
return dt;
}
/**//// <summary>
/// 连乘积函数
/// </summary>
public static int Product(int start, int finish)
{
int factorial = 1;
for (int i = start; i <= finish; i++)
factorial *= i;
return factorial;
}
/**//// <summary>
/// 阶乘函数
/// </summary>
public static int Factorial(int n)
{
return Product(2, n);
}
/**//// <summary>
/// 排列数函数
/// </summary>
public static int ArrangeCount(int m, int n)
{
return Product(n - m + 1, n);
}
/**//// <summary>
/// 生成排列表函数
/// </summary>
public static int[,] Arrange(int m, int n)
{
int A = ArrangeCount(m, n); //求得排列数,安排返回数组的第一维
int[,] arrange = new int[m, A]; //定义返回数组
ArrayList e = new ArrayList(); //设置元素表
for (int i = 0; i < n; i++)
e.Add(i + 1);
Arrange(ref arrange, e, m, 0, 0);
return arrange;
}
/**//// <summary>
/// 组合数函数
/// </summary>
public static int CombinationCount(int m, int n)
{
int a = Product(n - m + 1, n), b = Product(2, m); //a=n-m+1 * * n ; b = m!
return (int)a / b; //c=a/b
}
/**//// <summary>
/// 生成组合表函数
/// </summary>
public static int[,] Combination(int m, int n)
{
int A = CombinationCount(m, n); //求得排列数,安排返回数组的第一维
int[,] combination = new int[m, A]; //定义返回数组
ArrayList e = new ArrayList(); //设置元素表
for (int i = 0; i < n; i++)
e.Add(i + 1);
Combination(ref combination, e, m, 0, 0);
return combination;
}
#endregion
内部核心#region 内部核心
/**//// <summary>
/// 排列函数
/// </summary>
/// <param name="reslut">返回值数组</param>
/// <param name="elements">可供选择的元素数组</param>
/// <param name="m">目标选定元素个数</param>
/// <param name="x">当前返回值数组的列坐标</param>
/// <param name="y">当前返回值数组的行坐标</param>
private static void Arrange(ref int[,] reslut, ArrayList elements, int m, int x, int y)
{
int sub = ArrangeCount(m - 1, elements.Count - 1); //求取当前子排列的个数
for (int i = 0; i < elements.Count; i++, y += sub) //每个元素均循环一次,每次循环后移动行指针
{
int val = RemoveAndWrite(elements, i, ref reslut, x, y, sub);
if (m > 1) //递归条件为子排列数大于1
Arrange(ref reslut, elements, m - 1, x + 1, y);
elements.Insert(i, val); //恢复刚才删除的元素
}
}
/**//// <summary>
/// 组合函数
/// </summary>
/// /// <param name="reslut">返回值数组</param>
/// <param name="elements">可供选择的元素数组</param>
/// <param name="m">目标选定元素个数</param>
/// <param name="x">当前返回值数组的列坐标</param>
/// <param name="y">当前返回值数组的行坐标</param>
private static void Combination(ref int[,] reslut, ArrayList elements, int m, int x, int y)
{
ArrayList tmpElements = new ArrayList(); //所有本循环使用的元素都将暂时存放在这个数组
int elementsCount = elements.Count; //先记录可选元素个数
int sub;
for (int i = elementsCount - 1; i >= m - 1; i--, y += sub) //从elementsCount-1(即n-1)到m-1的循环,每次循环后移动行指针
{
sub = CombinationCount(m - 1, i); //求取当前子组合的个数
int val = RemoveAndWrite(elements, 0, ref reslut, x, y, sub);
tmpElements.Add(val); //把这个可选元素存放到临时数组,循环结束后一并恢复到elements数组中
if (sub > 1 || (elements.Count + 1 == m && elements.Count > 0)) //递归条件为 子组合数大于1 或 可选元素个数+1等于当前目标选择元素个数且可选元素个数大于1
Combination(ref reslut, elements, m - 1, x + 1, y);
}
elements.InsertRange(0, tmpElements); //一次性把上述循环删除的可选元素恢复到可选元素数组中
}
/**//// <summary>
/// 返回由Index指定的可选元素值,并在数组中删除之,再从y行开始在x列中连续写入subComb个值
/// </summary>
private static int RemoveAndWrite(ArrayList elements, int index, ref int[,] reslut, int x, int y, int count)
{
int val = (int)elements[index];
elements.RemoveAt(index);
for (int i = 0; i < count; i++)
reslut[x, y + i] = val;
return val;
}
#endregion
}
#region 公共函数
/**//// <summary>
/// 把二维整形数组转换为数据表
/// </summary>
public static DataTable TwoDemisionIntArrayToDataTable(int[,] source)
{
DataTable dt = new DataTable();
DataRow dr;
int i, j;
int b1 = source.GetUpperBound(0), b2 = source.GetUpperBound(1); //获取二维表的各维长度
for (i = 0; i <= b1; i++) //以第二维长度创建数据表的各字段
dt.Columns.Add(i.ToString(), System.Type.GetType("System.Int32"));
for (i = 0; i <= b2; i++) //对各返回排列循环
{
dr = dt.NewRow(); //准备插入新行
for (j = 0; j <= b1; j++) //在新行中逐个填入返回排列的各元素次序
dr[j.ToString()] = source[j, i]; //用序数指针获取原元素的值
dt.Rows.Add(dr); //插入新行
}
return dt;
}
/**//// <summary>
/// 连乘积函数
/// </summary>
public static int Product(int start, int finish)
{
int factorial = 1;
for (int i = start; i <= finish; i++)
factorial *= i;
return factorial;
}
/**//// <summary>
/// 阶乘函数
/// </summary>
public static int Factorial(int n)
-
相关阅读:
区间DP——石子合并
线性DP-最短编辑距离、编辑距离
生成树协议
交换机技术
以太网原理
接口知识点
目前在中国有影响的几种现场总线比较
委托
C#有关继承知识点
C#数组总结
-
原文地址:https://www.cnblogs.com/wang123/p/1210706.html
Copyright © 2020-2023
润新知