code_en_de.cs
上传用户:chizxy
上传日期:2014-11-29
资源大小:407k
文件大小:2k
- using System;
- using System.Text;
- namespace MKIms3
- {
- public class code_en_de
- {
- private static char[] key = "yA36zA48dEhfrvghGRg57h5UlDv3".ToCharArray();
-
- /// <summary>
- /// 字符串解密
- /// </summary>
- /// <param name="x">需要解密的字符</param>
- /// <returns></returns>
- public static string Decode(string x)
- {
- System.Text.StringBuilder sb = new System.Text.StringBuilder();
- char[] ca = x.ToCharArray();
- for (int i = 0; i < (ca.Length - 2); i += 2)
- {
- string s1 = "" + ca[i + 2] + ca[i + 3];
- string s2 = "" + System.Convert.ToString((byte) key[(i / 2) % 13], 16);
- string s3 = "" + ca[i] + ca[i + 1];
- int m = (System.Convert.ToInt32(s1, 16) ^ System.Convert.ToInt32(s2, 16)) - System.Convert.ToInt32(s3, 16);
- if (m < 0)
- {
- m += 255;
- }
- sb.Append((char) ((short) m));
- }
- return sb.ToString();
- }
- /// <summary>
- /// 字符串加密
- /// </summary>
- /// <param name="x">需要被加密的字符串</param>
- /// <returns></returns>
- public static string Encode(string x)
- {
- char[] ca = x.ToCharArray();
- int k = new System.Random().Next(255);
- System.Text.StringBuilder sb = null;
- if (k > 15)
- {
- sb = new System.Text.StringBuilder("" + System.Convert.ToString(k, 16));
- }
- else
- {
- sb = new System.Text.StringBuilder("0" + System.Convert.ToString(k, 16));
- }
- int m = System.Convert.ToInt32(sb.ToString(), 16);
- for (int i = 0; i < ca.Length; i++)
- {
- if ((ca[i] + m) > 255)
- {
- m = ((ca[i] + m) - 255) ^ key[i % 13];
- }
- else
- {
- m = (ca[i] + m) ^ key[i % 13];
- }
- if (m > 15)
- {
- sb.Append(System.Convert.ToString(m, 16));
- }
- else
- {
- sb.Append("0" + System.Convert.ToString(m, 16));
- }
- }
- return sb.ToString().ToUpper();
- }
- /// <summary>
- /// 将加密过的字符串解密后以数组的方式返回
- /// </summary>
- /// <param name="mima">需要解密的字符串,(本系统配制文件中读取出来的)</param>
- /// <returns>包含数据库连接信息的数组</returns>
- public static string[] get_char(string mima)
- {
- string[] chae = mima.Split('|');
- for(int i=0;i<chae.Length;i++)
- {
- chae[i]=code_en_de.Decode(chae[i]);
- }
- return chae;
- }
- }
- }