zhao
2021-07-02 081df17b8cc4a6e7e4f4e1e1887f24810e3ec2f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
 
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
 
namespace HH.WMS.Utils.NPOI.POIFS.Crypt
{
    public class Base64  //Convert Jave Base64 Leon
    {
        private static 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','+','/','='
        };
 
        private static string base64Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 
        public static string Base64Code(string message)
        {
            byte empty = (byte)0;
            ArrayList byteMessage = new ArrayList(Encoding.Default.GetBytes(message));
 
            StringBuilder outMessage;
            int messageLen = byteMessage.Count;
 
            int page = messageLen / 3;
            int use = 0;
 
            if ((use = messageLen % 3) > 0)
            {
                for (int i = 0; i < 3 - use; i++)
                {
                    byteMessage.Add(empty);
                }
 
                page++;
            }
 
            outMessage = new StringBuilder(page * 4);
            for (int i = 0; i < page; i++)
            {
                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];
 
                int[] outStr = new int[4];
 
                outStr[0] = inStr[0] >> 2;
                outStr[1] = ((inStr[0] & 0x03) << 4) ^ (inStr[1] >> 4);
                if (!inStr.Equals(empty))
                    outStr[2] = ((inStr[1] & 0x0f) << 2) ^ (inStr[2] >> 6);
                else
                    outStr[2] = 64;
 
                if (!inStr[2].Equals(empty))
                {
                    outStr[3] = inStr[2] & 0x3f;
                }
                else
                {
                    outStr[3] = 64;
                }
 
                outMessage.Append(base64Code[outStr[0]]);
                outMessage.Append(base64Code[outStr[1]]);
                outMessage.Append(base64Code[outStr[2]]);
                outMessage.Append(base64Code[outStr[3]]);
            }
 
            return outMessage.ToString();
        }
 
        public static byte[] DecodeBase64(string message)
        {
            if ((message.Length % 4) != 0)
            {
                throw new ArgumentException("Please check the correct base64 code");
            }
            if (!Regex.IsMatch(message, "^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase))
            {
                throw new ArgumentException("Please check the correct base64 code");
            }
 
            int page = message.Length / 4;
            ArrayList outMessage = new ArrayList(page * 3);
            char[] chMessage = message.ToCharArray();
 
            for (int i = 0; i < page; i++)
            {
                byte[] inStr = new byte[4];
                inStr[0] = (byte)base64Str.IndexOf(message[i * 4]);
                inStr[1] = (byte)base64Str.IndexOf(message[i * 4 + 1]);
                inStr[2] = (byte)base64Str.IndexOf(message[i * 4 + 2]);
                inStr[3] = (byte)base64Str.IndexOf(message[i * 4 + 3]);
 
                byte[] outStr = new byte[3];
                outStr[0] = (byte)((inStr[0] << 2) ^ ((inStr[1] & 0x30) >> 4));
 
                if (inStr[2] != 64)
                {
                    outStr[1] = (byte)((inStr[1] << 4) ^ ((inStr[2] & 0x30) >> 2));
                }
                else
                {
                    outStr[1] = 0;
                }
 
                if (inStr[3] != 64)
                {
                    outStr[2] = (byte)((inStr[1] << 6) ^ inStr[3]);
                }
                else
                {
                    outStr[2] = 0;
                }
 
                outMessage.Add(outStr[0]);
                if (outStr[1] != 0)
                {
                    outMessage.Add(outStr[1]);
                }
 
                if (outStr[2] != 0)
                {
                    outMessage.Add(outStr[2]);
                }
            }
            byte[] outByte = (byte[])outMessage.ToArray(typeof(System.Byte));
            //return Encoding.Default.GetString(outByte);
            return outByte;
        }
    }
}