📄 encryption.cs
字号:
using System;
using System.Runtime.InteropServices;
using System.Xml;
using System.Reflection;
namespace DJD.Security
{
[Guid("2929CA28-4CA5-4f61-AC5E-B5FF7BADADEC"), ClassInterface(ClassInterfaceType.AutoDual)]
public class keyParams
{
public keyParams(){}
static BigInteger _N,_E,_D;
public BigInteger N
{
get{return _N;}
set{_N = value;}
}
public BigInteger E
{
get{return _E;}
set{_E = value;}
}
public BigInteger D
{
get{return _D;}
set{_D = value;}
}
}
// algorithm taken from http://pajhome.org.uk/crypt/rsa/index.html 8/03 by Dom DiNatale
// Java implementation adapted to C# from http://www.geocities.com/wailian/QuickStartRSA/
// Also got some help from http://www.codeproject.com/csharp/biginteger.asp
[Guid("BEA6B48A-A477-45a6-AA82-5DE407FF2502"), ClassInterface(ClassInterfaceType.AutoDual)]
public sealed class Encryption : IDisposable
{
private keyParams _key = new keyParams();
public Encryption(){}
public Encryption(int bitSize)
{
if (!((bitSize == 8) | (bitSize == 16) | (bitSize == 32) | (bitSize == 64) | (bitSize == 128) | (bitSize == 256) | (bitSize == 512))){throw new ArgumentException("Encryption supports only 8, 16, 32, 64, 128, 256, 512 bit encryption");};
BigInteger q;
BigInteger p;
BigInteger m;
p = BigInteger.genPseudoPrime(bitSize,10,new System.Random());
do
{
q = BigInteger.genPseudoPrime(bitSize,10, new System.Random());
}
while(p == q);
_key.N = (p*q);
m = (p-1)*(q-1);
_key.E = new BigInteger("10001", 16);
_key.D = _key.E.modInverse(m);
}
public void GenerateKeys(int bitSize)
{
Encryption obj = new Encryption(bitSize);
this._key = obj.ExportParamaters(true);
obj.Dispose();
obj = null;
}
public string Decrypt( BigInteger[] encrypted )
{
if (_key.D == 0) {throw new ApplicationException("Invalid key");};
int i ;
BigInteger[] decrypted = new BigInteger[encrypted.Length] ;
for( i = 0 ; i < decrypted.Length ; i++ )
decrypted[i] = encrypted[i].modPow((_key.D),(_key.N)) ;
char[] charArray = new char[decrypted.Length] ;
for( i = 0 ; i < charArray.Length ; i++ )
charArray[i] = (char) ( (byte)decrypted[i].IntValue() ) ;
return( new string( charArray ) ) ;
}
public BigInteger[] Encrypt(string message )
{
if ((_key.E == 0) || (_key.N == 0)) {throw new ApplicationException("Invalid Key");};
int i ;
byte[] temp = new byte[1] ;
byte[] digits = System.Text.ASCIIEncoding.ASCII.GetBytes(message);
BigInteger[] bigdigits = new BigInteger[digits.Length] ;
for( i = 0 ; i < bigdigits.Length ; i++ )
{
temp[0] = digits[i] ;
bigdigits[i] = new BigInteger( temp ) ;
}
BigInteger[] encrypted = new BigInteger[bigdigits.Length] ;
for( i = 0 ; i < bigdigits.Length ; i++ )
encrypted[i] = bigdigits[i].modPow( _key.E,_key.N) ;
return( encrypted ) ;
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
public keyParams ExportParamaters(bool includePrivateParams)
{
keyParams pKey = _key;
if (!includePrivateParams)
{
pKey.D = 0;
}
return pKey;
}
public string ExportParamatersAsXML(bool includePrivateParams)
{
keyParams pKey = _key;
if (!includePrivateParams)
{
pKey.D = 0;
}
string str = "<key><N>" + System.Convert.ToBase64String(pKey.N.getBytes()) + "</N><E>" + System.Convert.ToBase64String(pKey.E.getBytes()) + "</E><D>" + System.Convert.ToBase64String(pKey.D.getBytes()) + "</D></key>";
return str;
}
public void ExportParamatersToFile(bool includePrivateParams, string FilePath)
{
System.IO.FileInfo fl = new System.IO.FileInfo(FilePath);
System.IO.FileStream fs = fl.OpenWrite();
string str = ExportParamatersAsXML(includePrivateParams);
byte[] b = new byte[str.Length];
b = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
fs.Write(b,0,b.Length);
fs.Close();
fs = null;
fl = null;
}
public void ImportKeyParameters(keyParams key)
{
_key.D = key.D;
_key.E = key.E;
_key.N = key.N;
}
public string ImportKeyParameters(System.IO.FileInfo keyFile)
{
keyParams pKey = new keyParams();
XmlTextReader reader = new XmlTextReader(keyFile.FullName);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "N")
{
reader.Read();
byte[] b = Convert.FromBase64String(reader.Value);
pKey.N = new BigInteger(b);
}
if (reader.Name == "E")
{
reader.Read();
byte[] b = Convert.FromBase64String(reader.Value);
pKey.E = new BigInteger(b);
}
if (reader.Name == "D")
{
reader.Read();
byte[] b = Convert.FromBase64String(reader.Value);
pKey.D = new BigInteger(b);
}
}
};
_key = pKey;
return "";
}
// public string ImportKeyParameters(string strXML)
// {
// keyParams pKey = new keyParams();
//
// string strN = "";
// string strE = "";
// string strD = "";
//
// int i, j;
// i = strXML.IndexOf("<N>");
// j = strXML.IndexOf("</N>");
// strN = Mid(str,i,j-i);
//
// i = strXML.IndexOf("<E>");
// j = strXML.IndexOf("</E>");
// strE = Mid(str,i,j-i);
//
// i = strXML.IndexOf("<D>");
// j = strXML.IndexOf("</D>");
// strD = Mid(str,i,j-i);
//
// byte[] b;
//
// b = Convert.FromBase64String(strN);
// pKey.N = new BigInteger(b);
//
// b = Convert.FromBase64String(strE);
// pKey.E = new BigInteger(b);
//
// b = Convert.FromBase64String(strD);
// pKey.D = new BigInteger(b);
//
// _key = pKey;
// return "";
// }
public string GetJavaScriptClientCode()
{
System.IO.Stream st = GetType().Assembly.GetManifestResourceStream("DJD.Security.BigInt.js");
byte[] b = new byte[st.Length];
st.Read(b,0,(int)st.Length);
st.Close();
string strBigInt = System.Text.ASCIIEncoding.ASCII.GetString(b);
return
"<script language=JavaScript>\n" +
strBigInt +
"\n\n" +
"// To use the encrypt function, use an onclick or onsubmit event of a button to encrypt a textbox's value property.\n" +
"// The following example puts the value of the encrypted password into a hidden input and makes the password text *'s the length of the original password.\n" +
"// <INPUT type=" + (char)34 + "submit" + (char)34 + " value=" + (char)34 + "Login" + (char)34 + " OnClick=" + (char)34 + "var s = txtPassword.value; password.value=Encrypt(s,'<%=Session(" + (char)34 + "n" + (char)34 + ")%>','<%=Session(" + (char)34 + "e" + (char)34 + ")%>'); var strStar = ''; for (i = 0; i < s.length; i++) {strStar = strStar + '*';} txtPassword.value = strStar" + (char)34 + "><input id=" + (char)34 + "password" + (char)34 + " type=" + (char)34 + "hidden" + (char)34 + " name=" + (char)34 + "password" + (char)34 + ">\n" +
"function Encrypt (str, n, e) {\n" +
" var n=str2bigInt(n,16,0);\n" +
" var x=str2bigInt(str,95,n.length);\n" +
" var y=str2bigInt(e,16,0);\n" +
" powMod(x,y,n,0);\n" +
" x = bigInt2str(x,16);\n" +
" return x;\n" +
"}\n" +
"</script>\n";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -