1 public class Base64 2 { 3 #region Base64加密 4 ///<summary> 5 ///Base64加密 6 ///</summary> 7 ///<paramname="Message"></param> 8 ///<returns></returns> 9 public static string Base64Code(string Message) 10 { 11 char[] Base64Code = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '=' }; 12 byte empty = (byte)0; 13 System.Collections.ArrayList byteMessage = new System.Collections.ArrayList(System.Text.Encoding.Default.GetBytes(Message)); 14 System.Text.StringBuilder outmessage; 15 int messageLen = byteMessage.Count; 16 //将字符分成3个字节一组,如果不足,则以0补齐 17 int page = messageLen / 3; 18 int use = 0; 19 if ((use = messageLen % 3) > 0) 20 { 21 for (int i = 0; i < 3 - use; i++) byteMessage.Add(empty); 22 page++; 23 } 24 //将3个字节的每组字符转换成4个字节一组的。3个一组,一组一组变成4个字节一组 25 //方法是:转换成ASCII码,按顺序排列24位数据,再把这24位数据分成4组,即每组6位。再在每组的的最高位前补两个0凑足一个字节。 26 outmessage = new System.Text.StringBuilder(page * 4); for (int i = 0; i < page; i++) 27 { 28 //取一组3个字节的组 29 byte[] instr = new byte[3]; instr[0] = (byte)byteMessage[i * 3]; instr[1] = (byte)byteMessage[i * 3 + 1]; instr[2] = (byte)byteMessage[i * 3 + 2]; 30 //六个位为一组,补0变成4个字节 31 int[] outstr = new int[4]; 32 //第一个输出字节:取第一输入字节的前6位,并且在高位补0,使其变成8位(一个字节) 33 outstr[0] = instr[0] >> 2; 34 //第二个输出字节:取第一输入字节的后2位和第二个输入字节的前4位(共6位),并且在高位补0,使其变成8位(一个字节) 35 outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >> 4); 36 //第三个输出字节:取第二输入字节的后4位和第三个输入字节的前2位(共6位),并且在高位补0,使其变成8位(一个字节) 37 if (!instr[1].Equals(empty)) outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >> 6); else outstr[2] = 64; 38 //第四个输出字节:取第三输入字节的后6位,并且在高位补0,使其变成8位(一个字节) 39 if (!instr[2].Equals(empty)) outstr[3] = (instr[2] & 0x3f); else outstr[3] = 64; outmessage.Append(Base64Code[outstr[0]]); 40 outmessage.Append(Base64Code[outstr[1]]); 41 outmessage.Append(Base64Code[outstr[2]]); 42 outmessage.Append(Base64Code[outstr[3]]); 43 } 44 return outmessage.ToString(); 45 } 46 #endregion Base64加密 47 #region Base64解密 48 ///<summary>///Base64解密 49 ///</summary> 50 ///<paramname="Message"></param> 51 ///<returns></returns> 52 public static string Base64Decode(string Message) 53 { 54 if ((Message.Length % 4) != 0) 55 { 56 throw new ArgumentException("不是正确的BASE64编码,请检查。", "Message"); 57 } 58 if (!System.Text.RegularExpressions.Regex.IsMatch(Message, "^[A-Z0-9/+=]*$", System.Text.RegularExpressions.RegexOptions.IgnoreCase)) 59 { 60 throw new ArgumentException("包含不正确的BASE64编码,请检查。", "Message"); 61 } 62 string Base64Code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 63 int page = Message.Length / 4; 64 System.Collections.ArrayList outMessage = new System.Collections.ArrayList(page * 3); 65 char[] message = Message.ToCharArray(); 66 for (int i = 0; i < page; i++) 67 { 68 byte[] instr = new byte[4]; 69 instr[0] = (byte)Base64Code.IndexOf(message[i * 4]); 70 instr[1] = (byte)Base64Code.IndexOf(message[i * 4 + 1]); 71 instr[2] = (byte)Base64Code.IndexOf(message[i * 4 + 2]); 72 instr[3] = (byte)Base64Code.IndexOf(message[i * 4 + 3]); 73 byte[] outstr = new byte[3]; 74 outstr[0] = (byte)((instr[0] << 2) ^ ((instr[1] & 0x30) >> 4)); 75 if (instr[2] != 64) 76 { 77 outstr[1] = (byte)((instr[1] << 4) ^ ((instr[2] & 0x3c) >> 2)); 78 } 79 else 80 { 81 outstr[2] = 0; 82 } 83 if (instr[3] != 64) 84 { 85 outstr[2] = (byte)((instr[2] << 6) ^ instr[3]); 86 } 87 else 88 { 89 outstr[2] = 0; 90 } 91 outMessage.Add(outstr[0]); 92 if (outstr[1] != 0) outMessage.Add(outstr[1]); 93 if (outstr[2] != 0) outMessage.Add(outstr[2]); 94 } 95 byte[] outbyte = (byte[])outMessage.ToArray(Type.GetType("System.Byte")); 96 return System.Text.Encoding.Default.GetString(outbyte); 97 } 98 #endregion Base64解密 99 #region RSA加密解密 100 /// <summary> 101 /// RSA加密 102 /// </summary> 103 /// <param name="publickey"></param> 104 /// <param name="content"></param> 105 /// <returns></returns> 106 public static string RSAEncrypt(string publickey, string content) 107 { 108 publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; 109 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 110 byte[] cipherbytes; 111 rsa.FromXmlString(publickey); 112 cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); 113 114 return Convert.ToBase64String(cipherbytes); 115 } 116 117 /// <summary> 118 /// RSA解密 119 /// </summary> 120 /// <param name="privatekey"></param> 121 /// <param name="content"></param> 122 /// <returns></returns> 123 public static string RSADecrypt(string privatekey, string content) 124 { 125 privatekey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>"; 126 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 127 byte[] cipherbytes; 128 rsa.FromXmlString(privatekey); 129 cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); 130 131 return Encoding.UTF8.GetString(cipherbytes); 132 } 133 #endregion RSA加密解密 134 135 }