C#幫助類:Base64
阿新 • • 發佈:2019-01-05
pca omx ldo convert encrypt count decrypt iss 幫助
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 }
C#幫助類:Base64