房租管理小软件(六):通用功能包括时间,效验,MD5加密,XML 操作

1.时间相关

 1 public static DateTime getNow()
 2         {
 3             MyFZDataContext dataContext = MyFZDataContext.getDataContext();
 4             DateTime datetime = dataContext.ExecuteQuery<DateTime>("select getdate() as datetime1").Single();
 5             return datetime;
 6 
 7         }
 8         //public static DateTime getSystemNow() {
 9         //    MyFZDataContext dataContext = MyFZDataContext.getDataContext();
10         //    var v1 = from t in dataContext.T1_SystemTime
11         //              select new
12         //              {
13         //                  t.SystemTime
14         //              };
15 
16         //    return Convert.ToDateTime(v1.Single().SystemTime);
17         //}
18         public static DateTime getMonthFirstDay(DateTime dtInter)// 本月第一天
19         {
20             DateTime dt1 = new DateTime(dtInter.Year, dtInter.Month, 1);
21             return dt1;
22         }
23 
24         public static DateTime getMonthLastDay(DateTime dtInter)// 本月最后一天
25         {
26             DateTime dt1 = new DateTime(dtInter.Year, dtInter.Month, 1);
27             dt1 = dt1.AddMonths(1).AddSeconds(-1);
28             return dt1;
29         }
30         public static DateTime getNextMonthFirstDay(DateTime dtInter)// 下月第一天
31         {
32             DateTime dt1 = new DateTime(dtInter.Year, dtInter.Month, 1);
33             dt1 = dt1.AddMonths(1);
34             return dt1;
35         }
36 
37         public static int getCountMonthDay(DateTime dtInter)//本月一共多少天
38         {
39             DateTime dt1 = new DateTime(dtInter.Year, dtInter.Month, 1);
40             DateTime dt2 = dt1.AddMonths(1);//下月第一天
41             return (dt2 - dt1).Days;
42         }
43         public static int getLastMonthDay(DateTime dtInter) // 从输入时间算起,还有多少天
44         {
45             DateTime dt1 = new DateTime(dtInter.Year, dtInter.Month, dtInter.Day);//今天
46             DateTime dtd = new DateTime(dtInter.Year, dtInter.Month, 1);// 本月第一天
47             DateTime dtF = dtd.AddMonths(1);// 下月第一天
48             return (dtF - dt1).Days;
49         }
50         public static int getPassMonthDay(DateTime dtInter)// 1号要现在一共多少天
51         {
52             // 经过多少天
53             DateTime dt1 = new DateTime(dtInter.Year, dtInter.Month, 1);// 
54             return (dtInter - dt1).Days;
55 
56         }
57 
58         public static String getYearMonthString(DateTime dst)
59         {
60             String year = dst.Year.ToString();
61             String month = dst.Month.ToString();
62             if (month.Length == 1) { month = "0" + month; }
63             return year + month;
64         }
65 
66         public static String getNextYearMonthString(DateTime dst)
67         {
68             DateTime dst1 = dst.AddMonths(1);
69             String year = dst1.Year.ToString();
70             String month = dst1.Month.ToString();
71             if (month.Length == 1) { month = "0" + month; }
72             return year + month;
73         }
74         public static DateTime getFirstDate(string YearMonth)
75         {
76             string year = YearMonth.Substring(0, 4);
77             string month = YearMonth.Substring(4, 2);
78             return DateTime.Parse(year + "-" + month + "-" + "01");
79         }
View Code

 

2.效验

 1  public void Validate_EmptyStringRule(BaseEdit control, DevExpress.XtraEditors.DXErrorProvider.DXErrorProvider dxErrorProvider1) 
 2         {
 3             if (control.EditValue == null || control.Text.Trim().Length == 0)
 4             {
 5                 dxErrorProvider1.SetError(control, "不能为空!",ErrorType.Default);
 6 
 7                 
 8                 control.Focus();
 9                 throw new Exception("值不能为空!");
10             }
11             else
12             {
13                 dxErrorProvider1.SetError(control, "");
14             }
15         }
16 
17 
18         public void Validate_EqualRule(BaseEdit control1,BaseEdit control2, DXErrorProvider dxErrorProvider1)
19         {
20             if (!control1.EditValue.Equals(control2.EditValue))
21             {
22                 dxErrorProvider1.SetError(control1, "两次输入的密码不一样!", ErrorType.Default);
23                 dxErrorProvider1.SetError(control2, "两次输入的密码不一样!", ErrorType.Default);
24                 control1.Focus();
25                 throw new Exception("两次输入的密码不一样!");
26             }
27             else
28             {
29                 dxErrorProvider1.SetError(control1, "");
30                 dxErrorProvider1.SetError(control2, "");
31             }
32         }
View Code

 

