//加密字符串,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了) public string DesEncrypt(string strText, string strEncrKey) //解密字符串,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了) public string DesDecrypt(string strText,string sDecrKey) //加密数据文件,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了) public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey) //解密数据文件,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了) public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey) //MD5加密 public string MD5Encrypt(string strText) */ /****************************************************************************************** * Cryptography class is for Cryptography author:Jim e_mail:[email protected] * thanks: * URI: * * *****************************************************************************************/ using System; using System.Security.Cryptography; using System.Text; using System.IO; using System.Windows.Forms ; namespace Netbee.Classes.Security { /// <summary> ///Cryptography /// </summary> public class Cryptography { public Cryptography() { } /// <summary> /// Encrypt the string /// Attention:key must be 8 bits /// </summary> /// <param name="strText">string</param> /// <param name="strEncrKey">key</param> /// <returns></returns> public string DesEncrypt(string strText, string strEncrKey) { byte[] byKey=null; byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; try { byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ; cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch(System.Exception error) { MessageBox.Show(error.Message); return "error:" +error.Message+"\r"; } } /// <summary> /// Decrypt string /// Attention:key must be 8 bits /// </summary> /// <param name="strText">Decrypt string</param> /// <param name="sDecrKey">key</param> /// <returns>output string</returns> public string DesDecrypt(string strText,string sDecrKey) { byte[] byKey = null; byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; byte[] inputByteArray = new Byte[strText.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); return encoding.GetString(ms.ToArray()); } catch(System.Exception error) { MessageBox.Show(error.Message); return "error:"+error.Message+"\r"; } } /// <summary> /// Encrypt files /// Attention:key must be 8 bits /// </summary> /// <param name="m_InFilePath">Encrypt file path</param> /// <param name="m_OutFilePath">output file</param> /// <param name="strEncrKey">key</param> public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey) { byte[] byKey=null; byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; try { byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8)); FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); //Create variables to help with read and write. byte[] bin = new byte[100]; //This is intermediate storage for the encryption. long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write); //Read from the input file, then encrypt and write to the output file. while(rdlen < totlen) { len = fin.Read(bin, 0, 100); encStream.Write(bin, 0, len); rdlen = rdlen + len; } encStream.Close(); fout.Close(); fin.Close(); } catch(System.Exception error) { MessageBox.Show(error.Message.ToString()); } } /// <summary> /// Decrypt files /// Attention:key must be 8 bits /// </summary> /// <param name="m_InFilePath">Decrypt filepath</param> /// <param name="m_OutFilePath">output filepath</param> /// <param name="sDecrKey">key</param> public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey) { byte[] byKey = null; byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; try { byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8)); FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); //Create variables to help with read and write. byte[] bin = new byte[100]; //This is intermediate storage for the encryption. long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider(); CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); //Read from the input file, then encrypt and write to the output file. while(rdlen < totlen) { len = fin.Read(bin, 0, 100); encStream.Write(bin, 0, len); rdlen = rdlen + len; } encStream.Close(); fout.Close(); fin.Close(); } catch(System.Exception error) { MessageBox.Show( "error:"+error.Message); } } /// <summary> /// MD5 Encrypt /// </summary> /// <param name="strText">text</param> /// <returns>md5 Encrypt string</returns> public string MD5Encrypt(string strText) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText)); return System.Text.Encoding.Default.GetString(result); } } }
虽不常用,但可看看算法的幻方程序,我从C改成了C# using System;namespace cs.jq { /// <summary> /// MagicSquare 的摘要说明。 /// </summary> public class MagicSquare { private int N; private int[,] Data; public MagicSquare() { // // TODO: 在此处添加构造函数逻辑 // } public MagicSquare(int n) { if (n<=2) N=0; else { N=n; Data=new int[n,n]; Cal(); } } public void Cal() { if (N>1) { switch (N%4) { case 1: case 3: oddCreate(); break; case 0: doublyEven(); break; case 2: singleEven(); break; } } } /*奇数阶幻方采用Siamese 构造方法*/ private void oddCreate() { int i,j,Value,maxValue; i=0; j=N/2; Value=1; maxValue=N*N; while (Value<=maxValue) { Data[i,j]=Value; if (Value%N==0) i++; else { i--; j++; if (i<0) i+=N; if (j>=N) j-=N; } Value++; } } /* 4K阶幻方采用 如下方法 * 一次填充1-N*N,并将每个4X4小方格的两对角线上的数换成N×N+1-原来值 */ private void doublyEven() { int maxValue=N*N; int Value=1; for (int i=0;i<N;i++) { for (int j=0;j<N;j++) { int mI=i%4; int mJ=j%4; if ((mI==mJ)|| ((mI+mJ)==3)) Data[i,j]=maxValue+1-Value; else Data[i,j]=Value; Value++; } } } /* 4K+2 阶幻方 如下方法LUX方法 * 先变成2K+1奇数阶幻方, *前面K行采用L 即 * 4 1 * 2 3 *中间一行采用L,但最中间采用U 即 * 1 4 * 2 3 *K+2行采用U,但最中间采用L *其余K-1行采用X ,即 * 1 4 * 3 2 */ private void singleEven() { int K=N/4; int[,] a={{4,1,2,3},{1,4,2,3},{1,4,3,2}}; int i,j,T,Value,maxValue; i=0; j=K; T=2*K+1; Value=1; maxValue=T*T; while (Value<=maxValue) { int i2=i*2,j2=j*2,Value4=4*(Value -1),kind; if ((i<K)||((i==K)&&(j!=K)) || ((i==K+1)&&(j==K))) kind=0; else if ((i==K)&&(j==K) || ((i==K+1)&&(j!=K))) kind=1; else kind=2; Data[i2,j2]=Value4+a[kind,0]; Data[i2,j2+1]=Value4+a[kind,1]; Data[i2+1,j2]=Value4+a[kind,2]; Data[i2+1,j2+1]=Value4+a[kind,3]; if (Value%T==0) i++; else { i--; j++; if (i<0) i+=T; if (j>=T) j-=T; } Value++; } } public int[,] getResult() { return Data; } public string getString() { string str=""; str+=N.ToString ()+"阶幻方构造如下:\r\n"; for (int i=0;i<N;i++) { for(int j=0;j<N;j++) str+=Data[i,j].ToString ()+" "; str+="\r\n"; } return str; } } }
using System; using System.Text;namespace OnlineCalendar.InputCheck { /// <summary> /// Summary description for checkText. /// </summary> public sealed class checkText { public static string InputText(string inputString, int maxLength) { StringBuilder retVal = new StringBuilder(); // check incoming parameters for null or blank string if ((inputString != null) && (inputString != String.Empty)) { inputString = inputString.Trim(); //chop the string incase the client-side max length //fields are bypassed to prevent buffer over-runs if (inputString.Length > maxLength) inputString = inputString.Substring(0, maxLength); //convert some harmful symbols incase the regular //expression validators are changed for (int i = 0; i < inputString.Length; i++) { switch (inputString[i]) { case '"': retVal.Append(""); break; case '<': retVal.Append(""); break; case '>': retVal.Append(""); break; default: retVal.Append(inputString[i]); break; } } // Replace single quotes with white space retVal.Replace("'", " "); } return retVal.ToString(); } public checkText() { // // TODO: Add constructor logic here // } } }
tablename.select();
tablename.fiedname="value";
tablename.update();个人认为适用于频繁操作的表,逻辑比较清楚,操作方便,但是可能效率会有些低
using System.Text;
using System.Web.Security;namespace Hugo
{
/// <summary>
/// functions
/// </summary>
public class Functions
{
public Functions()
{
//
// TODO: Add constructor logic here
//
} /// <summary>
/// HTML encode
/// </summary>
/// <param name="str">string</param>
/// <returns></returns>
public static string Encode(string str)
{
str = str.Replace("&","&");
str = str.Replace("'","''");
str = str.Replace("\"",""");
str = str.Replace(" "," ");
str = str.Replace("<","<");
str = str.Replace(">",">");
str = str.Replace("\n","<br>");
return str;
}
/// <summary>
/// decode
/// </summary>
/// <param name="str">string</param>
/// <returns></returns>
public static string Decode(string str)
{
str = str.Replace("<br>","\n");
str = str.Replace(">",">");
str = str.Replace("<","<");
str = str.Replace(" "," ");
str = str.Replace(""","\"");
return str;
}
/// <summary>
/// encrypting string
/// </summary>
/// <returns></returns>
public static string Encrypt(string Password)
{
string str = "";
FormsAuthenticationTicket ticket = new System.Web.Security.FormsAuthenticationTicket(Password,true,2);
str = FormsAuthentication.Encrypt(ticket).ToString();
return str;
} /// <summary>
/// encrypting string
/// </summary>
/// <param name="Password">encrypting string</param>
/// <param name="Format">format,0 is SHA1,1 is MD5</param>
/// <returns></returns>
public static string Encrypt(string Password,int Format)
{
string str = "";
switch(Format)
{
case 0:
str = FormsAuthentication.HashPasswordForStoringInConfigFile(Password,"SHA1");
break;
case 1:
str = FormsAuthentication.HashPasswordForStoringInConfigFile(Password,"MD5");
break;
}
return str;
} /// <summary>
/// decrypt string
/// </summary>
/// <param name="Passowrd">encrypted string</param>
/// <returns></returns>
public static string Decrypt(string Passowrd)
{
string str="";
str= FormsAuthentication.Decrypt(Passowrd).Name.ToString();
return str;
}
/// <summary>
/// Encrypt Cookie
/// </summary>
/// <param name="str">Target cookie string</param>
/// <returns></returns>
public static string EncryptCookie(string strCookie,int type)
{
string str=En(strCookie,type);
StringBuilder sb = new StringBuilder();
foreach(char a in str)
{
sb.Append(Convert.ToString(a,16).PadLeft(4,'0'));
}
return sb.ToString();
}
/// <summary>
/// Decrypt Cookie
/// </summary>
/// <param name="str">Target cookie string</param>
/// <returns></returns>
public static string DecryptCookie(string strCookie,int type)
{
StringBuilder sb = new StringBuilder();
string [] strarr = new String[255];
int i,j,count=strCookie.Length/4;
string strTmp; for(i=0;i<count;i++)
{
for(j=0;j<4;j++)
{
sb.Append(strCookie.Substring(i*4+j,1));
}
strarr[i] = sb.ToString();
sb.Remove(0,sb.Length);
} for(i=0;i<count;i++)
{
strTmp = uint.Parse(strarr[i],System.Globalization.NumberStyles.AllowHexSpecifier).ToString("D");
char ch = (char)uint.Parse(strTmp);
sb.Append(ch);
} return De(sb.ToString(),type);
}
private static string En(string strCookie,int type)
{
string str;
if(type % 2==0)
{
str = Transform1(strCookie);
}
else
{
str = Transform3(strCookie);
}
str = Transform2(strCookie);
return str;
} private static string De(string strCookie,int type)
{
string str;
if(type % 2==0)
{
str = DeTransform1(strCookie);
}
else
{
str = DeTransform3(strCookie);
} str = Transform2(strCookie);
return str;
}
/// <summary>
/// methods 1
/// </summary>
/// <param name="str">string</param>
/// <returns>decrypted string</returns>
public static string DeTransform1(string str)
{
int i=0;
StringBuilder sb = new StringBuilder();
foreach(char a in str)
{
switch(i % 6)
{
case 0:
sb.Append((char)(a-1));
break;
case 1:
sb.Append((char)(a-5));
break;
case 2:
sb.Append((char)(a-7));
break;
case 3:
sb.Append((char)(a-2));
break;
case 4:
sb.Append((char)(a-4));
break;
case 5:
sb.Append((char)(a-9));
break;
}
i++;
} return sb.ToString();
} /// <summary>
/// method1
/// </summary>
/// <param name="str">string</param>
/// <returns>encrypted string</returns>
public static string Transform1(string str)
{
int i=0;
StringBuilder sb = new StringBuilder();
foreach(char a in str)
{
switch(i % 6)
{
case 0:
sb.Append((char)(a+1));
break;
case 1:
sb.Append((char)(a+5));
break;
case 2:
sb.Append((char)(a+7));
break;
case 3:
sb.Append((char)(a+2));
break;
case 4:
sb.Append((char)(a+4));
break;
case 5:
sb.Append((char)(a+9));
break;
}
i++;
} return sb.ToString();
} /// <summary>
/// method1
/// </summary>
/// <param name="str">string</param>
/// <returns>encrypted string</returns>
public static string Transform2(string str)
{
uint j=0;
StringBuilder sb = new StringBuilder();
str=Reverse(str);
foreach(char a in str)
{
j=a;
if(j>255)
{
j=(uint)((a>>8) + ((a&0x0ff)<<8));
}
else
{
j=(uint)((a>>4) + ((a&0x0f)<<4));
}
sb.Append((char)j);
} return sb.ToString();
} /// <summary>
/// methods 1
/// </summary>
/// <param name="str">string</param>
/// <returns>decrypted string</returns>
public static string DeTransform3(string str)
{
int i=0;
StringBuilder sb = new StringBuilder();
foreach(char a in str)
{
switch(i % 6)
{
case 0:
sb.Append((char)(a-3));
break;
case 1:
sb.Append((char)(a-6));
break;
case 2:
sb.Append((char)(a-8));
break;
case 3:
sb.Append((char)(a-7));
break;
case 4:
sb.Append((char)(a-5));
break;
case 5:
sb.Append((char)(a-2));
break;
}
i++;
} return sb.ToString();
} /// <summary>
/// method1
/// </summary>
/// <param name="str">string</param>
/// <returns>encrypted string</returns>
public static string Transform3(string str)
{
int i=0;
StringBuilder sb = new StringBuilder();
foreach(char a in str)
{
switch(i % 6)
{
case 0:
sb.Append((char)(a+3));
break;
case 1:
sb.Append((char)(a+6));
break;
case 2:
sb.Append((char)(a+8));
break;
case 3:
sb.Append((char)(a+7));
break;
case 4:
sb.Append((char)(a+5));
break;
case 5:
sb.Append((char)(a+2));
break;
}
i++;
} return sb.ToString();
} /// <summary>
/// reverse
/// </summary>
/// <param name="str">string</param>
/// <returns>reversed string</returns>
public static string Reverse(string str)
{
int i;
StringBuilder sb = new StringBuilder(); for(i=str.Length-1;i>=0;i--)
{
sb.Append(str[i]);
} return sb.ToString();
}
}
}
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration;namespace objLogin.myClass.Data
{
/// <summary>
/// DataAccessBase 的摘要说明:
/// 数据访问基类,封装了ADO.NET的各种对数据库的操作,直接访问数据库。
/// 所有访问数据库的类都有此类派生。
/// 是数据层的最下层实现。
/// </summary>
public class DataAccessBase
{
#region 属性 /// <summary>
/// 用来执行Sql语句的MyCommand,被MyCommand封装
/// </summary>
private OleDbCommand myCommand;
/// <summary>
/// 用来执行Sql语句的MyCommand,封装的myCommand
/// </summary>
protected OleDbCommand MyCommand
{
set
{
this.myCommand=value;
}
get
{
return this.myCommand;
}
} /// <summary>
/// 事务的MyTransaction
/// </summary>
private OleDbTransaction myTransaction;
private OleDbTransaction MyTransaction
{
set
{
this.myTransaction=value;
}
get
{
return this.myTransaction;
}
} //OleDbConnection连接对象
private OleDbConnection myConnection;
/// <summary>
/// 封装的连接串(this.myConnection.ConnectionString)
/// </summary>
protected string ConnectionString
{
set
{
this.myConnection.ConnectionString=value;
}
get
{
if(this.myConnection.ConnectionString==null)
return string.Empty;
else
return this.myConnection.ConnectionString;
}
} /// <summary>
/// Command执行的SQL语句字符串域,被CommandText封装
/// </summary>
private string commandText;
/// <summary>
/// Command执行的SQL语句字符串
/// 封装的commandText
/// </summary>
protected string CommandText
{
set
{
this.commandText=value;
}
get
{
if(this.commandText==null)
return string.Empty;
return this.commandText;
}
} /// <summary>
/// myConnection连接的状态
/// </summary>
protected string ConnectionState
{
get
{
return Convert.ToString(this.myConnection.State);
}
}
#endregion #region 构造函数
/// <summary>
/// 构造函数
/// </summary>
/// <param name="strConn">接收数据连接字符串</param>
public DataAccessBase(string strConn)
{
if(strConn==null | strConn.Length==0)
{
throw new Exception("DataAccessBase类的ConnectionString为空!");
}
else
{
this.CreateAttributeObject();
this.ConnectionString=strConn;
}
}
/// <summary>
/// 构造函数
/// </summary>
public DataAccessBase()
{
this.CreateAttributeObject();
} /// <summary>
/// 生成属性对象操作,
/// 包括myConnection和MyCommand
/// </summary>
private void CreateAttributeObject()
{
this.myConnection = new OleDbConnection();
this.MyCommand=this.myConnection.CreateCommand();
}
#endregion #region 方法 #region 获取状态的方法
/// <summary>
/// 打开连接
/// </summary>
protected bool OpenConnection()
{
// if(this.myConnection.State=="Closed"
try
{
this.myConnection.Open();
return true;
}
catch (OleDbException e)
{
this.ShowOleDbExceptionMessage(e);
return false;
}
} /// <summary>
/// 关闭连接
/// </summary>
protected bool CloseConnection()
{
try
{
this.myConnection.Close();
return true;
}
catch(OleDbException e)
{
this.ShowOleDbExceptionMessage(e);
return false;
}
}
#endregion #region 测试通过的方法 /// <summary>
/// 执行SQL语句 并返回DataTable
/// </summary>
/// <returns></returns> protected DataTable ExecuteSqlforDataTable()
{
this.EnsureCommandTextNotNull();
try
{
System.Data.OleDb.OleDbDataAdapter myCommand = new OleDbDataAdapter(this.CommandText, this.myConnection);
DataTable dt = new DataTable();
myCommand.Fill(dt);
return dt;
}
catch(OleDbException e)
{
this.ShowOleDbExceptionMessage(e);
}
return null;
}
/// <summary>
/// 执行类似insert delete等无返回值的Sql查询
/// </summary>
/// <returns>返回受影响的记录数</returns>
protected int ExecuteNonQuery()
{
this.EnsureCommandTextNotNull();
this.PrepareExecuteNonQueryTransaction();
return (this.ExecuteNonQueryTransaction());
}
#region 保留接口老方法
#endregion #endregion
/// <summary>
/// 执行strSql语句 返回DataReader
/// </summary>
protected OleDbDataReader ExecuteReader()
{
this.EnsureCommandTextNotNull();
this.MyCommand.CommandText=this.CommandText;
OleDbDataReader drd=null;
try
{
this.myConnection.Open();
drd=MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(OleDbException e)
{
this.ShowOleDbExceptionMessage(e);
}
return drd;
} #region 访问数据库的方法
/// <summary>
/// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略额外的列或行。
/// </summary>
protected object ExecuteScalar()
{
object ResultValue=null;
this.MyCommand.Connection.Open();
this.MyCommand.CommandText=this.CommandText;
try
{
ResultValue= myCommand.ExecuteScalar();
}
catch(OleDbException e)
{
throw new Exception(e.Message);
}
finally
{
this.MyCommand.Connection.Close();
}
return ResultValue;
}
#endregion #region 异常处理的方法
/// <summary>
/// 显示OleDbException接收到的异常信息
/// </summary>
protected void ShowOleDbExceptionMessage(OleDbException e)
{
string errorMessages = ""; for (int i=0; i < e.Errors.Count; i++)
{
errorMessages += "Index #" + i + "\n" +
"Message: " + e.Errors[i].Message + "\n" +
"NativeError: " + e.Errors[i].NativeError + "\n" +
"Source: " + e.Errors[i].Source + "\n" +
"SQLState: " + e.Errors[i].SQLState + "\n";
}
throw new Exception(errorMessages);
}
#endregion
#region 辅助函数
/// <summary>
/// 保证this.CommandText不为空,若为空抛出异常
/// </summary>
protected void EnsureCommandTextNotNull()
{
if(this.CommandText.Length==0)
{
throw new Exception("DataAccessBase.ExecuteSqlforDataTable(string)参数为空!");
}
} /// <summary>
/// 准备执行查询事务
/// </summary>
private void PrepareExecuteNonQueryTransaction()
{
this.CreateMyConnectionTransaction();
this.InitMyCommand();
} /// <summary>
/// 执行查询事务
/// </summary>
/// <returns>返回受影响的纪录数</returns>
private int ExecuteNonQueryTransaction()
{
int influenceRecordNumber=0;
try
{
influenceRecordNumber = myCommand.ExecuteNonQuery();
this.MyCommand.Transaction.Commit();
}
catch(Exception e)
{
try
{
this.MyCommand.Transaction.Rollback();
}
catch(OleDbException oleException)
{
this.ShowOleDbExceptionMessage(oleException);
}
throw new Exception(e.Message);
}
finally
{
this.MyCommand.Connection.Close();
}
return influenceRecordNumber;
} /// <summary>
/// 创建this.MyConnection的事务——this.MyTransaction
/// </summary>
private void CreateMyConnectionTransaction()
{
this.MyCommand.Connection.Open();//创建事务之前必须先打开连接
this.MyTransaction = this.myConnection.BeginTransaction();
} /// <summary>
/// 执行查询事务前初始化this.MyCommand
/// </summary>
private void InitMyCommand()
{
this.MyCommand.Transaction = this.MyTransaction;
this.MyCommand.CommandText = this.CommandText;
}
#endregion
#endregion
}
}
/************此版本已在Visual Studio.NET2003开发环境下测试***********************/
/************************文件名:ArrayOperator.Cs*****************************************/
/************************版本:Version 1.0***************************************/
/************************文件作者:CSTC Group Eben *****************************/
/************************创建日期:2004年3月10日*********************************/
/************************文件功能:数组列表操作** *******************************/
using System;
using System.Collections;namespace CSTCUtilitySharp
{
/// <summary>
/// ArrayOperator 的摘要说明。
/// </summary>
public class ArrayOperator
{
private Math EbenMath=new Math();
public ArrayOperator()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 是否已经有了相同的字段
/// </summary>
/// <param name="al">列表所有的选项</param>
/// <param name="Searchtext">现在的字符</param>
/// <returns>返回真假</returns>
public bool IsItem(ArrayList al,string Searchtext)
{
foreach(object a in al)
{
if(Searchtext==a.ToString())
{
return true;
}
}
return false;
}
/// <summary>
/// 计算列表数字的和
/// </summary>
/// <param name="al">列表</param>
/// <returns></returns>
public int sum(ArrayList al)
{
int sum=0;
if(al==null)
return sum;
foreach(object a in al)
{
if(EbenMath.IsNum(a.ToString()))
{
sum+=Convert.ToInt32(a.ToString(),10);
}
else
{
continue;
}
}
return sum;
} /// <summary>
/// 计算列表数字的和
/// </summary>
/// <param name="al">列表</param>
/// <returns></returns>
public double sumd(ArrayList al)
{
double sum=0.0;
if(al==null)
return sum;
foreach(object a in al)
{
if(EbenMath.IsNum(a.ToString()))
{
sum+=Convert.ToDouble(a.ToString());
}
else
{
continue;
}
}
return sum;
}
/// <summary>
/// 查找某个字符串在列表中
/// </summary>
/// <param name="al">列表所有的选项</param>
/// <param name="Searchtext">现在的字符</param>
/// <returns>返回所得第一个字符串</returns>
public static string FindItem(ArrayList al,string Searchtext)
{
string szTemp=Searchtext;
int i=-1;
foreach(object a in al)
{
i=a.ToString().IndexOf(Searchtext,0,a.ToString().Length);
if(i!=-1)
{
szTemp=a.ToString();
return szTemp;
}
} return szTemp;
}
}
}
{
f_strsrc=Microsoft.VisualBasic.Strings.StrConv(f_strsrc,Microsoft.VisualBasic.VbStrConv.TraditionalChinese,0);
f_strsrc=Encoding.GetEncoding(936).GetString(Encoding.Convert(Encoding.GetEncoding(936),Encoding.GetEncoding(950),Encoding.GetEncoding(936).GetBytes(f_strsrc)));
return f_strsrc;
} public static string b2g(string f_strsrc)
{
f_strsrc=Encoding.GetEncoding(936).GetString(Encoding.Convert(Encoding.GetEncoding(950),Encoding.GetEncoding(936),Encoding.GetEncoding(936).GetBytes(f_strsrc)));
f_strsrc=Microsoft.VisualBasic.Strings.StrConv(f_strsrc,Microsoft.VisualBasic.VbStrConv.SimplifiedChinese,0);
return f_strsrc;
}
public string DesEncrypt(string strText, string strEncrKey)
//解密字符串,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了) public string DesDecrypt(string strText,string sDecrKey) //加密数据文件,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了) public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey) //解密数据文件,注意strEncrKey的长度为8位(如果要增加或者减少key长度,调整IV的长度就是了) public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey) //MD5加密 public string MD5Encrypt(string strText) */
/******************************************************************************************
* Cryptography class is for Cryptography
author:Jim
e_mail:[email protected]
* thanks:
* URI:
*
*
*****************************************************************************************/
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Windows.Forms ; namespace Netbee.Classes.Security
{
/// <summary>
///Cryptography
/// </summary>
public class Cryptography
{
public Cryptography()
{ }
/// <summary>
/// Encrypt the string
/// Attention:key must be 8 bits
/// </summary>
/// <param name="strText">string</param>
/// <param name="strEncrKey">key</param>
/// <returns></returns>
public string DesEncrypt(string strText, string strEncrKey)
{
byte[] byKey=null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(System.Exception error)
{
MessageBox.Show(error.Message);
return "error:" +error.Message+"\r";
}
}
/// <summary>
/// Decrypt string
/// Attention:key must be 8 bits
/// </summary>
/// <param name="strText">Decrypt string</param>
/// <param name="sDecrKey">key</param>
/// <returns>output string</returns>
public string DesDecrypt(string strText,string sDecrKey)
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception error)
{
MessageBox.Show(error.Message);
return "error:"+error.Message+"\r";
}
}
/// <summary>
/// Encrypt files
/// Attention:key must be 8 bits
/// </summary>
/// <param name="m_InFilePath">Encrypt file path</param>
/// <param name="m_OutFilePath">output file</param>
/// <param name="strEncrKey">key</param>
public void DesEncrypt(string m_InFilePath,string m_OutFilePath,string strEncrKey)
{
byte[] byKey=null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8));
FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
} encStream.Close();
fout.Close();
fin.Close();
}
catch(System.Exception error)
{
MessageBox.Show(error.Message.ToString()); }
}
/// <summary>
/// Decrypt files
/// Attention:key must be 8 bits
/// </summary>
/// <param name="m_InFilePath">Decrypt filepath</param>
/// <param name="m_OutFilePath">output filepath</param>
/// <param name="sDecrKey">key</param>
public void DesDecrypt(string m_InFilePath,string m_OutFilePath,string sDecrKey)
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8));
FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
} encStream.Close();
fout.Close();
fin.Close(); }
catch(System.Exception error)
{
MessageBox.Show( "error:"+error.Message);
}
}
/// <summary>
/// MD5 Encrypt
/// </summary>
/// <param name="strText">text</param>
/// <returns>md5 Encrypt string</returns>
public string MD5Encrypt(string strText)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
return System.Text.Encoding.Default.GetString(result);
}
}
}
using System;namespace cs.jq
{
/// <summary>
/// MagicSquare 的摘要说明。
/// </summary>
public class MagicSquare
{
private int N;
private int[,] Data;
public MagicSquare()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public MagicSquare(int n)
{
if (n<=2)
N=0;
else
{
N=n;
Data=new int[n,n];
Cal();
}
} public void Cal()
{
if (N>1)
{
switch (N%4)
{
case 1:
case 3:
oddCreate();
break;
case 0:
doublyEven();
break;
case 2:
singleEven();
break;
}
}
} /*奇数阶幻方采用Siamese 构造方法*/
private void oddCreate()
{
int i,j,Value,maxValue;
i=0;
j=N/2;
Value=1;
maxValue=N*N; while (Value<=maxValue)
{
Data[i,j]=Value;
if (Value%N==0)
i++;
else
{
i--;
j++;
if (i<0)
i+=N;
if (j>=N)
j-=N; }
Value++;
}
} /* 4K阶幻方采用 如下方法
* 一次填充1-N*N,并将每个4X4小方格的两对角线上的数换成N×N+1-原来值
*/
private void doublyEven()
{
int maxValue=N*N;
int Value=1; for (int i=0;i<N;i++)
{
for (int j=0;j<N;j++)
{
int mI=i%4;
int mJ=j%4; if ((mI==mJ)|| ((mI+mJ)==3))
Data[i,j]=maxValue+1-Value;
else
Data[i,j]=Value;
Value++;
}
}
}
/* 4K+2 阶幻方 如下方法LUX方法
* 先变成2K+1奇数阶幻方,
*前面K行采用L 即
* 4 1
* 2 3
*中间一行采用L,但最中间采用U 即
* 1 4
* 2 3
*K+2行采用U,但最中间采用L
*其余K-1行采用X ,即
* 1 4
* 3 2
*/ private void singleEven()
{
int K=N/4;
int[,] a={{4,1,2,3},{1,4,2,3},{1,4,3,2}};
int i,j,T,Value,maxValue;
i=0;
j=K;
T=2*K+1;
Value=1;
maxValue=T*T; while (Value<=maxValue)
{
int i2=i*2,j2=j*2,Value4=4*(Value -1),kind; if ((i<K)||((i==K)&&(j!=K)) || ((i==K+1)&&(j==K)))
kind=0;
else if ((i==K)&&(j==K) || ((i==K+1)&&(j!=K)))
kind=1;
else
kind=2; Data[i2,j2]=Value4+a[kind,0];
Data[i2,j2+1]=Value4+a[kind,1];
Data[i2+1,j2]=Value4+a[kind,2];
Data[i2+1,j2+1]=Value4+a[kind,3]; if (Value%T==0)
i++;
else
{
i--;
j++;
if (i<0)
i+=T;
if (j>=T)
j-=T;
}
Value++;
}
} public int[,] getResult()
{
return Data;
} public string getString()
{
string str="";
str+=N.ToString ()+"阶幻方构造如下:\r\n";
for (int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
str+=Data[i,j].ToString ()+" ";
str+="\r\n";
}
return str;
} }
}
using System.Text;namespace OnlineCalendar.InputCheck
{
/// <summary>
/// Summary description for checkText.
/// </summary>
public sealed class checkText
{
public static string InputText(string inputString, int maxLength)
{
StringBuilder retVal = new StringBuilder();
// check incoming parameters for null or blank string
if ((inputString != null) && (inputString != String.Empty))
{
inputString = inputString.Trim(); //chop the string incase the client-side max length
//fields are bypassed to prevent buffer over-runs
if (inputString.Length > maxLength)
inputString = inputString.Substring(0, maxLength); //convert some harmful symbols incase the regular
//expression validators are changed
for (int i = 0; i < inputString.Length; i++)
{
switch (inputString[i])
{
case '"':
retVal.Append("");
break;
case '<':
retVal.Append("");
break;
case '>':
retVal.Append("");
break;
default:
retVal.Append(inputString[i]);
break;
}
} // Replace single quotes with white space
retVal.Replace("'", " ");
} return retVal.ToString();
}
public checkText()
{
//
// TODO: Add constructor logic here
//
}
}
}
WebDiyer/Lostinet