基于.net 4.0框架的Cipher演示程序
using System.Text; namespace Cipher.Algorithm
{
static class Caesar
{
static public string Encrypt(string input, int key)
{
StringBuilder sb = new StringBuilder();
for(int i = ; i < input.Length; ++i)
{
if ('a' <= input[i] && input[i] <= 'z')
{
sb.Append((char)((input[i] - 'a' + key + ) % + 'a'));
}
else if ('A' <= input[i] && input[i] <= 'Z')
{
sb.Append((char)((input[i] - 'A' + key + ) % + 'A'));
}
else
{
sb.Append(input[i]);
}
}
return sb.ToString();
} static public string Decrypt(string input, int key)
{
StringBuilder sb = new StringBuilder();
for (int i = ; i < input.Length; ++i)
{
if ('a' <= input[i] && input[i] <= 'z')
{
sb.Append((char)((input[i] - 'a' - key + ) % + 'a'));
}
else if ('A' <= input[i] && input[i] <= 'Z')
{
sb.Append((char)((input[i] - 'A' - key + ) % + 'A'));
}
else
{
sb.Append(input[i]);
}
}
return sb.ToString();
}
}
}
using System;
using System.Text; namespace Cipher.Algorithm
{
public class Hill
{
// 矩阵阶数
private int _level;
// 加密矩阵
private long[][] _matrix;
// 解密矩阵
private long[][] _inverseMatrix = null; private int _times = ; // 用于填充的无效字符
const char INVALID_CHAR = 'A'; /// <summary>
/// 带阶数的构造函数
/// </summary>
/// <param name="level">矩阵阶数</param>
public Hill(int level)
{
_level = level;
while(_inverseMatrix == null)
{
_matrix = getRandomMatrix();
_inverseMatrix = getInverseMatrix(_matrix);
++_times;
}
;
} public Hill(int level, long[][] matrix)
{
_level = level;
_matrix = matrix;
_inverseMatrix = getInverseMatrix(_matrix);
if (null == _inverseMatrix) _inverseMatrix = getNewMatrix();
} #region Properties public int Level
{
get
{
return _level;
}
} /// <summary>当前矩阵
/// </summary>
public long[][] Matrix
{
get
{
return _matrix;
}
} public long[][] InverseMatrix
{
get
{
return _inverseMatrix;
}
} public int Times
{
get
{
return _times;
}
}
#endregion /// <summary>
/// 得到一个新的整数矩阵
/// </summary>
/// <returns>矩阵</returns>
public long[][] getNewMatrix()
{
long[][] res = new long[_level][];
for (int i = ; i < _level; ++i) res[i] = new long[_level];
for (int i = ; i < _level; ++i)
for (int j = ; j < _level; ++j) res[i][j] = ;
return res;
} /// <summary>
/// 得到一个n阶整数矩阵
/// </summary>
/// <param name="level">阶数</param>
/// <returns>矩阵</returns>
public static long[][] getNewMatrix(int level)
{
long[][] res = new long[level][];
for (int i = ; i < level; ++i) res[i] = new long[level];
for (int i = ; i < level; ++i)
for (int j = ; j < level; ++j) res[i][j] = ;
return res;
} /// <summary>
/// 求关于MOD26的逆矩阵
/// </summary>
/// <param name="o">原矩阵</param>
/// <returns>逆矩阵</returns>
private long[][] getInverseMatrix(long[][] o)
{
long[][] res = getNewMatrix();
long[][] original = getNewMatrix(); for (int i = ; i < _level; ++i)
{
for (int j = ; j < _level; ++j)
{
if (i == j) res[i][j] = ;
else res[i][j] = ;
original[i][j] = o[i][j];
}
}
for (int k = ; k <_level; ++k)
{
bool isGCD = false;
for (int i = k; i < _level; ++i)
{
if (GCD(original[i][k], ) == )
{
isGCD = true;
if (i != k)
{
long[] temp1 = original[i], temp2 = res[i];
original[i] = original[k]; res[i] = res[k];
original[k] = temp1; res[k] = temp2;
}
break;
}
}
// 若矩阵一列中没有与26互素的元素,则认为该矩阵不可逆
if (!isGCD) return null;
long ie = getInverseElement(original[k][k], );
Console.WriteLine(original[k][k] + "的逆元是:" + ie);
if (- == ie) return null;
for (int j = ; j < _level; ++j)
{
original[k][j] = (original[k][j] * ie) % ;
res[k][j] = (res[k][j] * ie) % ;
}
for (int i = k + ; i < _level; ++i)
{
long l = original[i][k] / original[k][k];
for (int j = ; j < _level; ++j)
{
// 对增广矩阵的运算
res[i][j] = getMOD((res[i][j] - l * res[k][j]), );
// 对原矩阵的运算
original[i][j] = getMOD((original[i][j] - l * original[k][j]), );
}
}
}
for (int k = _level - ; k > ; --k)
{
if (original[k][k] == ) return null;
for (int i = k - ; i >= ; --i)
{
long l = original[i][k] / original[k][k]; // 对增广矩阵的运算
for (int j = ; j < _level; ++j)
{
if (res[k][j] == ) continue;
res[i][j] = getMOD((res[i][j] - l * res[k][j]), );
}
// 对原矩阵的运算
original[i][k] = getMOD((original[i][k] - l * original[k][k]), );
}
}
return res;
} private long getMOD(long x, long m)
{
while (x < m)
{
x += m;
}
return x % m;
} /// <summary>
/// 求a关于m的乘法逆元
/// </summary>
/// <param name="a"></param>
/// <param name="m"></param>
/// <returns>逆元</returns>
public static long getInverseElement(long a, long m)
{
long x = , y = ;
long gcd = E_GCD(a, m, ref x, ref y);
if ( % gcd != ) return -;
x *= / gcd;
m = Math.Abs(m);
long res = x % m;
if (res <= ) res += m;
return res;
} /// <summary>
/// 拓展欧几里德算法
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns>GCD(a, b)</returns>
public static long E_GCD(long a, long b, ref long x, ref long y)
{
if ( == b)
{
x = ;
y = ;
return a;
}
long res = E_GCD(b, a % b, ref x, ref y);
long temp = x;
x = y;
y = temp - a / b * y;
return res;
} /// <summary>
/// 求最大公约数
/// </summary>
/// <param name="x">第一个参数</param>
/// <param name="y">第二个参数</param>
/// <returns>最大公约数</returns>
static public long GCD(long x, long y)
{
if (y == ) return x;
return GCD(y, x % y);
} static int GetRandomSeed()
{
byte[] bytes = new byte[];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, );
} private long[][] getRandomMatrix()
{
long[][] res = getNewMatrix(); for (int i = ; i < _level; ++i)
{
for (int j = ; j < _level; ++j)
{
int t;
Random rd = new Random(GetRandomSeed());
t = rd.Next(, );
res[i][j] = t;
}
}
return res;
} private string getOneGroup(string input, long[][] matrix)
{
StringBuilder sb = new StringBuilder();
int[] p = new int[_level];
for (int i = ; i < _level; ++i)
{
if (i < input.Length)
p[i] = input[i] - 'A';
else p[i] = INVALID_CHAR;
}
for (int i = ; i < _level; ++i)
{
long o = ;
for (int j = ; j < _level; ++j)
{
o += matrix[i][j] * p[j] ;
}
Console.Write(o.ToString() + " ");
sb.Append((char)(o % + 'A'));
}
Console.WriteLine();
return sb.ToString();
} /// <summary>
/// 加密
/// </summary>
/// <param name="input">请确保输入的字符串只有字母</param>
/// <returns></returns>
public string Encrypt(string input)
{
StringBuilder sb = new StringBuilder();
input = input.ToUpper();
for (int i = ; i < input.Length; i += _level)
{
int end = _level < (input.Length - i) ? _level : (input.Length - i);
sb.Append(getOneGroup(input.Substring(i, end), _matrix));
}
return sb.ToString();
} public string Decrypt(string input)
{
StringBuilder sb = new StringBuilder();
input = input.ToUpper();
for (int i = ; i < input.Length; i += _level)
{
int end = _level < (input.Length - i) ? _level : (input.Length - i);
sb.Append(getOneGroup(input.Substring(i, end), _inverseMatrix));
}
return sb.ToString();
}
}
}
using System.Text;
using System.Windows; namespace Cipher.Algorithm
{
public static class Playfair
{
private static char[,] _key = new char[, ]; // 经过处理的5×5矩阵
private static Point[] _location = new Point[]; // 26个字母在key中的位置
private static string _group; // 分组后的字符串
private static char _ch = 'Q'; // 无效字母,如Q, K, X public static string Encrypt(string input)
{
StringBuilder sb = new StringBuilder();
string str = group(input);
for(int i = ; i < str.Length; i += )
{
int r1 = (int)(_location[str[i] - 'A'].X);
int r2 = (int)(_location[str[i + ] - 'A'].X);
int c1 = (int)(_location[str[i] - 'A'].Y);
int c2 = (int)(_location[str[i + ] - 'A'].Y);
// 字母同行
if (r1 == r2)
{
sb.Append(_key[r1, (c1 + ) % ]).Append(_key[r1, (c2 + ) % ]);
}
// 字母同列
else if (c1 == c2)
{
sb.Append(_key[(r1 + ) % , c1]).Append(_key[(r2 + ) % , c1]);
}
else
{
if (r1 > r2 && c1 > c2)
{
sb.Append(_key[r1, c2]).Append(_key[r2, c1]);
}
else if (r1 < r2 && c1 > c2)
{
sb.Append(_key[r2, c1]).Append(_key[r1, c2]);
}
else if (r1 > r2 && c1 < c2)
{
sb.Append(_key[r1, c2]).Append(_key[r2, c1]);
}
else
{
sb.Append(_key[r2, c1]).Append(_key[r1, c2]);
}
}
}
return sb.ToString();
} public static string Decrypt(string input)
{
StringBuilder sb = new StringBuilder();
string str = (string)input.ToUpper();
if (str.Length % == || str.Length == || str.IndexOf(' ') != -) return "";
for (int i = ; i < str.Length; i += )
{
int r1 = (int)(_location[str[i] - 'A'].X);
int r2 = (int)(_location[str[i + ] - 'A'].X);
int c1 = (int)(_location[str[i] - 'A'].Y);
int c2 = (int)(_location[str[i + ] - 'A'].Y);
// 字母同行
if (r1 == r2)
{
sb.Append(_key[r1, (c1 - + ) % ]).Append(_key[r1, (c2 - + ) % ]);
}
// 字母同列
else if (c1 == c2)
{
sb.Append(_key[(r1 - + ) % , c1]).Append(_key[(r2 - + ) % , c1]);
}
else
{
if (r1 > r2 && c1 > c2)
{
sb.Append(_key[r1, c2]).Append(_key[r2, c1]);
}
else if (r1 < r2 && c1 > c2)
{
sb.Append(_key[r2, c1]).Append(_key[r1, c2]);
}
else if (r1 > r2 && c1 < c2)
{
sb.Append(_key[r1, c2]).Append(_key[r2, c1]);
}
else
{
sb.Append(_key[r2, c1]).Append(_key[r1, c2]);
}
}
}
for(int i = ; i < sb.Length; ++i)
{
if(sb[i].Equals(sb[i - ]) && sb[i - ].Equals(_ch))
{
sb.Remove(i - , );
}
}
if (sb[sb.Length - ].Equals(_ch)) sb.Remove(sb.Length - , );
return sb.ToString();
} public static char[, ] Key(string word)
{
string temp = word.ToUpper();
StringBuilder sb = new StringBuilder();
bool[] flag = new bool[];
for(int i = ; i < temp.Length; ++i)
{
// 该字母未出现过
if (flag[temp[i] - 'A'] == false)
{
sb.Append(temp[i]);
}
flag[temp[i] - 'A'] = true;
}
for(int i = ; i < ; ++i)
{
if (i == 'J' - 'A')
{
continue;
}
if (flag[i] == false)
{
sb.Append((char)(i + 'A'));
}
}
for (int i = ; i < ; ++i)
{
for(int j = ; j < ; ++j)
{
_key[i, j] = sb[i * + j];
Point insert = new Point(i, j);
_location[_key[i, j] - 'A'] = insert;
}
}
return _key;
} private static string group(string input)
{
StringBuilder sb = new StringBuilder();
string temp = input.ToUpper();
for(int i = ; i < temp.Length; )
{
if ( != i && sb.Length > && temp[i] == sb[sb.Length - ])
{
sb.Append(_ch);
}
else if ('A' <= temp[i] && temp[i] <= 'Z')
{
sb.Append(temp[i]);
++i;
}
else
{
++i;
}
}
if (sb.Length % == )
{
sb.Append(_ch);
}
_group = sb.ToString();
return sb.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Cipher.Algorithm
{
class Rd
{
public static int GetRandomSeed()
{
byte[] bytes = new byte[];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, );
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text; namespace Cipher.Algorithm
{
class RSA
{
// 已保存的素数集
protected int[] primes = { , , , , , , , , , , , , , , }; protected BigInteger rsa_e;
protected BigInteger rsa_d;
protected BigInteger rsa_n; protected BigInteger rsa_p;
protected BigInteger rsa_q; #region Properties
public string P
{
get
{
return rsa_p.ToString();
}
}
public string Q
{
get
{
return rsa_q.ToString();
}
} public string E
{
get
{
return rsa_e.ToString();
}
} public string D
{
get
{
return rsa_d.ToString();
}
} public string N
{
get
{
return rsa_n.ToString();
}
}
#endregion public RSA()
{
BigInteger p, q;
p = getRandomPrime();
q = getRandomPrime();
while (p == q)
{
// 确保p与q不相等
q = getRandomPrime();
}
BigInteger n = p * q;
BigInteger fi_n = (p - ) * (q - );
BigInteger e = getRandomPrime();
while (GCD(fi_n, e) != )
{
e = getRandomPrime();
}
BigInteger d = getInverseElement(e, fi_n); rsa_e = e;
rsa_d = d;
rsa_n = n;
rsa_p = p;
rsa_q = q;
} public RSA(BigInteger p, BigInteger q, BigInteger e)
{
rsa_p = p;
rsa_q = q;
rsa_e = e;
BigInteger n = p * q;
BigInteger fi_n = (p - ) * (q - );
if (GCD(fi_n, e) != ) return;
BigInteger d = getInverseElement(e, fi_n); rsa_d = d;
rsa_n = n;
} public BigInteger[] Encrypt(string input)
{
List<BigInteger> res = new List<BigInteger>();
char[] c = input.ToArray();
for (int i = ; i < c.Length; ++i)
{
res.Add(EncryptSingle(c[i], rsa_e));
}
return res.ToArray();
} public char[] Decrypt(BigInteger[] input)
{
List<char> res = new List<char>();
for (int i = ; i < input.Length; ++i)
{
int ch = Int32.Parse(EncryptSingle(input[i], rsa_d).ToString());
res.Add((char)ch);
}
return res.ToArray();
} /// <summary>
/// 对单个字符进行幂运算加密
/// </summary>
/// <param name="input"></param>
/// <param name="m"></param>
/// <returns></returns>
protected BigInteger EncryptSingle(BigInteger input, BigInteger m)
{
BigInteger res = ;
for (int i = ; i < m; ++i)
{
res = (res * input) % rsa_n;
}
return res;
} protected BigInteger getRandomPrime()
{
Random rd = new Random(Rd.GetRandomSeed());
BigInteger res = new BigInteger(primes[rd.Next(, primes.Length)]);
return res;
} protected BigInteger GCD(BigInteger a, BigInteger b)
{
if (b == BigInteger.Zero) return a;
return GCD(b, a % b);
} /// <summary>
/// 求a关于m的乘法逆元
/// </summary>
/// <param name="a">原数</param>
/// <param name="m">被MOD的数</param>
/// <returns>逆元</returns>
protected BigInteger getInverseElement(BigInteger a, BigInteger m)
{
BigInteger x = , y = ;
BigInteger gcd = E_GCD(a, m, ref x, ref y);
if ( % gcd != ) return -;
x *= / gcd;
m = BigInteger.Abs(m);
BigInteger res = x % m;
if (res <= ) res += m;
return res;
} /// <summary>
/// 拓展欧几里德算法
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns>GCD(a, b)</returns>
protected BigInteger E_GCD(BigInteger a, BigInteger b, ref BigInteger x, ref BigInteger y)
{
if ( == b)
{
x = ;
y = ;
return a;
}
BigInteger res = E_GCD(b, a % b, ref x, ref y);
BigInteger temp = x;
x = y;
y = temp - a / b * y;
return res;
}
}
}
基于.net 4.0框架的Cipher演示程序的更多相关文章
- 一个基于Net Core3.0的WPF框架Hello World实例
目录 一个基于Net Core3.0的WPF框架Hello World实例 1.创建WPF解决方案 1.1 创建Net Core版本的WPF工程 1.2 指定项目名称,路径,解决方案名称 2. 依赖库 ...
- CefSharp基于.Net Framework 4.0 框架编译
CefSharp基于.Net Framework 4.0 框架编译 本次源码使用的是Github上CefSharp官方的79版本源码 准备 IDE Visual Studio 2017 Enterpr ...
- 基于NetMQ的TLS框架NetMQ.Security的实现分析
基于NetMQ的TLS框架NetMQ.Security的实现分析 前言 介绍 交互过程 支持的协议 TLS协议 支持的算法 实现 握手 第一次握手 Client Hello 第二次握手 Server ...
- springmvc工作原理以及源码分析(基于spring3.1.0)
springmvc是一个基于spring的web框架.本篇文章对它的工作原理以及源码进行深入分析. 一.springmvc请求处理流程 二.springmvc的工作机制 三.springmvc核心源码 ...
- 一个功能完备的.NET开源OpenID Connect/OAuth 2.0框架——IdentityServer3
今天推荐的是我一直以来都在关注的一个开源的OpenID Connect/OAuth 2.0服务框架--IdentityServer3.其支持完整的OpenID Connect/OAuth 2.0标准, ...
- 基于MyEclipse+9.0+++Tomcat+7.0的SSH+平台搭建
基于MyEclipse+9.0+++Tomcat+7.0的SSH+平台搭建 http://wenku.baidu.com/view/96fbfe0f581b6bd97f19ea1d.html 用MyE ...
- 基于Hadoop2.2.0版本号分布式云盘的设计与实现
基于Hadoop2.2.0版本号分布式云盘的设计与实现 一.前言 在学习了hadoop2.2一个月以来,我重点是在学习hadoop2.2的HDFS.即是hadoop的分布式系统,看了非常久的源代码看的 ...
- 基于MEF的插件框架之总体设计
基于MEF的插件框架之总体设计 1.MEF框架简介 MEF的全称是Managed Extensibility Framework(MEF),其是.net4.0的组成部分,在3.5上也可以使用.熟悉ja ...
- robotlegs2.0框架实例源码带注释
robotlegs2.0框架实例源码带注释 Robotlegs2的Starling扩展 有个老外写了robotleges2的starling扩展,地址是 https://github.com/brea ...
随机推荐
- SSM框架中使用日志框架
在 pom,xml 配置 Log4j jar 添加一个 mybatis_log.xml 文件 完整配置信息 <?xml version="1.0" encoding=&quo ...
- Python模拟浏览器前进后退操作
# 模拟浏览器前进后退操作 # 代码中引入selenium版本为:3.4.3 # 通过Chrom浏览器访问发起请求 # Chrom版本:59 ,chromdriver:2.3 # 需要对应版本的Chr ...
- C# 学习第二天笔记
1. Convert 类型转换 数据类型不兼容的需要转换时,使用Convert转换.转成什么类型就to谁 例: string--> int string a=“b” Int c= Conver ...
- luogu 1593 因子和 约数+线性筛
等比数列那里忘判项数等于 $1$ 的情况了. Code: #include <cstdio> #include <vector> #include <algorithm& ...
- Mysql主从同步 异常Slave_SQL_Running: No
在刚搭建好的mysql主从节点上对从节点进行操作,导致同步异常:报错如下: 从节点执行: mysql> show slave status\G;************************* ...
- 在CentOS 7上搭建WordPress
环境(ECS阿里云服务器) 服务器操作系统:CentOS 7.3 : 博客部署服务器:Apache HTTP: 数据库:MySql: 框架:WordPress: 步骤 一.安装 Apache HTTP ...
- TP框架如何绑定参数。目的进行ajax验证
TP框架的自动绑定 对于某些操作的情况(例如模型的写入和更新方法),可以支持参数的自动绑定,例如: 首先需要开启DB_BIND_PARAM配置参数: 'DB_BIND_PARAM' => tru ...
- Android OkHttp3简介和使用详解
一 OKHttp简介 OKHttp是一个处理网络请求的开源项目,Android 当前最火热网络框架,由移动支付Square公司贡献,用于替代HttpUrlConnection和Apache HttpC ...
- WebStrom编程小技巧--HTML快速创建指定id或者类名的div
打印div标签快速方法:“先打出#yz,然后Tab键补全即可获得<div id="yz"></div>同理:我们也可以先打出“.tz"然后Tab键 ...
- Runtime 源码阅读
Runtime 属性说明 /** * 每一个 Java 应用程序都有一个关联的运行时对象 * * @author unascribed * @see java.lang.Runtime#getRunt ...