javascript C# 兼容的DES Base64加密/解密方法 整理

1.C# 实现 

 

C#版DES加解密方法及相关函数
  1  #region  C#版DES加解密方法及相关函数
  2 
  3         /// <summary>
  4         /// DES 加密
  5         /// </summary>
  6         /// <param name="beinetstr">待加密的字符串</param>
  7         /// <param name="beinetkey">密钥</param>
  8         /// <returns></returns>
  9         public string EncryptDES(string beinetstr, string beinetkey)
 10         {
 11             if (string.IsNullOrEmpty(beinetkey))
 12                 return string.Empty;
 13 
 14             return stringToHex(des(beinetkey, beinetstr, true, false, string.Empty));
 15         }
 16 
 17         /// <summary>
 18         /// DES 解密
 19         /// </summary>
 20         /// <param name="beinetstr">待解密的字符串</param>
 21         /// <param name="beinetkey">密钥</param>
 22         /// <returns></returns>
 23         public string DecryptDES(string beinetstr, string beinetkey)
 24         {
 25             if (string.IsNullOrEmpty(beinetkey))
 26                 return null;
 27             string ret = des(beinetkey, HexTostring(beinetstr), false, false, string.Empty);
 28             return ret;
 29         }
 30 
 31         /// <summary>
 32         /// 把字符串转换为16进制字符串
 33         /// 如:a变成61(即10进制的97);abc变成616263
 34         /// </summary>
 35         /// <param name="s"></param>
 36         /// <returns></returns>
 37         public string stringToHex(string s)
 38         {
 39             string r = "";
 40             string[] hexes = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
 41             for (int i = 0; i < (s.Length); i++)
 42             {
 43                 r += hexes[RM(s[i], 4)] + hexes[s[i] & 0xf];
 44             }
 45             return r;
 46         }
 47 
 48         /// <summary>
 49         /// 16进制字符串转换为字符串
 50         /// 如:61(即10进制的97)变成a;616263变成abc
 51         /// </summary>
 52         /// <param name="s"></param>
 53         /// <returns></returns>
 54         public  string HexTostring(string s)
 55         {
 56             string ret = string.Empty;
 57 
 58             for (int i = 0; i < s.Length; i += 2)
 59             {
 60                 int sxx = Convert.ToInt32(s.Substring(i, 2), 16);
 61                 ret += (char)sxx;
 62             }
 63             return ret;
 64         }
 65 
 66         /// <summary>
 67         /// 带符号位右移(类似于js的>>>)
 68         /// </summary>
 69         /// <param name="a">用于右移的操作数</param>
 70         /// <param name="bit">右移位数</param>
 71         /// <returns></returns>
 72         public  int RM(int a, int bit)
 73         {
 74             unchecked
 75             {
 76                 uint b = (uint)a;
 77                 b = b >> bit;
 78                 return (int)b;
 79             }
 80         }
 81 
 82         /// <summary>
 83         /// 加解密主调方法
 84         /// </summary>
 85         /// <param name="beinetkey">密钥</param>
 86         /// <param name="message">加密时为string,解密时为byte[]</param>
 87         /// <param name="encrypt">true:加密;false:解密</param>
 88         /// <param name="mode">true:CBC mode;false:非CBC mode</param>
 89         /// <param name="iv">初始化向量</param>
 90         /// <returns></returns>
 91         private string des(string beinetkey, string message, bool encrypt, bool mode, string iv)
 92         {
 93             //declaring this locally speeds things up a bit
 94             long[] spfunction1 = { 0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 0x4, 0x10000, 0x400, 0x1010400, 0x1010404, 0x400, 0x1000404, 0x1010004, 0x1000000, 0x4, 0x404, 0x1000400, 0x1000400, 0x10400, 0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 0x404, 0x10404, 0x1000000, 0x10000, 0x1010404, 0x4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 0x400, 0x1010004, 0x10000, 0x10400, 0x1000004, 0x400, 0x4, 0x1000404, 0x10404, 0x1010404, 0x10004, 0x1010000, 0x1000404, 0x1000004, 0x404, 0x10404, 0x1010400, 0x404, 0x1000400, 0x1000400, 0, 0x10004, 0x10400, 0, 0x1010004 };
 95             long[] spfunction2 = { -0x7fef7fe0, -0x7fff8000, 0x8000, 0x108020, 0x100000, 0x20, -0x7fefffe0, -0x7fff7fe0, -0x7fffffe0, -0x7fef7fe0, -0x7fef8000, -0x80000000, -0x7fff8000, 0x100000, 0x20, -0x7fefffe0, 0x108000, 0x100020, -0x7fff7fe0, 0, -0x80000000, 0x8000, 0x108020, -0x7ff00000, 0x100020, -0x7fffffe0, 0, 0x108000, 0x8020, -0x7fef8000, -0x7ff00000, 0x8020, 0, 0x108020, -0x7fefffe0, 0x100000, -0x7fff7fe0, -0x7ff00000, -0x7fef8000, 0x8000, -0x7ff00000, -0x7fff8000, 0x20, -0x7fef7fe0, 0x108020, 0x20, 0x8000, -0x80000000, 0x8020, -0x7fef8000, 0x100000, -0x7fffffe0, 0x100020, -0x7fff7fe0, -0x7fffffe0, 0x100020, 0x108000, 0, -0x7fff8000, 0x8020, -0x80000000, -0x7fefffe0, -0x7fef7fe0, 0x108000 };
 96             long[] spfunction3 = { 0x208, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008, 0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 0x208, 0x8000000, 0x8, 0x8020200, 0x200, 0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 0x8, 0x8020208, 0x200, 0x8000000, 0x8020200, 0x8000000, 0x20008, 0x208, 0x20000, 0x8020200, 0x8000200, 0, 0x200, 0x20008, 0x8020208, 0x8000200, 0x8000008, 0x200, 0, 0x8020008, 0x8000208, 0x20000, 0x8000000, 0x8020208, 0x8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 0x208, 0x8020000, 0x20208, 0x8, 0x8020008, 0x20200 };
 97             long[] spfunction4 = { 0x802001, 0x2081, 0x2081, 0x80, 0x802080, 0x800081, 0x800001, 0x2001, 0, 0x802000, 0x802000, 0x802081, 0x81, 0, 0x800080, 0x800001, 0x1, 0x2000, 0x800000, 0x802001, 0x80, 0x800000, 0x2001, 0x2080, 0x800081, 0x1, 0x2080, 0x800080, 0x2000, 0x802080, 0x802081, 0x81, 0x800080, 0x800001, 0x802000, 0x802081, 0x81, 0, 0, 0x802000, 0x2080, 0x800080, 0x800081, 0x1, 0x802001, 0x2081, 0x2081, 0x80, 0x802081, 0x81, 0x1, 0x2000, 0x800001, 0x2001, 0x802080, 0x800081, 0x2001, 0x2080, 0x800000, 0x802001, 0x80, 0x800000, 0x2000, 0x802080 };
 98             long[] spfunction5 = { 0x100, 0x2080100, 0x2080000, 0x42000100, 0x80000, 0x100, 0x40000000, 0x2080000, 0x40080100, 0x80000, 0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 0x42000000, 0x80100, 0x80000, 0x42000100, 0x100, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100, 0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 0x100, 0x2000000, 0x42080000, 0x42080100, 0x80100, 0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 0, 0x40080000, 0x2080100, 0x40000100 };
 99             long[] spfunction6 = { 0x20000010, 0x20400000, 0x4000, 0x20404010, 0x20400000, 0x10, 0x20404010, 0x400000, 0x20004000, 0x404010, 0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 0x4010, 0, 0x400010, 0x20004010, 0x4000, 0x404000, 0x20004010, 0x10, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 0x4010, 0x404000, 0x20404000, 0x20000000, 0x20004000, 0x10, 0x20400010, 0x404000, 0x20404010, 0x400000, 0x4010, 0x20000010, 0x400000, 0x20004000, 0x20000000, 0x4010, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 0, 0x20400010, 0x10, 0x4000, 0x20400000, 0x404010, 0x4000, 0x400010, 0x20004010, 0, 0x20404000, 0x20000000, 0x400010, 0x20004010 };
100             long[] spfunction7 = { 0x200000, 0x4200002, 0x4000802, 0, 0x800, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000, 0, 0x4000002, 0x2, 0x4000000, 0x4200002, 0x802, 0x4000800, 0x200802, 0x200002, 0x4000800, 0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 0x800, 0x802, 0x4200802, 0x200800, 0x2, 0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 0x2, 0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 0x802, 0x200802, 0x4200800, 0x802, 0x4000002, 0x4200802, 0x4200000, 0x200800, 0, 0x2, 0x4200802, 0, 0x200802, 0x4200000, 0x800, 0x4000002, 0x4000800, 0x800, 0x200002 };
101             long[] spfunction8 = { 0x10001040, 0x1000, 0x40000, 0x10041040, 0x10000000, 0x10001040, 0x40, 0x10000000, 0x40040, 0x10040000, 0x10041040, 0x41000, 0x10041000, 0x41040, 0x1000, 0x40, 0x10040000, 0x10000040, 0x10001000, 0x1040, 0x41000, 0x40040, 0x10040040, 0x10041000, 0x1040, 0, 0, 0x10040040, 0x10000040, 0x10001000, 0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 0x1000, 0x40, 0x10040040, 0x1000, 0x41040, 0x10001000, 0x40, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 0x1040, 0x1040, 0x40040, 0x10000000, 0x10041000 };
102 
103 
104             //create the 16 or 48 subkeys we will need
105             int[] keys = des_createKeys(beinetkey);
106             int m = 0;
107             int i, j;
108             int temp, right1, right2, left, right;
109             int[] looping;
110             int cbcleft = 0, cbcleft2 = 0, cbcright = 0, cbcright2 = 0;
111             int endloop;
112             int loopinc;
113             int len = message.Length;
114             int chunk = 0;
115             //set up the loops for single and triple des
116             var iterations = keys.Length == 32 ? 3 : 9;//single or triple des
117             if (iterations == 3)
118             {
119                 looping = encrypt ? new int[] { 0, 32, 2 } : new int[] { 30, -2, -2 };
120             }
121             else { looping = encrypt ? new int[] { 0, 32, 2, 62, 30, -2, 64, 96, 2 } : new int[] { 94, 62, -2, 32, 64, 2, 30, -2, -2 }; }
122 
123             if (encrypt)
124             {
125                 message += "\0\0\0\0\0\0\0\0";//pad the message out with null bytes
126             }
127             //store the result here
128             //List<byte> result = new List<byte>();
129             //List<byte> tempresult = new List<byte>();
130             string result = string.Empty;
131             string tempresult = string.Empty;
132 
133             if (mode)
134             {//CBC mode
135                 int[] tmp = { 0, 0, 0, 0, 0, 0, 0, 0 };
136                 int pos = 24;
137                 int iTmp = 0;
138                 while (m < iv.Length && iTmp < tmp.Length)
139                 {
140                     if (pos < 0)
141                         pos = 24;
142                     tmp[iTmp++] = iv[m++] << pos;
143                     pos -= 8;
144                 }
145                 cbcleft = tmp[0] | tmp[1] | tmp[2] | tmp[3];
146                 cbcright = tmp[4] | tmp[5] | tmp[6] | tmp[7];
147 
148                 //cbcleft = (iv[m++] << 24) | (iv[m++] << 16) | (iv[m++] << 8) | iv[m++];
149                 //cbcright = (iv[m++] << 24) | (iv[m++] << 16) | (iv[m++] << 8) | iv[m++];
150                 m = 0;
151             }
152 
153             //loop through each 64 bit chunk of the message
154             while (m < len)
155             {
156                 if (encrypt)
157                 {/*加密时按双字节操作*/
158                     left = (message[m++] << 16) | message[m++];
159                     right = (message[m++] << 16) | message[m++];
160                 }
161                 else
162                 {
163                     left = (message[m++] << 24) | (message[m++] << 16) | (message[m++] << 8) | message[m++];
164                     right = (message[m++] << 24) | (message[m++] << 16) | (message[m++] << 8) | message[m++];
165                 }
166                 //for Cipher Block Chaining mode,xor the message with the previous result
167                 if (mode)
168                 {
169                     if (encrypt)
170                     {
171                         left ^= cbcleft; right ^= cbcright;
172                     }
173                     else
174                     {
175                         cbcleft2 = cbcleft; cbcright2 = cbcright; cbcleft = left; cbcright = right;
176                     }
177                 }
178 
179                 //first each 64 but chunk of the message must be permuted according to IP
180                 temp = (RM(left, 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
181                 temp = (RM(left, 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16);
182                 temp = (RM(right, 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2);
183                 temp = (RM(right, 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
184                 temp = (RM(left, 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
185 
186                 left = ((left << 1) | RM(left, 31));
187                 right = ((right << 1) | RM(right, 31));
188 
189                 //do this either 1 or 3 times for each chunk of the message
190                 for (j = 0; j < iterations; j += 3)
191                 {
192                     endloop = looping[j + 1];
193                     loopinc = looping[j + 2];
194                     //now go through and perform the encryption or decryption 
195                     for (i = looping[j]; i != endloop; i += loopinc)
196                     {//for efficiency
197                         right1 = right ^ keys[i];
198                         right2 = (RM(right, 4) | (right << 28)) ^ keys[i + 1];
199                         //the result is attained by passing these bytes through the S selection functions
200                         temp = left;
201                         left = right;
202                         right = (int)(temp ^ (spfunction2[RM(right1, 24) & 0x3f] | spfunction4[RM(right1, 16) & 0x3f] | spfunction6[RM(right1, 8) & 0x3f] | spfunction8[right1 & 0x3f] | spfunction1[RM(right2, 24) & 0x3f] | spfunction3[RM(right2, 16) & 0x3f] | spfunction5[RM(right2, 8) & 0x3f] | spfunction7[right2 & 0x3f]));
203                     }
204                     temp = left; left = right; right = temp;//unreverse left and right
205                 }//for either 1 or 3 iterations
206 
207                 //move then each one bit to the right
208                 left = (RM(left, 1) | (left << 31));
209                 right = (RM(right, 1) | (right << 31));
210 
211                 //now perform IP-1,which is IP in the opposite direction
212                 temp = (RM(left, 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
213                 temp = (RM(right, 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
214                 temp = (RM(right, 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2);
215                 temp = (RM(left, 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16);
216                 temp = (RM(left, 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
217 
218                 //for Cipher Block Chaining mode,xor the message with the previous result
219                 if (mode)
220                 {
221                     if (encrypt)
222                     {
223                         cbcleft = left; cbcright = right;
224                     }
225                     else
226                     {
227                         left ^= cbcleft2; right ^= cbcright2;
228                     }
229                 }
230                 //int[] arrInt;
231                 if (encrypt)
232                 {
233                     //arrInt = new int[]{ RM(left, 24), (RM(left, 16) & 0xff), (RM(left, 8) & 0xff), (left & 0xff), RM(right, 24), (RM(right, 16) & 0xff), (RM(right, 8) & 0xff), (right & 0xff) };
234                     tempresult += String.Concat((char)RM(left, 24),
235                         (char)(RM(left, 16) & 0xff),
236                         (char)(RM(left, 8) & 0xff),
237                         (char)(left & 0xff),
238                         (char)RM(right, 24),
239                         (char)(RM(right, 16) & 0xff),
240                         (char)(RM(right, 8) & 0xff),
241                         (char)(right & 0xff));
242                 }
243                 else
244                 {
245                     // 解密时,最后一个字符如果是\0,去除
246                     //arrInt = new int[] { (RM(left, 16) & 0xffff), (left & 0xffff), (RM(right, 16) & 0xffff), (right & 0xffff) };
247                     int tmpch = (RM(left, 16) & 0xffff);
248                     if (tmpch != 0)
249                         tempresult += (char)tmpch;
250                     tmpch = (left & 0xffff);
251                     if (tmpch != 0)
252                         tempresult += (char)tmpch;
253                     tmpch = (RM(right, 16) & 0xffff);
254                     if (tmpch != 0)
255                         tempresult += (char)tmpch;
256                     tmpch = (right & 0xffff);
257                     if (tmpch != 0)
258                         tempresult += (char)tmpch;
259                     //tempresult += String.Concat((char)(RM(left, 16) & 0xffff),
260                     //    (char)(left & 0xffff),
261                     //    (char)(RM(right, 16) & 0xffff),
262                     //    (char)(right & 0xffff));
263                 }/*解密时输出双字节*/
264                 //byte[] arrByte = new byte[arrInt.Length];
265                 //for (int loop = 0; loop < arrInt.Length; loop++)
266                 //{
267                 //    tempresult.Add(byte.Parse(arrInt[loop].ToString()));
268                 //    //arrByte[loop] = byte.Parse(arrInt[loop].ToString());
269                 //}
270                 //tempresult.Add(arrByte;// System.Text.Encoding.Unicode.GetString(arrByte);
271 
272                 chunk += encrypt ? 16 : 8;
273                 if (chunk == 512)
274                 {
275                     //result.AddRange(tempresult);tempresult.Clear(); 
276                     result += tempresult; tempresult = string.Empty;
277                     chunk = 0;
278                 }
279             }//for every 8 characters,or 64 bits in the message
280 
281             //return the result as an array
282 
283             //result.AddRange(tempresult);
284             //return result.ToArray();
285             return result + tempresult;
286         }//end of des
287 
288         /// <summary>
289         /// this takes as input a 64 bit beinetkey(even though only 56 bits are used)
290         /// as an array of 2 integers,and returns 16 48 bit keys
291         /// </summary>
292         /// <param name="beinetkey"></param>
293         /// <returns></returns>
294         private int[] des_createKeys(string beinetkey)
295         {
296             //declaring this locally speeds things up a bit
297             int[] pc2bytes0 = { 0, 0x4, 0x20000000, 0x20000004, 0x10000, 0x10004, 0x20010000, 0x20010004, 0x200, 0x204, 0x20000200, 0x20000204, 0x10200, 0x10204, 0x20010200, 0x20010204 };
298             int[] pc2bytes1 = { 0, 0x1, 0x100000, 0x100001, 0x4000000, 0x4000001, 0x4100000, 0x4100001, 0x100, 0x101, 0x100100, 0x100101, 0x4000100, 0x4000101, 0x4100100, 0x4100101 };
299             int[] pc2bytes2 = { 0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808, 0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808 };
300             int[] pc2bytes3 = { 0, 0x200000, 0x8000000, 0x8200000, 0x2000, 0x202000, 0x8002000, 0x8202000, 0x20000, 0x220000, 0x8020000, 0x8220000, 0x22000, 0x222000, 0x8022000, 0x8222000 };
301             int[] pc2bytes4 = { 0, 0x40000, 0x10, 0x40010, 0, 0x40000, 0x10, 0x40010, 0x1000, 0x41000, 0x1010, 0x41010, 0x1000, 0x41000, 0x1010, 0x41010 };
302             int[] pc2bytes5 = { 0, 0x400, 0x20, 0x420, 0, 0x400, 0x20, 0x420, 0x2000000, 0x2000400, 0x2000020, 0x2000420, 0x2000000, 0x2000400, 0x2000020, 0x2000420 };
303             int[] pc2bytes6 = { 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002, 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002 };
304             int[] pc2bytes7 = { 0, 0x10000, 0x800, 0x10800, 0x20000000, 0x20010000, 0x20000800, 0x20010800, 0x20000, 0x30000, 0x20800, 0x30800, 0x20020000, 0x20030000, 0x20020800, 0x20030800 };
305             int[] pc2bytes8 = { 0, 0x40000, 0, 0x40000, 0x2, 0x40002, 0x2, 0x40002, 0x2000000, 0x2040000, 0x2000000, 0x2040000, 0x2000002, 0x2040002, 0x2000002, 0x2040002 };
306             int[] pc2bytes9 = { 0, 0x10000000, 0x8, 0x10000008, 0, 0x10000000, 0x8, 0x10000008, 0x400, 0x10000400, 0x408, 0x10000408, 0x400, 0x10000400, 0x408, 0x10000408 };
307             int[] pc2bytes10 = { 0, 0x20, 0, 0x20, 0x100000, 0x100020, 0x100000, 0x100020, 0x2000, 0x2020, 0x2000, 0x2020, 0x102000, 0x102020, 0x102000, 0x102020 };
308             int[] pc2bytes11 = { 0, 0x1000000, 0x200, 0x1000200, 0x200000, 0x1200000, 0x200200, 0x1200200, 0x4000000, 0x5000000, 0x4000200, 0x5000200, 0x4200000, 0x5200000, 0x4200200, 0x5200200 };
309             int[] pc2bytes12 = { 0, 0x1000, 0x8000000, 0x8001000, 0x80000, 0x81000, 0x8080000, 0x8081000, 0x10, 0x1010, 0x8000010, 0x8001010, 0x80010, 0x81010, 0x8080010, 0x8081010 };
310             int[] pc2bytes13 = { 0, 0x4, 0x100, 0x104, 0, 0x4, 0x100, 0x104, 0x1, 0x5, 0x101, 0x105, 0x1, 0x5, 0x101, 0x105 };
311 
312             //how many iterations(1 for des,3 for triple des)
313             int iterations = beinetkey.Length >= 24 ? 3 : 1;
314             //stores the return keys
315             int[] keys = new int[32 * iterations];
316             //now define the left shifts which need to be done
317             int[] shifts = { 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0 };
318             //other variables
319             int left, right;
320             int lefttemp;
321             int righttemp;
322             int m = 0, n = 0;
323             int temp;
324 
325             for (int j = 0; j < iterations; j++)
326             {//either 1 or 3 iterations
327                 int[] tmp = { 0, 0, 0, 0, 0, 0, 0, 0 };
328                 int pos = 24;
329                 int iTmp = 0;
330                 while (m < beinetkey.Length && iTmp < tmp.Length)
331                 {
332                     if (pos < 0)
333                         pos = 24;
334                     tmp[iTmp++] = beinetkey[m++] << pos;
335                     pos -= 8;
336                 }
337                 left = tmp[0] | tmp[1] | tmp[2] | tmp[3];
338                 right = tmp[4] | tmp[5] | tmp[6] | tmp[7];
339 
340                 //left = (beinetkey[m++] << 24) | (beinetkey[m++] << 16) | (beinetkey[m++] << 8) | beinetkey[m++];
341                 //right = (beinetkey[m++] << 24) | (beinetkey[m++] << 16) | (beinetkey[m++] << 8) | beinetkey[m++];
342 
343                 temp = (RM(left, 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
344                 temp = (RM(right, -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
345                 temp = (RM(left, 2) ^ right) & 0x33333333; right ^= temp; left ^= (temp << 2);
346                 temp = (RM(right, -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
347                 temp = (RM(left, 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
348                 temp = (RM(right, 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
349                 temp = (RM(left, 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
350 
351                 //the right side needs to be shifted and to get the last four bits of the left side
352                 temp = (left << 8) | (RM(right, 20) & 0x000000f0);
353                 //left needs to be put upside down
354                 left = (right << 24) | ((right << 8) & 0xff0000) | (RM(right, 8) & 0xff00) | (RM(right, 24) & 0xf0);
355                 right = temp;
356 
357                 //now go through and perform these shifts on the left and right keys
358                 for (int i = 0; i < shifts.Length; i++)
359                 {
360                     //shift the keys either one or two bits to the left
361                     if (shifts[i] == 1)
362                     {
363                         left = (left << 2) | RM(left, 26); right = (right << 2) | RM(right, 26);
364                     }
365                     else
366                     {
367                         left = (left << 1) | RM(left, 27); right = (right << 1) | RM(right, 27);
368                     }
369                     left &= -0xf; right &= -0xf;
370 
371                     //now apply PC-2,in such a way that E is easier when encrypting or decrypting
372                     //this conversion will look like PC-2 except only the last 6 bits of each byte are used
373                     //rather than 48 consecutive bits and the order of lines will be according to 
374                     //how the S selection functions will be applied:S2,S4,S6,S8,S1,S3,S5,S7
375                     lefttemp = pc2bytes0[RM(left, 28)] | pc2bytes1[RM(left, 24) & 0xf]
376                    | pc2bytes2[RM(left, 20) & 0xf] | pc2bytes3[RM(left, 16) & 0xf]
377                    | pc2bytes4[RM(left, 12) & 0xf] | pc2bytes5[RM(left, 8) & 0xf]
378                    | pc2bytes6[RM(left, 4) & 0xf];
379                     righttemp = pc2bytes7[RM(right, 28)] | pc2bytes8[RM(right, 24) & 0xf]
380                    | pc2bytes9[RM(right, 20) & 0xf] | pc2bytes10[RM(right, 16) & 0xf]
381                    | pc2bytes11[RM(right, 12) & 0xf] | pc2bytes12[RM(right, 8) & 0xf]
382                    | pc2bytes13[RM(right, 4) & 0xf];
383                     temp = (RM(righttemp, 16) ^ lefttemp) & 0x0000ffff;
384                     keys[n++] = lefttemp ^ temp; keys[n++] = righttemp ^ (temp << 16);
385                 }
386             }//for each iterations
387             //return the keys we"ve created
388             return keys;
389         }//end of des_createKeys
390 
391         #endregion

 

DES加密解密算法调用
 1 ///</summary>
 2         /// DES加密解密算法调用
 3         /// </summary>
 4         public void DESManager()
 5         {
 6             string strOutPutData = "DES加密算法测试";
 7             strOutPutData = EncryptDES(strOutPutData, "12345678");//加密
 8             strOutPutData = DecryptDES(strOutPutData, "12345678");//解密
 9             
10         }

 

Base64加密解密算法实现
 1  #region Base64加密解密算法实现
 2         /// <summary>
 3         /// Base64加密
 4         /// </summary>
 5         /// <param name="Message"></param>
 6         /// <returns></returns>
 7         public string Base64Code(string Message)
 8         {
 9             byte[] encData_byte = new byte[Message.Length];
10             encData_byte = System.Text.Encoding.UTF8.GetBytes(Message);
11             string encodedData = Convert.ToBase64String(encData_byte);
12             return encodedData;
13         }
14         /// <summary>
15         /// Base64解密
16         /// </summary>
17         /// <param name="Message"></param>
18         /// <returns></returns>
19         public string Base64Decode(string Message)
20         {
21             try
22             {
23                 System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
24                 System.Text.Decoder utf8Decode = encoder.GetDecoder();
25                 byte[] todecode_byte = Convert.FromBase64String(Message);
26                 int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
27                 char[] decoded_char = new char[charCount];
28                 utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
29                 string result = new String(decoded_char);
30                 return result;
31             }
32             catch (Exception e)
33             {
34                 throw new Exception("Error in base64Decode" + e.Message);
35             }
36         }
37         #endregion

 

Base64加密算法调用
1 /// <summary>
2         /// Base64加密算法调用
3         /// </summary>
4         public void Base64Manager()
5         {
6             string strOutPutData = "Base64加密解密测试";
7             strOutPutData =Base64Code(strOutPutData);
8             strOutPutData = Base64Decode(strOutPutData);
9         }

 

 

 

2.javascript 实现

js DES加密解密
  1 /**
  2  * Created with JetBrains WebStorm.
  3  * User: markeluo
  4  * Date: 13-3-7
  5  * Time: 下午12:31
  6  * To change this template use File | Settings | File Templates.
  7  */
  8 function des(beinetkey, message, encrypt, mode, iv) {
  9     //declaring this locally speeds things up a bit
 10     var spfunction1 = new Array(0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 0x4, 0x10000, 0x400, 0x1010400, 0x1010404, 0x400, 0x1000404, 0x1010004, 0x1000000, 0x4, 0x404, 0x1000400, 0x1000400, 0x10400, 0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 0x404, 0x10404, 0x1000000, 0x10000, 0x1010404, 0x4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 0x400, 0x1010004, 0x10000, 0x10400, 0x1000004, 0x400, 0x4, 0x1000404, 0x10404, 0x1010404, 0x10004, 0x1010000, 0x1000404, 0x1000004, 0x404, 0x10404, 0x1010400, 0x404, 0x1000400, 0x1000400, 0, 0x10004, 0x10400, 0, 0x1010004);
 11     var spfunction2 = new Array(-0x7fef7fe0, -0x7fff8000, 0x8000, 0x108020, 0x100000, 0x20, -0x7fefffe0, -0x7fff7fe0, -0x7fffffe0, -0x7fef7fe0, -0x7fef8000, -0x80000000, -0x7fff8000, 0x100000, 0x20, -0x7fefffe0, 0x108000, 0x100020, -0x7fff7fe0, 0, -0x80000000, 0x8000, 0x108020, -0x7ff00000, 0x100020, -0x7fffffe0, 0, 0x108000, 0x8020, -0x7fef8000, -0x7ff00000, 0x8020, 0, 0x108020, -0x7fefffe0, 0x100000, -0x7fff7fe0, -0x7ff00000, -0x7fef8000, 0x8000, -0x7ff00000, -0x7fff8000, 0x20, -0x7fef7fe0, 0x108020, 0x20, 0x8000, -0x80000000, 0x8020, -0x7fef8000, 0x100000, -0x7fffffe0, 0x100020, -0x7fff7fe0, -0x7fffffe0, 0x100020, 0x108000, 0, -0x7fff8000, 0x8020, -0x80000000, -0x7fefffe0, -0x7fef7fe0, 0x108000);
 12     var spfunction3 = new Array(0x208, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008, 0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 0x208, 0x8000000, 0x8, 0x8020200, 0x200, 0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 0x8, 0x8020208, 0x200, 0x8000000, 0x8020200, 0x8000000, 0x20008, 0x208, 0x20000, 0x8020200, 0x8000200, 0, 0x200, 0x20008, 0x8020208, 0x8000200, 0x8000008, 0x200, 0, 0x8020008, 0x8000208, 0x20000, 0x8000000, 0x8020208, 0x8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 0x208, 0x8020000, 0x20208, 0x8, 0x8020008, 0x20200);
 13     var spfunction4 = new Array(0x802001, 0x2081, 0x2081, 0x80, 0x802080, 0x800081, 0x800001, 0x2001, 0, 0x802000, 0x802000, 0x802081, 0x81, 0, 0x800080, 0x800001, 0x1, 0x2000, 0x800000, 0x802001, 0x80, 0x800000, 0x2001, 0x2080, 0x800081, 0x1, 0x2080, 0x800080, 0x2000, 0x802080, 0x802081, 0x81, 0x800080, 0x800001, 0x802000, 0x802081, 0x81, 0, 0, 0x802000, 0x2080, 0x800080, 0x800081, 0x1, 0x802001, 0x2081, 0x2081, 0x80, 0x802081, 0x81, 0x1, 0x2000, 0x800001, 0x2001, 0x802080, 0x800081, 0x2001, 0x2080, 0x800000, 0x802001, 0x80, 0x800000, 0x2000, 0x802080);
 14     var spfunction5 = new Array(0x100, 0x2080100, 0x2080000, 0x42000100, 0x80000, 0x100, 0x40000000, 0x2080000, 0x40080100, 0x80000, 0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 0x42000000, 0x80100, 0x80000, 0x42000100, 0x100, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100, 0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 0x100, 0x2000000, 0x42080000, 0x42080100, 0x80100, 0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 0, 0x40080000, 0x2080100, 0x40000100);
 15     var spfunction6 = new Array(0x20000010, 0x20400000, 0x4000, 0x20404010, 0x20400000, 0x10, 0x20404010, 0x400000, 0x20004000, 0x404010, 0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 0x4010, 0, 0x400010, 0x20004010, 0x4000, 0x404000, 0x20004010, 0x10, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 0x4010, 0x404000, 0x20404000, 0x20000000, 0x20004000, 0x10, 0x20400010, 0x404000, 0x20404010, 0x400000, 0x4010, 0x20000010, 0x400000, 0x20004000, 0x20000000, 0x4010, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 0, 0x20400010, 0x10, 0x4000, 0x20400000, 0x404010, 0x4000, 0x400010, 0x20004010, 0, 0x20404000, 0x20000000, 0x400010, 0x20004010);
 16     var spfunction7 = new Array(0x200000, 0x4200002, 0x4000802, 0, 0x800, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000, 0, 0x4000002, 0x2, 0x4000000, 0x4200002, 0x802, 0x4000800, 0x200802, 0x200002, 0x4000800, 0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 0x800, 0x802, 0x4200802, 0x200800, 0x2, 0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 0x2, 0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 0x802, 0x200802, 0x4200800, 0x802, 0x4000002, 0x4200802, 0x4200000, 0x200800, 0, 0x2, 0x4200802, 0, 0x200802, 0x4200000, 0x800, 0x4000002, 0x4000800, 0x800, 0x200002);
 17     var spfunction8 = new Array(0x10001040, 0x1000, 0x40000, 0x10041040, 0x10000000, 0x10001040, 0x40, 0x10000000, 0x40040, 0x10040000, 0x10041040, 0x41000, 0x10041000, 0x41040, 0x1000, 0x40, 0x10040000, 0x10000040, 0x10001000, 0x1040, 0x41000, 0x40040, 0x10040040, 0x10041000, 0x1040, 0, 0, 0x10040040, 0x10000040, 0x10001000, 0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 0x1000, 0x40, 0x10040040, 0x1000, 0x41040, 0x10001000, 0x40, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 0x1040, 0x1040, 0x40040, 0x10000000, 0x10041000);
 18 
 19     //create the 16 or 48 subkeys we will need
 20     var keys = des_createKeys(beinetkey);
 21     var m = 0, i, j, temp, temp2, right1, right2, left, right, looping;
 22     var cbcleft, cbcleft2, cbcright, cbcright2
 23     var endloop, loopinc;
 24     var len = message.length;
 25     var chunk = 0;
 26     //set up the loops for single and triple des
 27     var iterations = keys.length == 32 ? 3 : 9; //single or triple des
 28     if (iterations == 3) { looping = encrypt ? new Array(0, 32, 2) : new Array(30, -2, -2); }
 29     else { looping = encrypt ? new Array(0, 32, 2, 62, 30, -2, 64, 96, 2) : new Array(94, 62, -2, 32, 64, 2, 30, -2, -2); }
 30 
 31     message += '\0\0\0\0\0\0\0\0'; //pad the message out with null bytes
 32     //store the result here
 33     result = '';
 34     tempresult = '';
 35 
 36     if (mode == 1) {//CBC mode
 37         cbcleft = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++);
 38         cbcright = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++);
 39         m = 0;
 40     }
 41 
 42     //loop through each 64 bit chunk of the message
 43     while (m < len) {
 44         if (encrypt) {//加密时按双字节操作
 45             left = (message.charCodeAt(m++) << 16) | message.charCodeAt(m++);
 46             right = (message.charCodeAt(m++) << 16) | message.charCodeAt(m++);
 47         } else {
 48             left = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) | (message.charCodeAt(m++) << 8) | message.charCodeAt(m++);
 49             right = (message.charCodeAt(m++) << 24) | (message.charCodeAt(m++) << 16) | (message.charCodeAt(m++) << 8) | message.charCodeAt(m++);
 50         }
 51         //for Cipher Block Chaining mode,xor the message with the previous result
 52         if (mode == 1) { if (encrypt) { left ^= cbcleft; right ^= cbcright; } else { cbcleft2 = cbcleft; cbcright2 = cbcright; cbcleft = left; cbcright = right; } }
 53 
 54         //first each 64 but chunk of the message must be permuted according to IP
 55         temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
 56         temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16);
 57         temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2);
 58         temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
 59         temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
 60 
 61         left = ((left << 1) | (left >>> 31));
 62         right = ((right << 1) | (right >>> 31));
 63 
 64         //do this either 1 or 3 times for each chunk of the message
 65         for (j = 0; j < iterations; j += 3) {
 66             endloop = looping[j + 1];
 67             loopinc = looping[j + 2];
 68             //now go through and perform the encryption or decryption
 69             for (i = looping[j]; i != endloop; i += loopinc) {//for efficiency
 70                 right1 = right ^ keys[i];
 71                 right2 = ((right >>> 4) | (right << 28)) ^ keys[i + 1];
 72                 //the result is attained by passing these bytes through the S selection functions
 73                 temp = left;
 74                 left = right;
 75                 right = temp ^ (spfunction2[(right1 >>> 24) & 0x3f] | spfunction4[(right1 >>> 16) & 0x3f] | spfunction6[(right1 >>> 8) & 0x3f] | spfunction8[right1 & 0x3f] | spfunction1[(right2 >>> 24) & 0x3f] | spfunction3[(right2 >>> 16) & 0x3f] | spfunction5[(right2 >>> 8) & 0x3f] | spfunction7[right2 & 0x3f]);
 76             }
 77             temp = left; left = right; right = temp; //unreverse left and right
 78         } //for either 1 or 3 iterations
 79 
 80         //move then each one bit to the right
 81         left = ((left >>> 1) | (left << 31));
 82         right = ((right >>> 1) | (right << 31));
 83 
 84         //now perform IP-1,which is IP in the opposite direction
 85         temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
 86         temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
 87         temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2);
 88         temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16);
 89         temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
 90 
 91         //for Cipher Block Chaining mode,xor the message with the previous result
 92         if (mode == 1) { if (encrypt) { cbcleft = left; cbcright = right; } else { left ^= cbcleft2; right ^= cbcright2; } }
 93         if (encrypt) {
 94             tempresult += String.fromCharCode((left >>> 24), ((left >>> 16) & 0xff), ((left >>> 8) & 0xff), (left & 0xff), (right >>> 24), ((right >>> 16) & 0xff), ((right >>> 8) & 0xff), (right & 0xff));
 95         }
 96         else {
 97             tempresult += String.fromCharCode(((left >>> 16) & 0xffff), (left & 0xffff), ((right >>> 16) & 0xffff), (right & 0xffff));
 98         } //解密时输出双字节
 99         encrypt ? chunk += 16 : chunk += 8;
100         if (chunk == 512) { result += tempresult; tempresult = ''; chunk = 0; }
101     } //for every 8 characters,or 64 bits in the message
102 
103     //return the result as an array
104     return result + tempresult;
105 } //end of des
106 
107 //des_createKeys
108 //this takes as input a 64 bit beinetkey(even though only 56 bits are used)
109 //as an array of 2 integers,and returns 16 48 bit keys
110 function des_createKeys(beinetkey) {
111     //declaring this locally speeds things up a bit
112     pc2bytes0 = new Array(0, 0x4, 0x20000000, 0x20000004, 0x10000, 0x10004, 0x20010000, 0x20010004, 0x200, 0x204, 0x20000200, 0x20000204, 0x10200, 0x10204, 0x20010200, 0x20010204);
113     pc2bytes1 = new Array(0, 0x1, 0x100000, 0x100001, 0x4000000, 0x4000001, 0x4100000, 0x4100001, 0x100, 0x101, 0x100100, 0x100101, 0x4000100, 0x4000101, 0x4100100, 0x4100101);
114     pc2bytes2 = new Array(0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808, 0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808);
115     pc2bytes3 = new Array(0, 0x200000, 0x8000000, 0x8200000, 0x2000, 0x202000, 0x8002000, 0x8202000, 0x20000, 0x220000, 0x8020000, 0x8220000, 0x22000, 0x222000, 0x8022000, 0x8222000);
116     pc2bytes4 = new Array(0, 0x40000, 0x10, 0x40010, 0, 0x40000, 0x10, 0x40010, 0x1000, 0x41000, 0x1010, 0x41010, 0x1000, 0x41000, 0x1010, 0x41010);
117     pc2bytes5 = new Array(0, 0x400, 0x20, 0x420, 0, 0x400, 0x20, 0x420, 0x2000000, 0x2000400, 0x2000020, 0x2000420, 0x2000000, 0x2000400, 0x2000020, 0x2000420);
118     pc2bytes6 = new Array(0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002, 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002);
119     pc2bytes7 = new Array(0, 0x10000, 0x800, 0x10800, 0x20000000, 0x20010000, 0x20000800, 0x20010800, 0x20000, 0x30000, 0x20800, 0x30800, 0x20020000, 0x20030000, 0x20020800, 0x20030800);
120     pc2bytes8 = new Array(0, 0x40000, 0, 0x40000, 0x2, 0x40002, 0x2, 0x40002, 0x2000000, 0x2040000, 0x2000000, 0x2040000, 0x2000002, 0x2040002, 0x2000002, 0x2040002);
121     pc2bytes9 = new Array(0, 0x10000000, 0x8, 0x10000008, 0, 0x10000000, 0x8, 0x10000008, 0x400, 0x10000400, 0x408, 0x10000408, 0x400, 0x10000400, 0x408, 0x10000408);
122     pc2bytes10 = new Array(0, 0x20, 0, 0x20, 0x100000, 0x100020, 0x100000, 0x100020, 0x2000, 0x2020, 0x2000, 0x2020, 0x102000, 0x102020, 0x102000, 0x102020);
123     pc2bytes11 = new Array(0, 0x1000000, 0x200, 0x1000200, 0x200000, 0x1200000, 0x200200, 0x1200200, 0x4000000, 0x5000000, 0x4000200, 0x5000200, 0x4200000, 0x5200000, 0x4200200, 0x5200200);
124     pc2bytes12 = new Array(0, 0x1000, 0x8000000, 0x8001000, 0x80000, 0x81000, 0x8080000, 0x8081000, 0x10, 0x1010, 0x8000010, 0x8001010, 0x80010, 0x81010, 0x8080010, 0x8081010);
125     pc2bytes13 = new Array(0, 0x4, 0x100, 0x104, 0, 0x4, 0x100, 0x104, 0x1, 0x5, 0x101, 0x105, 0x1, 0x5, 0x101, 0x105);
126 
127     //how many iterations(1 for des,3 for triple des)
128     var iterations = beinetkey.length >= 24 ? 3 : 1;
129     //stores the return keys
130     var keys = new Array(32 * iterations);
131     //now define the left shifts which need to be done
132     var shifts = new Array(0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0);
133     //other variables
134     var lefttemp, righttemp, m = 0, n = 0, temp;
135 
136     for (var j = 0; j < iterations; j++) {//either 1 or 3 iterations
137         left = (beinetkey.charCodeAt(m++) << 24) | (beinetkey.charCodeAt(m++) << 16) | (beinetkey.charCodeAt(m++) << 8) | beinetkey.charCodeAt(m++);
138         right = (beinetkey.charCodeAt(m++) << 24) | (beinetkey.charCodeAt(m++) << 16) | (beinetkey.charCodeAt(m++) << 8) | beinetkey.charCodeAt(m++);
139 
140         temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
141         temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
142         temp = ((left >>> 2) ^ right) & 0x33333333; right ^= temp; left ^= (temp << 2);
143         temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
144         temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
145         temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
146         temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
147 
148         //the right side needs to be shifted and to get the last four bits of the left side
149         temp = (left << 8) | ((right >>> 20) & 0x000000f0);
150         //left needs to be put upside down
151         left = (right << 24) | ((right << 8) & 0xff0000) | ((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0);
152         right = temp;
153 
154         //now go through and perform these shifts on the left and right keys
155         for (i = 0; i < shifts.length; i++) {
156             //shift the keys either one or two bits to the left
157             if (shifts[i]) { left = (left << 2) | (left >>> 26); right = (right << 2) | (right >>> 26); }
158             else { left = (left << 1) | (left >>> 27); right = (right << 1) | (right >>> 27); }
159             left &= -0xf; right &= -0xf;
160 
161             //now apply PC-2,in such a way that E is easier when encrypting or decrypting
162             //this conversion will look like PC-2 except only the last 6 bits of each byte are used
163             //rather than 48 consecutive bits and the order of lines will be according to
164             //how the S selection functions will be applied:S2,S4,S6,S8,S1,S3,S5,S7
165             lefttemp = pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf]
166                 | pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(left >>> 16) & 0xf]
167                 | pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf]
168                 | pc2bytes6[(left >>> 4) & 0xf];
169             righttemp = pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf]
170                 | pc2bytes9[(right >>> 20) & 0xf] | pc2bytes10[(right >>> 16) & 0xf]
171                 | pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf]
172                 | pc2bytes13[(right >>> 4) & 0xf];
173             temp = ((righttemp >>> 16) ^ lefttemp) & 0x0000ffff;
174             keys[n++] = lefttemp ^ temp; keys[n++] = righttemp ^ (temp << 16);
175         }
176     } //for each iterations
177     //return the keys we've created
178     return keys;
179 } //end of des_createKeys
180 
181 // 把字符串转换为16进制字符串
182 // 如:a变成61(即10进制的97);abc变成616263
183 function stringToHex(s) {
184     var r='';
185     var hexes=new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
186     for(var i=0;i<(s.length);i++){r+=hexes[s.charCodeAt(i)>>4]+hexes[s.charCodeAt(i)&0xf];}
187     return r;
188 }
189 // 16进制字符串转换为字符串
190 // 如:61(即10进制的97)变成a;616263变成abc
191 function HexTostring(s){
192     var r='';
193     for(var i=0;i<s.length;i+=2){
194         var sxx=parseInt(s.substring(i,i+2),16);
195         r+=String.fromCharCode(sxx);}
196     return r;
197 }
198 
199 /// 加密测试函数
200 /// s     待加密的字符串
201 /// k     密钥
202 function encMe(s, k){
203     return stringToHex(des(k,s,1,0));
204 }
205 
206 /// 解密测试函数
207 /// s     待解密的字符串
208 /// k     密钥
209 function uncMe(s, k){
210     return des(k,HexTostring(s),0,0);
211 }
js Base64加密解密
  1 var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  2 var base64DecodeChars = new Array(
  3     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  4     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  5     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  6     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  7     -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
  8     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  9     -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
 10     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
 11 
 12 function base64encode(str) {
 13     var out, i, len;
 14     var c1, c2, c3;
 15 
 16     len = str.length;
 17     i = 0;
 18     out = "";
 19     while(i < len) {
 20         c1 = str.charCodeAt(i++) & 0xff;
 21         if(i == len)
 22         {
 23             out += base64EncodeChars.charAt(c1 >> 2);
 24             out += base64EncodeChars.charAt((c1 & 0x3) << 4);
 25             out += "==";
 26             break;
 27         }
 28         c2 = str.charCodeAt(i++);
 29         if(i == len)
 30         {
 31             out += base64EncodeChars.charAt(c1 >> 2);
 32             out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
 33             out += base64EncodeChars.charAt((c2 & 0xF) << 2);
 34             out += "=";
 35             break;
 36         }
 37         c3 = str.charCodeAt(i++);
 38         out += base64EncodeChars.charAt(c1 >> 2);
 39         out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
 40         out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
 41         out += base64EncodeChars.charAt(c3 & 0x3F);
 42     }
 43     return out;
 44 }
 45 
 46 function base64decode(str) {
 47     var c1, c2, c3, c4;
 48     var i, len, out;
 49 
 50     len = str.length;
 51     i = 0;
 52     out = "";
 53     while(i < len) {
 54         /* c1 */
 55         do {
 56             c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
 57         } while(i < len && c1 == -1);
 58         if(c1 == -1)
 59             break;
 60 
 61         /* c2 */
 62         do {
 63             c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
 64         } while(i < len && c2 == -1);
 65         if(c2 == -1)
 66             break;
 67 
 68         out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
 69 
 70         /* c3 */
 71         do {
 72             c3 = str.charCodeAt(i++) & 0xff;
 73             if(c3 == 61)
 74                 return out;
 75             c3 = base64DecodeChars[c3];
 76         } while(i < len && c3 == -1);
 77         if(c3 == -1)
 78             break;
 79 
 80         out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
 81 
 82         /* c4 */
 83         do {
 84             c4 = str.charCodeAt(i++) & 0xff;
 85             if(c4 == 61)
 86                 return out;
 87             c4 = base64DecodeChars[c4];
 88         } while(i < len && c4 == -1);
 89         if(c4 == -1)
 90             break;
 91         out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
 92     }
 93     return out;
 94 }
 95 
 96 function utf16to8(str) {
 97     var out, i, len, c;
 98 
 99     out = "";
100     len = str.length;
101     for(i = 0; i < len; i++) {
102         c = str.charCodeAt(i);
103         if ((c >= 0x0001) && (c <= 0x007F)) {
104             out += str.charAt(i);
105         } else if (c > 0x07FF) {
106             out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
107             out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
108             out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
109         } else {
110             out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
111             out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
112         }
113     }
114     return out;
115 }
116 
117 function utf8to16(str) {
118     var out, i, len, c;
119     var char2, char3;
120 
121     out = "";
122     len = str.length;
123     i = 0;
124     while(i < len) {
125         c = str.charCodeAt(i++);
126         switch(c >> 4)
127         {
128             case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
129             // 0xxxxxxx
130             out += str.charAt(i-1);
131             break;
132             case 12: case 13:
133             // 110x xxxx   10xx xxxx
134             char2 = str.charCodeAt(i++);
135             out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
136             break;
137             case 14:
138                 // 1110 xxxx  10xx xxxx  10xx xxxx
139                 char2 = str.charCodeAt(i++);
140                 char3 = str.charCodeAt(i++);
141                 out += String.fromCharCode(((c & 0x0F) << 12) |
142                     ((char2 & 0x3F) << 6) |
143                     ((char3 & 0x3F) << 0));
144                 break;
145         }
146     }
147 
148     return out;
149 }

 

 

 

 

转载于:https://www.cnblogs.com/luowanli/archive/2013/03/08/DES-Base64%e5%8a%a0%e5%af%86%e8%a7%a3%e5%af%86.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值