问题如下,可以告诉我哪本书上有这样的详细介绍,把书名就,作者,出版社发过了。
急需。百度我找遍了,就是没看到。我确实是没分了。希望各位好人能成全我,我将万分感谢
加密解密中的basic64编码 是什么算法,我要详细的加密过程
答案:2 悬赏:0 手机版
解决时间 2021-02-03 15:35
- 提问者网友:两耳就是菩提
- 2021-02-02 15:41
最佳答案
- 五星知识达人网友:一叶十三刺
- 2021-02-02 16:02
1.using System;
2.using System.Collections.Generic;
3.using System.Text;
4.using System.Collections;
5.
6.namespace basic64
7.{
8. public class Basic64
9. {
10.
11.
12. private Hashtable hash = new Hashtable();
13. private Encoding encoding;
14. private char[] dataMapping ={
15. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
16. 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
17. 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
18. 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/',
19. '='
20. };
21.
22. public Basic64(Encoding encoding)
23. {
24. InitTable();
25. this.encoding = encoding;
26. }
27. public Basic64()
28. {
29. InitTable();
30.
31. this.encoding = Encoding.GetEncoding("gb2312");
32. }
33. private void InitTable()
34. {
35. // A-Z 0-25
36. for (int i = 0; i <= 25; i++)
37. {
38. string letter = "" + Convert.ToChar('A' + i);
39. hash.Add(letter, i);
40. }
41. for (int i = 26; i <= 51; i++)
42. {
43. string letter = "" + Convert.ToChar('a' + i - 26);
44. hash.Add(letter, i);
45. }
46. for (int i = 52; i <= 61; i++)
47. {
48. hash.Add("" + (i - 52), i);
49. }
50.
51. hash.Add("+", 62);
52. hash.Add("/", 63);
53. hash.Add("=", 64);
54. }
55.
56. ///
57. /// basic64编码
58. ///
59. ///
60. ///
61. public string ToBase64(string code)
62. {
63. byte[] bytes = this.encoding.GetBytes(code);
64. int size = (bytes.Length % 3 == 0) ? 3 : (bytes.Length % 3);
65. byte[] buffer = new byte[bytes.Length + (3 - size)];
66. string ret = "";
67. Buffer.BlockCopy(bytes, 0, buffer, 0, bytes.Length);
68. byte[] dBuffer = new byte[buffer.Length / 3 * 4];
69. int j = 0;
70. for (int i = 0; i < buffer.Length / 3; i++)
71. {
72. int q = i * 3;
73. dBuffer[j] = (byte)((buffer[q] >> 2) & 0x3f);
74. dBuffer[++j] = (byte)((((buffer[q] << 6) >> 2) & 0x3f) | (((buffer[q + 1] >> 4)) & 0x0f));
75. dBuffer[++j] = (byte)(((buffer[q + 1] << 4 >> 2) & 0x3f) | ((buffer[q + 2] >> 6) & 0x03));
76. dBuffer[++j] = (byte)((buffer[q + 2] << 2 >> 2) & 0x3f);
77. j++;
78. }
79. for (int i = 0; i < dBuffer.Length - (3 - size); i++)
80. {
81. ret += dataMapping[(int)dBuffer[i]] + "";
82. }
83. if ((3 - size) > 0)
84. {
85. for (int i = 0; i < (3 - size); i++)
86. ret += '=';
87. }
88. return ret;
89. }
90.
91.
92. ///
93. /// basic64解码
94. ///
95. ///
96. ///
97. public string FromBasic64(string code)
98. {
99. if (code.Length % 4 != 0) throw new Exception("不正确的Basic64编码长度!");
100. byte[] buffer = new byte[code.Length / 4 * 3];
101. int j = 0;
102. int padCount = findPadStr(code);
103. for (int i = 0; i < code.Length / 4; i++)
104. {
105. int q = i * 4;
106. int _a = Convert.ToInt32(hash[code[q].ToString()]);
107. int _b = Convert.ToInt32(hash[code[q + 1].ToString()]);
108. int _c = Convert.ToInt32(hash[code[q + 2].ToString()]);
109. int _d = Convert.ToInt32(hash[code[q + 3].ToString()]);
110.
111. string a = fill8bits(Convert.ToString(_a, 2)).Substring(2, 6);
112. string b = fill8bits(Convert.ToString(_b, 2)).Substring(2, 6);
113. string c = fill8bits(Convert.ToString(_c, 2)).Substring(2, 6);
114. string d = fill8bits(Convert.ToString(_d, 2)).Substring(2, 6);
115.
116. buffer[j] = Convert.ToByte(a + b.Substring(0, 2), 2);
117. buffer[++j] = Convert.ToByte(b.Substring(2, 4) + c.Substring(0, 4), 2);
118. buffer[++j] = Convert.ToByte((c.Substring(4, 2) + d), 2);
119. j++;
120. }
121. return this.encoding.GetString(buffer, 0, buffer.Length - padCount);
122. }
123. #region helper
124. private string fill8bits(string str)
125. {
126. int len = str.Length;
127. if (len < 8)
128. {
129. for (int i = 0; i < (8 - len); i++)
130. str = "0" + str;
131. }
132. return str;
133. }
134. private int findPadStr(string code)
135. {
136. int j = 0;
137. int len = code.Length;
138. for (int i = 0; i < 3; i++)
139. {
140. if (code.Substring(len - i - 1, 1).Equals("="))
141. {
142. j++;
143. }
144. }
145. return j;
146.
147. }
148. #endregion
149. }
150.}
2.using System.Collections.Generic;
3.using System.Text;
4.using System.Collections;
5.
6.namespace basic64
7.{
8. public class Basic64
9. {
10.
11.
12. private Hashtable hash = new Hashtable();
13. private Encoding encoding;
14. private char[] dataMapping ={
15. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
16. 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
17. 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
18. 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/',
19. '='
20. };
21.
22. public Basic64(Encoding encoding)
23. {
24. InitTable();
25. this.encoding = encoding;
26. }
27. public Basic64()
28. {
29. InitTable();
30.
31. this.encoding = Encoding.GetEncoding("gb2312");
32. }
33. private void InitTable()
34. {
35. // A-Z 0-25
36. for (int i = 0; i <= 25; i++)
37. {
38. string letter = "" + Convert.ToChar('A' + i);
39. hash.Add(letter, i);
40. }
41. for (int i = 26; i <= 51; i++)
42. {
43. string letter = "" + Convert.ToChar('a' + i - 26);
44. hash.Add(letter, i);
45. }
46. for (int i = 52; i <= 61; i++)
47. {
48. hash.Add("" + (i - 52), i);
49. }
50.
51. hash.Add("+", 62);
52. hash.Add("/", 63);
53. hash.Add("=", 64);
54. }
55.
56. ///
57. /// basic64编码
58. ///
59. ///
60. ///
61. public string ToBase64(string code)
62. {
63. byte[] bytes = this.encoding.GetBytes(code);
64. int size = (bytes.Length % 3 == 0) ? 3 : (bytes.Length % 3);
65. byte[] buffer = new byte[bytes.Length + (3 - size)];
66. string ret = "";
67. Buffer.BlockCopy(bytes, 0, buffer, 0, bytes.Length);
68. byte[] dBuffer = new byte[buffer.Length / 3 * 4];
69. int j = 0;
70. for (int i = 0; i < buffer.Length / 3; i++)
71. {
72. int q = i * 3;
73. dBuffer[j] = (byte)((buffer[q] >> 2) & 0x3f);
74. dBuffer[++j] = (byte)((((buffer[q] << 6) >> 2) & 0x3f) | (((buffer[q + 1] >> 4)) & 0x0f));
75. dBuffer[++j] = (byte)(((buffer[q + 1] << 4 >> 2) & 0x3f) | ((buffer[q + 2] >> 6) & 0x03));
76. dBuffer[++j] = (byte)((buffer[q + 2] << 2 >> 2) & 0x3f);
77. j++;
78. }
79. for (int i = 0; i < dBuffer.Length - (3 - size); i++)
80. {
81. ret += dataMapping[(int)dBuffer[i]] + "";
82. }
83. if ((3 - size) > 0)
84. {
85. for (int i = 0; i < (3 - size); i++)
86. ret += '=';
87. }
88. return ret;
89. }
90.
91.
92. ///
93. /// basic64解码
94. ///
95. ///
96. ///
97. public string FromBasic64(string code)
98. {
99. if (code.Length % 4 != 0) throw new Exception("不正确的Basic64编码长度!");
100. byte[] buffer = new byte[code.Length / 4 * 3];
101. int j = 0;
102. int padCount = findPadStr(code);
103. for (int i = 0; i < code.Length / 4; i++)
104. {
105. int q = i * 4;
106. int _a = Convert.ToInt32(hash[code[q].ToString()]);
107. int _b = Convert.ToInt32(hash[code[q + 1].ToString()]);
108. int _c = Convert.ToInt32(hash[code[q + 2].ToString()]);
109. int _d = Convert.ToInt32(hash[code[q + 3].ToString()]);
110.
111. string a = fill8bits(Convert.ToString(_a, 2)).Substring(2, 6);
112. string b = fill8bits(Convert.ToString(_b, 2)).Substring(2, 6);
113. string c = fill8bits(Convert.ToString(_c, 2)).Substring(2, 6);
114. string d = fill8bits(Convert.ToString(_d, 2)).Substring(2, 6);
115.
116. buffer[j] = Convert.ToByte(a + b.Substring(0, 2), 2);
117. buffer[++j] = Convert.ToByte(b.Substring(2, 4) + c.Substring(0, 4), 2);
118. buffer[++j] = Convert.ToByte((c.Substring(4, 2) + d), 2);
119. j++;
120. }
121. return this.encoding.GetString(buffer, 0, buffer.Length - padCount);
122. }
123. #region helper
124. private string fill8bits(string str)
125. {
126. int len = str.Length;
127. if (len < 8)
128. {
129. for (int i = 0; i < (8 - len); i++)
130. str = "0" + str;
131. }
132. return str;
133. }
134. private int findPadStr(string code)
135. {
136. int j = 0;
137. int len = code.Length;
138. for (int i = 0; i < 3; i++)
139. {
140. if (code.Substring(len - i - 1, 1).Equals("="))
141. {
142. j++;
143. }
144. }
145. return j;
146.
147. }
148. #endregion
149. }
150.}
全部回答
- 1楼网友:从此江山别
- 2021-02-02 17:11
可以私聊我~
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