惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
H
Help Net Security
P
Privacy & Cybersecurity Law Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
Hugging Face - Blog
Hugging Face - Blog
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V2EX - 技术
V2EX - 技术
人人都是产品经理
人人都是产品经理
L
LINUX DO - 最新话题
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google Online Security Blog
Google Online Security Blog
博客园 - 聂微东
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Blog — PlanetScale
Blog — PlanetScale
C
Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
Webroot Blog
Webroot Blog
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
Simon Willison's Weblog
Simon Willison's Weblog
A
Arctic Wolf
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
Y
Y Combinator Blog
Last Week in AI
Last Week in AI
S
Securelist
Cloudbric
Cloudbric
G
GRAHAM CLULEY
M
MIT News - Artificial intelligence
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Visual Studio Blog
S
Schneier on Security
Engineering at Meta
Engineering at Meta

博客园 - Vincent

reCaptcha 在UpdatePanel 和 ModalPopup中的使用 - Vincent Invalid character in a Base-64 string System.ArgumentOutOfRangeException at System.Web.HttpCachePolicy.UtcSetLastModified (DateTime utcDate) Fabulous Adventures In Coding Microsoft will end OEM and shrink-wrapped sales of Windows XP on June 30, 2008 - Vincent My AutoCompleteExtender Windows Live Writer GRI-MindHarbor软件公司 单表继承 (Single Table Inheritance) 写给yzx110的一个实例,仅作参考! 很久没有写blog了. MD5加密 信用卡是超前消费的一种手段 vs.net2003无法打开*.xsd文件的解决方法 针对MS Project 2003 的开发 Test Your Knowledge of Microsoft Visual Studio .NET page event Microsoft Office InfoPath 2003 Toolkit for Visual Studio .NET 在Code-Behind中操作WebUserControl
Base64
Vincent · 2005-07-29 · via 博客园 - Vincent

 1public class Base64Decoder
 2    {
 3        char[] source;
 4        int length, length2, length3;
 5        int blockCount;
 6        int paddingCount;
 7        public Base64Decoder(char[] input)
 8        {
 9            int temp=0;
10            source=input;
11            length=input.Length;
12
13            //find how many padding are there
14            for (int x=0;x<2;x++)
15            {
16                if(input[length-x-1]=='=')
17                    temp++;
18            }

19            paddingCount=temp;
20            //calculate the blockCount;
21            //assuming all whitespace and carriage returns/newline were removed.
22            blockCount=length/4;
23            length2=blockCount*3;
24        }

25
26        public byte[] GetDecoded()
27        {
28            byte[] buffer=new byte[length];//first conversion result
29            byte[] buffer2=new byte[length2];//decoded array with padding
30
31            for(int x=0;x<length;x++)
32            {
33                buffer[x]=char2sixbit(source[x]);
34            }

35
36            byte b, b1,b2,b3;
37            byte temp1, temp2, temp3, temp4;
38
39            for(int x=0;x<blockCount;x++)
40            {
41                temp1=buffer[x*4];
42                temp2=buffer[x*4+1];
43                temp3=buffer[x*4+2];
44                temp4=buffer[x*4+3];                
45
46                b=(byte)(temp1<<2);
47                b1=(byte)((temp2 & 48)>>4);
48                b1+=b;
49
50                b=(byte)((temp2 & 15)<<4);
51                b2=(byte)((temp3 & 60)>>2);
52                b2+=b;
53
54                b=(byte)((temp3 & 3)<<6);
55                b3=temp4;
56                b3+=b;
57
58                buffer2[x*3]=b1;
59                buffer2[x*3+1]=b2;
60                buffer2[x*3+2]=b3;
61            }

62            //remove paddings
63            length3=length2-paddingCount;
64            byte[] result=new byte[length3];
65
66            for(int x=0;x<length3;x++)
67            {
68                result[x]=buffer2[x];
69            }

70
71            return result;
72        }

73
74        private byte char2sixbit(char c)
75        {
76            char[] lookupTable=new char[64]
77                    {    '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',
78                        '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',
79                        '0','1','2','3','4','5','6','7','8','9','+','/'}
;
80            if(c=='=')
81                return 0;
82            else
83            {
84                for (int x=0;x<64;x++)
85                {
86                    if (lookupTable[x]==c)
87                        return (byte)x;
88                }

89                //should not reach here
90                return 0;
91            }

92
93        }

94
95    }

96

  1public class Base64Encoder
  2    {
  3        byte[] source;
  4        int length,length2;
  5        int blockCount;
  6        int paddingCount;
  7        public Base64Encoder(byte[] input)
  8        {
  9            source=input;
 10            length=input.Length;
 11            if((length % 3)==0)
 12            {
 13                paddingCount=0;
 14                blockCount=length/3;
 15            }

 16            else
 17            {
 18                paddingCount=3-(length % 3);//need to add padding
 19                blockCount=(length+paddingCount) / 3;
 20            }

 21            length2=length+paddingCount;//or blockCount *3
 22        }

 23
 24        public char[] GetEncoded()
 25        {
 26            byte[] source2;
 27            source2=new byte[length2];
 28            //copy data over insert padding
 29            for (int x=0; x<length2;x++)
 30            {
 31                if (x<length)
 32                {
 33                    source2[x]=source[x];
 34                }

 35                else
 36                {
 37                    source2[x]=0;
 38                }

 39            }

 40            
 41            byte b1, b2, b3;
 42            byte temp, temp1, temp2, temp3, temp4;
 43            byte[] buffer=new byte[blockCount*4];
 44            char[] result=new char[blockCount*4];
 45            for (int x=0;x<blockCount;x++)
 46            {
 47                b1=source2[x*3];
 48                b2=source2[x*3+1];
 49                b3=source2[x*3+2];
 50
 51                temp1=(byte)((b1 & 252)>>2);//first
 52
 53                temp=(byte)((b1 & 3)<<4);
 54                temp2=(byte)((b2 & 240)>>4);
 55                temp2+=temp; //second
 56
 57                temp=(byte)((b2 & 15)<<2);
 58                temp3=(byte)((b3 & 192)>>6);
 59                temp3+=temp; //third
 60
 61                temp4=(byte)(b3 & 63); //fourth
 62
 63                buffer[x*4]=temp1;
 64                buffer[x*4+1]=temp2;
 65                buffer[x*4+2]=temp3;
 66                buffer[x*4+3]=temp4;
 67
 68            }

 69
 70            for (int x=0; x<blockCount*4;x++)
 71            {
 72                result[x]=sixbit2char(buffer[x]);
 73            }

 74
 75            //covert last "A"s to "=", based on paddingCount
 76            switch (paddingCount)
 77            {
 78                case 0:break;
 79                case 1:result[blockCount*4-1]='=';break;
 80                case 2:result[blockCount*4-1]='=';
 81                    result[blockCount*4-2]='=';
 82                    break;
 83                default:break;
 84            }

 85            return result;
 86        }

 87
 88        private char sixbit2char(byte b)
 89        {
 90            char[] lookupTable=new char[64]
 91                    {    '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',
 92                        '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',
 93                        '0','1','2','3','4','5','6','7','8','9','+','/'}
;
 94
 95            if((b>=0&&(b<=63))
 96            {
 97                return lookupTable[(int)b];
 98            }

 99            else
100            {
101                //should not happen;
102                return ' ';
103            }

104        }

105    }

106

最好是在encode前和decode后都用utf8转换一下,中文就没问题了