C# 简单实现对字符串加密和解密

发布时间 2023-09-26 20:34:44作者: soliang

 C# 简单实现对字符串加密和解密有两种方式,供参考:

 1 /// <summary>
 2 /// 编译字符串
 3 /// </summary>
 4 /// <param name="strUnicode"></param>
 5 /// <returns></returns>
 6 public static string FromUnicodeToAscii(string strUnicode)
 7 {
 8   
 9     try
10     {
11         if (String.IsNullOrEmpty(strUnicode))
12         {
13             return String.Empty;
14         }
15         UnicodeEncoding Unicode = new UnicodeEncoding();
16 
17         int byteCount = Unicode.GetByteCount(strUnicode);
18         byte[] origbytes = new byte[byteCount];
19         origbytes = Unicode.GetBytes(strUnicode);
20 
21         StringBuilder strDecoded = new StringBuilder();
22         for (int i = 0; i < byteCount; i++)
23         {
24             byte b = origbytes[i];
25             //translate each byte into two digits of hexa.
26             strDecoded.Append(b.ToString("X2"));
27             //chDest[i] =  Convert.ToChar(b);
28         }
29         return strDecoded.ToString();
30     }
31     catch (Exception err)
32     {
33         Console.WriteLine(err.Message+" "+err.StackTrace);             
34         return "*********异常";
35     }
36 }
37 /// <summary>
38 /// 反编译字符串
39 /// </summary>
40 /// <param name="strAscii"></param>
41 /// <returns></returns>
42 public static string FromAsciiToUnicode(string strAscii)
43 {
44 
45     try
46     {
47         if (String.IsNullOrEmpty(strAscii))
48         {
49             return String.Empty;
50         }
51         StringBuilder strNew = new StringBuilder();
52         UnicodeEncoding Unicode = new UnicodeEncoding();
53         byte[] currbytes = new byte[2];
54         for (int i = 0; i < strAscii.Length; i = i + 4)
55         {
56             for (int j = 0; j < 2; j++)
57             {
58                 //each two hexa digits are becoming one byte
59                 currbytes[j] = Convert.ToByte(strAscii.Substring(i + j * 2, 2), 16);
60             }
61             strNew.Append(Unicode.GetString(currbytes));
62         }
63         return strNew.ToString();
64     }
65     catch (Exception err)
66     {
67     
68        
69         return "******异常";
70     }
71 }
 1     /// <summary>
 2     /// 字符串简单加密
 3     /// </summary>
 4     public class EncodeAndDecode
 5     {
 6         /// <summary>
 7         /// Base64加密
 8         /// </summary>
 9         /// <param name="codeName">加密采用的编码方式</param>
10         /// <param name="source">待加密的明文</param>
11         /// <returns></returns>
12         public static string EncodeBase64(Encoding encode, string source)
13         {
14             string enstring = "";
15             byte[] bytes = encode.GetBytes(source);
16             try
17             {
18                 enstring = Convert.ToBase64String(bytes);
19             }
20             catch
21             {
22                 enstring = source;
23             }
24             return enstring;
25         }
26  
27         /// <summary>
28         /// Base64加密,采用utf8编码方式加密
29         /// </summary>
30         /// <param name="source">待加密的明文</param>
31         /// <returns>加密后的字符串</returns>
32         public static string EncodeBase64(string source)
33         {
34             return EncodeBase64(Encoding.UTF8, source);
35         }
36  
37         /// <summary>
38         /// Base64解密
39         /// </summary>
40         /// <param name="codeName">解密采用的编码方式,注意和加密时采用的方式一致</param>
41         /// <param name="result">待解密的密文</param>
42         /// <returns>解密后的字符串</returns>
43         public static string DecodeBase64(Encoding encode, string result)
44         {
45             string decode = "";
46             byte[] bytes = Convert.FromBase64String(result);
47             try
48             {
49                 decode = encode.GetString(bytes);
50             }
51             catch
52             {
53                 decode = result;
54             }
55             return decode;
56         }
57  
58         /// <summary>
59         /// Base64解密,采用utf8编码方式解密
60         /// </summary>
61         /// <param name="result">待解密的密文</param>
62         /// <returns>解密后的字符串</returns>
63         public static string DecodeBase64(string result)
64         {
65             return DecodeBase64(Encoding.UTF8, result);
66         }
67     }