3.MD5

  1  public class MD5Encryption
  2     {
  3         //申明变量
  4         private static UInt32 A;
  5         private static UInt32 B;
  6         private static UInt32 C;
  7         private static UInt32 D;
  8 
  9         //number of bits to rotate in tranforming 
 10         private const int S11 = 7;
 11         private const int S12 = 12;
 12         private const int S13 = 17;
 13         private const int S14 = 22;
 14         private const int S21 = 5;
 15         private const int S22 = 9;
 16         private const int S23 = 14;
 17         private const int S24 = 20;
 18         private const int S31 = 4;
 19         private const int S32 = 11;
 20         private const int S33 = 16;
 21         private const int S34 = 23;
 22         private const int S41 = 6;
 23         private const int S42 = 10;
 24         private const int S43 = 15;
 25         private const int S44 = 21;
 26 
 27 
 28         /* F, G, H and I are basic MD5 functions. 
 29         * 四个非线性函数: 
 30         * 
 31         * F(X,Y,Z) =(X&Y)|((~X)&Z) 
 32         * G(X,Y,Z) =(X&Z)|(Y&(~Z)) 
 33         * H(X,Y,Z) =X^Y^Z 
 34         * I(X,Y,Z)=Y^(X|(~Z)) 
 35         * 
 36         * (&与,|或,~非,^异或) 
 37         */
 38         private static UInt32 F(UInt32 x, UInt32 y, UInt32 z)
 39         {
 40             return (x & y) | ((~x) & z);
 41         }
 42         private static UInt32 G(UInt32 x, UInt32 y, UInt32 z)
 43         {
 44             return (x & z) | (y & (~z));
 45         }
 46         private static UInt32 H(UInt32 x, UInt32 y, UInt32 z)
 47         {
 48             return x ^ y ^ z;
 49         }
 50         private static UInt32 I(UInt32 x, UInt32 y, UInt32 z)
 51         {
 52             return y ^ (x | (~z));
 53         }
 54 
 55         /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 
 56         * Rotation is separate from addition to prevent recomputation. 
 57         */
 58         private static void FF(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
 59         {
 60             a = a + F(b, c, d) + mj + ti;
 61             a = a << s | a >> (32 - s);
 62             a += b;
 63         }
 64         private static void GG(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
 65         {
 66             a = a + G(b, c, d) + mj + ti;
 67             a = a << s | a >> (32 - s);
 68             a += b;
 69         }
 70         private static void HH(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
 71         {
 72             a = a + H(b, c, d) + mj + ti;
 73             a = a << s | a >> (32 - s);
 74             a += b;
 75         }
 76         private static void II(ref UInt32 a, UInt32 b, UInt32 c, UInt32 d, UInt32 mj, int s, UInt32 ti)
 77         {
 78             a = a + I(b, c, d) + mj + ti;
 79             a = a << s | a >> (32 - s);
 80             a += b;
 81         }
 82 
 83         private static void MD5_Init()
 84         {
 85             A = 0x67452301; //in memory, this is 0x01234567 
 86             B = 0xefcdab89; //in memory, this is 0x89abcdef 
 87             C = 0x98badcfe; //in memory, this is 0xfedcba98 
 88             D = 0x10325476; //in memory, this is 0x76543210 
 89         }
 90 
 91         private static UInt32[] MD5_Append(byte[] input)
 92         {
 93             int zeros = 0;
 94             int ones = 1;
 95             int size = 0;
 96             int n = input.Length;
 97             int m = n % 64;
 98             if (m < 56)
 99             {
100                 zeros = 55 - m;
101                 size = n - m + 64;
102             }
103             else if (m == 56)
104             {
105                 zeros = 0;
106                 ones = 0;
107                 size = n + 8;
108             }
109             else
110             {
111                 zeros = 63 - m + 56;
112                 size = n + 64 - m + 64;
113             }
114 
115             ArrayList bs = new ArrayList(input);
116             if (ones == 1)
117             {
118                 bs.Add((byte)0x80); // 0x80 = $10000000 
119             }
120             for (int i = 0; i < zeros; i++)
121             {
122                 bs.Add((byte)0);
123             }
124 
125             UInt64 N = (UInt64)n * 8;
126             byte h1 = (byte)(N & 0xFF);
127             byte h2 = (byte)((N >> 8) & 0xFF);
128             byte h3 = (byte)((N >> 16) & 0xFF);
129             byte h4 = (byte)((N >> 24) & 0xFF);
130             byte h5 = (byte)((N >> 32) & 0xFF);
131             byte h6 = (byte)((N >> 40) & 0xFF);
132             byte h7 = (byte)((N >> 48) & 0xFF);
133             byte h8 = (byte)(N >> 56);
134             bs.Add(h1);
135             bs.Add(h2);
136             bs.Add(h3);
137             bs.Add(h4);
138             bs.Add(h5);
139             bs.Add(h6);
140             bs.Add(h7);
141             bs.Add(h8);
142             byte[] ts = (byte[])bs.ToArray(typeof(byte));
143 
144             /* Decodes input (byte[]) into output (UInt32[]). Assumes len is 
145             * a multiple of 4. 
146             */
147             UInt32[] output = new UInt32[size / 4];
148             for (Int64 i = 0, j = 0; i < size; j++, i += 4)
149             {
150                 output[j] = (UInt32)(ts[i] | ts[i + 1] << 8 | ts[i + 2] << 16 | ts[i + 3] << 24);
151             }
152             return output;
153         }
154         private static UInt32[] MD5_Trasform(UInt32[] x)
155         {
156 
157             UInt32 a, b, c, d;
158 
159             for (int k = 0; k < x.Length; k += 16)
160             {
161                 a = A;
162                 b = B;
163                 c = C;
164                 d = D;
165 
166                 /* Round 1 */
167                 FF(ref a, b, c, d, x[k + 0], S11, 0xd76aa478); /* 1 */
168                 FF(ref d, a, b, c, x[k + 1], S12, 0xe8c7b756); /* 2 */
169                 FF(ref c, d, a, b, x[k + 2], S13, 0x242070db); /* 3 */
170                 FF(ref b, c, d, a, x[k + 3], S14, 0xc1bdceee); /* 4 */
171                 FF(ref a, b, c, d, x[k + 4], S11, 0xf57c0faf); /* 5 */
172                 FF(ref d, a, b, c, x[k + 5], S12, 0x4787c62a); /* 6 */
173                 FF(ref c, d, a, b, x[k + 6], S13, 0xa8304613); /* 7 */
174                 FF(ref b, c, d, a, x[k + 7], S14, 0xfd469501); /* 8 */
175                 FF(ref a, b, c, d, x[k + 8], S11, 0x698098d8); /* 9 */
176                 FF(ref d, a, b, c, x[k + 9], S12, 0x8b44f7af); /* 10 */
177                 FF(ref c, d, a, b, x[k + 10], S13, 0xffff5bb1); /* 11 */
178                 FF(ref b, c, d, a, x[k + 11], S14, 0x895cd7be); /* 12 */
179                 FF(ref a, b, c, d, x[k + 12], S11, 0x6b901122); /* 13 */
180                 FF(ref d, a, b, c, x[k + 13], S12, 0xfd987193); /* 14 */
181                 FF(ref c, d, a, b, x[k + 14], S13, 0xa679438e); /* 15 */
182                 FF(ref b, c, d, a, x[k + 15], S14, 0x49b40821); /* 16 */
183 
184                 /* Round 2 */
185                 GG(ref a, b, c, d, x[k + 1], S21, 0xf61e2562); /* 17 */
186                 GG(ref d, a, b, c, x[k + 6], S22, 0xc040b340); /* 18 */
187                 GG(ref c, d, a, b, x[k + 11], S23, 0x265e5a51); /* 19 */
188                 GG(ref b, c, d, a, x[k + 0], S24, 0xe9b6c7aa); /* 20 */
189                 GG(ref a, b, c, d, x[k + 5], S21, 0xd62f105d); /* 21 */
190                 GG(ref d, a, b, c, x[k + 10], S22, 0x2441453); /* 22 */
191                 GG(ref c, d, a, b, x[k + 15], S23, 0xd8a1e681); /* 23 */
192                 GG(ref b, c, d, a, x[k + 4], S24, 0xe7d3fbc8); /* 24 */
193                 GG(ref a, b, c, d, x[k + 9], S21, 0x21e1cde6); /* 25 */
194                 GG(ref d, a, b, c, x[k + 14], S22, 0xc33707d6); /* 26 */
195                 GG(ref c, d, a, b, x[k + 3], S23, 0xf4d50d87); /* 27 */
196                 GG(ref b, c, d, a, x[k + 8], S24, 0x455a14ed); /* 28 */
197                 GG(ref a, b, c, d, x[k + 13], S21, 0xa9e3e905); /* 29 */
198                 GG(ref d, a, b, c, x[k + 2], S22, 0xfcefa3f8); /* 30 */
199                 GG(ref c, d, a, b, x[k + 7], S23, 0x676f02d9); /* 31 */
200                 GG(ref b, c, d, a, x[k + 12], S24, 0x8d2a4c8a); /* 32 */
201 
202                 /* Round 3 */
203                 HH(ref a, b, c, d, x[k + 5], S31, 0xfffa3942); /* 33 */
204                 HH(ref d, a, b, c, x[k + 8], S32, 0x8771f681); /* 34 */
205                 HH(ref c, d, a, b, x[k + 11], S33, 0x6d9d6122); /* 35 */
206                 HH(ref b, c, d, a, x[k + 14], S34, 0xfde5380c); /* 36 */
207                 HH(ref a, b, c, d, x[k + 1], S31, 0xa4beea44); /* 37 */
208                 HH(ref d, a, b, c, x[k + 4], S32, 0x4bdecfa9); /* 38 */
209                 HH(ref c, d, a, b, x[k + 7], S33, 0xf6bb4b60); /* 39 */
210                 HH(ref b, c, d, a, x[k + 10], S34, 0xbebfbc70); /* 40 */
211                 HH(ref a, b, c, d, x[k + 13], S31, 0x289b7ec6); /* 41 */
212                 HH(ref d, a, b, c, x[k + 0], S32, 0xeaa127fa); /* 42 */
213                 HH(ref c, d, a, b, x[k + 3], S33, 0xd4ef3085); /* 43 */
214                 HH(ref b, c, d, a, x[k + 6], S34, 0x4881d05); /* 44 */
215                 HH(ref a, b, c, d, x[k + 9], S31, 0xd9d4d039); /* 45 */
216                 HH(ref d, a, b, c, x[k + 12], S32, 0xe6db99e5); /* 46 */
217                 HH(ref c, d, a, b, x[k + 15], S33, 0x1fa27cf8); /* 47 */
218                 HH(ref b, c, d, a, x[k + 2], S34, 0xc4ac5665); /* 48 */
219 
220                 /* Round 4 */
221                 II(ref a, b, c, d, x[k + 0], S41, 0xf4292244); /* 49 */
222                 II(ref d, a, b, c, x[k + 7], S42, 0x432aff97); /* 50 */
223                 II(ref c, d, a, b, x[k + 14], S43, 0xab9423a7); /* 51 */
224                 II(ref b, c, d, a, x[k + 5], S44, 0xfc93a039); /* 52 */
225                 II(ref a, b, c, d, x[k + 12], S41, 0x655b59c3); /* 53 */
226                 II(ref d, a, b, c, x[k + 3], S42, 0x8f0ccc92); /* 54 */
227                 II(ref c, d, a, b, x[k + 10], S43, 0xffeff47d); /* 55 */
228                 II(ref b, c, d, a, x[k + 1], S44, 0x85845dd1); /* 56 */
229                 II(ref a, b, c, d, x[k + 8], S41, 0x6fa87e4f); /* 57 */
230                 II(ref d, a, b, c, x[k + 15], S42, 0xfe2ce6e0); /* 58 */
231                 II(ref c, d, a, b, x[k + 6], S43, 0xa3014314); /* 59 */
232                 II(ref b, c, d, a, x[k + 13], S44, 0x4e0811a1); /* 60 */
233                 II(ref a, b, c, d, x[k + 4], S41, 0xf7537e82); /* 61 */
234                 II(ref d, a, b, c, x[k + 11], S42, 0xbd3af235); /* 62 */
235                 II(ref c, d, a, b, x[k + 2], S43, 0x2ad7d2bb); /* 63 */
236                 II(ref b, c, d, a, x[k + 9], S44, 0xeb86d391); /* 64 */
237 
238                 A += a;
239                 B += b;
240                 C += c;
241                 D += d;
242             }
243             return new UInt32[] { A, B, C, D };
244         }
245         public static byte[] MD5Array(byte[] input)
246         {
247             MD5_Init();
248             UInt32[] block = MD5_Append(input);
249             UInt32[] bits = MD5_Trasform(block);
250 
251             /* Encodes bits (UInt32[]) into output (byte[]). Assumes len is 
252             * a multiple of 4. 
253             */
254             byte[] output = new byte[bits.Length * 4];
255             for (int i = 0, j = 0; i < bits.Length; i++, j += 4)
256             {
257                 output[j] = (byte)(bits[i] & 0xff);
258                 output[j + 1] = (byte)((bits[i] >> 8) & 0xff);
259                 output[j + 2] = (byte)((bits[i] >> 16) & 0xff);
260                 output[j + 3] = (byte)((bits[i] >> 24) & 0xff);
261             }
262             return output;
263         }
264 
265         public static string ArrayToHexString(byte[] array, bool uppercase)
266         {
267             string hexString = "";
268             string format = "x2";
269             if (uppercase)
270             {
271                 format = "X2";
272             }
273             foreach (byte b in array)
274             {
275                 hexString += b.ToString(format);
276             }
277             return hexString;
278         }
279 
280         public static string MDString(string message)
281         {
282             char[] c = message.ToCharArray();
283             byte[] b = new byte[c.Length];
284             for (int i = 0; i < c.Length; i++)
285             {
286                 b[i] = (byte)c[i];
287             }
288             byte[] digest = MD5Array(b);
289             return ArrayToHexString(digest, false);
290         }
291         public static string MDFile(string fileName)
292         {
293             FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read);
294             byte[] array = new byte[fs.Length];
295             fs.Read(array, 0, (int)fs.Length);
296             byte[] digest = MD5Array(array);
297             fs.Close();
298             return ArrayToHexString(digest, false);
299         }
300 
301         public static string Test(string message)
302         {
303             return "rnMD5 (" + message + ") = " + MD5Encryption.MDString(message);
304         }
305         public static string TestSuite()
306         {
307             string s = "";
308             s += Test("");
309             s += Test("a");
310             s += Test("abc");
311             s += Test("message digest");
312             s += Test("abcdefghijklmnopqrstuvwxyz");
313             s += Test("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
314             s += Test("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
315             return s;
316         }
317     }
318 }
View Code

 

4.xml

 1 public void Serialize(Setting setting)  
 2       {  
 3       using (FileStream fs = new FileStream(strFile, FileMode.Create))  
 4       {  
 5       XmlSerializer formatter = new XmlSerializer(typeof(Setting));  
 6       formatter.Serialize(fs, setting);  
 7       }  
 8       }  
 9 
10       public Setting DeSerialize()  
11       {  
12 
13 
14       Setting setting;  
15       using (FileStream fs = new FileStream(strFile, FileMode.Open))  
16       {  
17       XmlSerializer formatter = new XmlSerializer(typeof(Setting));  
18       setting = (Setting)formatter.Deserialize(fs);  
19       }  
20       return setting;  
21       }  
22       }
View Code

 

posted @ 2013-07-14 13:59  xiajing12345  阅读(501)  评论(0编辑  收藏  举报