toBase64() static private method

static private toBase64 ( byte buf, int start, int length ) : byte[]
buf byte
start int
length int
return byte[]
Esempio n. 1
0
 public void writeSECSHPublicKey(Stream outs, String comment)
 {
     byte[] pubblob = getPublicKeyBlob();
     byte[] pub     = Util.toBase64(pubblob, 0, pubblob.Length);
     try
     {
         Write(outs, Util.getBytes("---- BEGIN SSH2 PUBLIC KEY ----")); Write(outs, cr);
         Write(outs, Util.getBytes("Comment: \"" + comment + "\"")); Write(outs, cr);
         int index = 0;
         while (index < pub.Length)
         {
             int len = 70;
             if ((pub.Length - index) < len)
             {
                 len = pub.Length - index;
             }
             outs.Write(pub, index, len); Write(outs, cr);
             index += len;
         }
         Write(outs, Util.getBytes("---- END SSH2 PUBLIC KEY ----")); Write(outs, cr);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Esempio n. 2
0
        public void writePrivateKey(Stream outs)
        {
            byte[] plain = getPrivateKey();
            var    _iv   = new byte[1][];

            byte[] encoded = encrypt(plain, _iv);
            byte[] iv      = _iv[0];
            byte[] prv     = Util.toBase64(encoded, 0, encoded.Length);

            try
            {
                Write(outs, getBegin());
                Write(outs, cr);
                if (passphrase != null)
                {
                    Write(outs, header[0]);
                    Write(outs, cr);
                    Write(outs, header[1]);
                    for (int j = 0; j < iv.Length; j++)
                    {
                        outs.WriteByte(b2a((byte)((iv[j] >> 4) & 0x0f)));
                        outs.WriteByte(b2a((byte)(iv[j] & 0x0f)));
                    }
                    Write(outs, cr);
                    Write(outs, cr);
                }
                int i = 0;
                while (i < prv.Length)
                {
                    if (i + 64 < prv.Length)
                    {
                        outs.Write(prv, i, 64);
                        Write(outs, cr);
                        i += 64;
                        continue;
                    }
                    outs.Write(prv, i, prv.Length - i);
                    Write(outs, cr);
                    break;
                }
                Write(outs, getEnd());
                Write(outs, cr);
                //outs.close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Esempio n. 3
0
 public void writePublicKey(Stream outs, String comment)
 {
     byte[] pubblob = getPublicKeyBlob();
     byte[] pub     = Util.toBase64(pubblob, 0, pubblob.Length);
     try
     {
         Write(outs, getKeyTypeName()); Write(outs, space);
         outs.Write(pub, 0, pub.Length); Write(outs, space);
         Write(outs, Util.getBytes(comment));
         Write(outs, cr);
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Esempio n. 4
0
        public void connect(SocketFactory socket_factory, String host, int port, int timeout)
        {
            try
            {
                if (socket_factory == null)
                {
                    socket = Util.createSocket(proxy_host, proxy_port, timeout);
                    ins    = new JStream(socket.getInputStream());
                    outs   = new JStream(socket.getOutputStream());
                }
                else
                {
                    socket = socket_factory.createSocket(proxy_host, proxy_port);
                    ins    = new JStream(socket_factory.getInputStream(socket));
                    outs   = new JStream(socket_factory.getOutputStream(socket));
                }
                if (timeout > 0)
                {
                    socket.setSoTimeout(timeout);
                }
                socket.setTcpNoDelay(true);

                outs.write(new String("CONNECT " + host + ":" + port + " HTTP/1.0\r\n").getBytes());

                if (user != null && passwd != null)
                {
                    byte[] _code = (user + ":" + passwd).getBytes();
                    _code = Util.toBase64(_code, 0, _code.Length);
                    outs.write(new String("Proxy-Authorization: Basic ").getBytes());
                    outs.write(_code);
                    outs.write(new String("\r\n").getBytes());
                }

                outs.write(new String("\r\n").getBytes());
                outs.flush();

                int foo = 0;

                var sb = new StringBuffer();
                while (foo >= 0)
                {
                    foo = ins.read();
                    if (foo != 13)
                    {
                        sb.append((char)foo);
                        continue;
                    }
                    foo = ins.read();
                    if (foo != 10)
                    {
                        continue;
                    }
                    break;
                }
                if (foo < 0)
                {
                    throw new IOException();
                }

                String response = sb.toString();
                String reason   = "Unknow reason";
                int    code     = -1;
                try
                {
                    foo = response.indexOf(' ');
                    int bar = response.indexOf(' ', foo + 1);
                    code   = Integer.parseInt(response.substring(foo + 1, bar));
                    reason = response.substring(bar + 1);
                }
                catch (Exception)
                {
                }
                if (code != 200)
                {
                    throw new IOException("proxy error: " + reason);
                }

                /*
                 *              while(foo>=0){
                 *                foo=in.read(); if(foo!=13) continue;
                 *                foo=in.read(); if(foo!=10) continue;
                 *                foo=in.read(); if(foo!=13) continue;
                 *                foo=in.read(); if(foo!=10) continue;
                 *                break;
                 *              }
                 */

                int count = 0;
                while (true)
                {
                    count = 0;
                    while (foo >= 0)
                    {
                        foo = ins.read();
                        if (foo != 13)
                        {
                            count++;
                            continue;
                        }
                        foo = ins.read();
                        if (foo != 10)
                        {
                            continue;
                        }
                        break;
                    }
                    if (foo < 0)
                    {
                        throw new IOException();
                    }
                    if (count == 0)
                    {
                        break;
                    }
                }
            }
            catch (RuntimeException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                try
                {
                    if (socket != null)
                    {
                        socket.close();
                    }
                }
                catch (Exception)
                {
                }
                String message = "ProxyHTTP: " + e.toString();
                throw e;
            }
        }
Esempio n. 5
0
        public void connect(SocketFactory socket_factory, String host, int port, int timeout)
        {
            try
            {
                if (socket_factory == null)
                {
                    socket = Util.createSocket(proxy_host, proxy_port, timeout);
                    ins    = new JStream(new NetworkStream(socket));
                    outs   = new JStream(new NetworkStream(socket));
                }
                else
                {
                    socket = socket_factory.createSocket(proxy_host, proxy_port);
                    ins    = new JStream(socket_factory.getInputStream(socket));
                    outs   = new JStream(socket_factory.getOutputStream(socket));
                }
                if (timeout > 0)
                {
                    socket.ReceiveTimeout = timeout;
                    socket.SendTimeout    = timeout;
                }
                socket.NoDelay = true;

                outs.write("CONNECT " + host + ":" + port + " HTTP/1.0\r\n");

                if (user != null && passwd != null)
                {
                    byte[] _code = Encoding.UTF8.GetBytes(user + ":" + passwd);
                    _code = Util.toBase64(_code, 0, _code.Length);
                    outs.write("Proxy-Authorization: Basic ");
                    outs.write(_code);
                    outs.write("\r\n");
                }

                outs.write("\r\n");
                outs.flush();

                int foo = 0;

                string sb = "";
                while (foo >= 0)
                {
                    foo = ins.read(); if (foo != 13)
                    {
                        sb += ((char)foo);  continue;
                    }
                    foo = ins.read(); if (foo != 10)
                    {
                        continue;
                    }
                    break;
                }
                if (foo < 0)
                {
                    throw new System.IO.IOException();
                }

                String response = sb;
                String reason   = "Unknow reason";
                int    code     = -1;
                try
                {
                    foo = response.IndexOf(' ');
                    int bar = response.IndexOf(' ', foo + 1);
                    code   = System.Convert.ToInt32(response.Substring(foo + 1, bar));
                    reason = response.Substring(bar + 1);
                }
                catch (Exception)
                {
                }
                if (code != 200)
                {
                    throw new System.IO.IOException("proxy error: " + reason);
                }

                /*
                 * while(foo>=0){
                 * foo=in.read(); if(foo!=13) continue;
                 * foo=in.read(); if(foo!=10) continue;
                 * foo=in.read(); if(foo!=13) continue;
                 * foo=in.read(); if(foo!=10) continue;
                 * break;
                 * }
                 */

                int count = 0;
                while (true)
                {
                    count = 0;
                    while (foo >= 0)
                    {
                        foo = ins.read(); if (foo != 13)
                        {
                            count++;  continue;
                        }
                        foo = ins.read(); if (foo != 10)
                        {
                            continue;
                        }
                        break;
                    }
                    if (foo < 0)
                    {
                        throw new System.IO.IOException();
                    }
                    if (count == 0)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                try{ if (socket != null)
                     {
                         socket.Close();
                     }
                }
                catch (Exception)
                {
                }
                String message = "ProxyHTTP: " + e.ToString();
                throw e;
            }
        }
Esempio n. 6
0
        public void Connect(SocketFactory socketFactory, String host, int port, int timeout)
        {
            try
            {
                if (socketFactory == null)
                {
                    Socket       = Util.createSocket(Host, Port, timeout);
                    inputStream  = new JStream(Socket.getInputStream());
                    outputStream = new JStream(Socket.getOutputStream());
                }
                else
                {
                    Socket       = socketFactory.createSocket(Host, Port);
                    inputStream  = new JStream(socketFactory.getInputStream(Socket));
                    outputStream = new JStream(socketFactory.getOutputStream(Socket));
                }
                if (timeout > 0)
                {
                    Socket.setSoTimeout(timeout);
                }
                Socket.setTcpNoDelay(true);

                outputStream.Write(("CONNECT " + host + ":" + port + " HTTP/1.0\r\n").GetBytes());

                if (User != null && Password != null)
                {
                    byte[] _code = (User + ":" + Password).GetBytes();
                    _code = Util.toBase64(_code, 0, _code.Length);
                    outputStream.Write("Proxy-Authorization: Basic ".GetBytes());
                    outputStream.Write(_code);
                    outputStream.Write("\r\n".GetBytes());
                }

                outputStream.Write("\r\n".GetBytes());
                outputStream.flush();

                int foo = 0;

                StringBuilder sb = new StringBuilder();
                while (foo >= 0)
                {
                    foo = inputStream.read();
                    if (foo != 13)
                    {
                        sb.Append((char)foo);
                        continue;
                    }
                    foo = inputStream.read();
                    if (foo != 10)
                    {
                        continue;
                    }
                    break;
                }
                if (foo < 0)
                {
                    throw new IOException();
                }

                String response = sb.ToString();
                String reason   = "Unknow reason";
                int    code     = -1;
                try
                {
                    foo = response.IndexOf(' ');
                    int bar = response.IndexOf(' ', foo + 1);
                    code   = Int32.Parse(response.Substring(foo + 1, bar));
                    reason = response.Substring(bar + 1);
                }
                catch (Exception) { }

                if (code != 200)
                {
                    throw new IOException("proxy error: " + reason);
                }

                int count = 0;
                while (true)
                {
                    count = 0;
                    while (foo >= 0)
                    {
                        foo = inputStream.read();
                        if (foo != 13)
                        {
                            count++;
                            continue;
                        }
                        foo = inputStream.read();
                        if (foo != 10)
                        {
                            continue;
                        }
                        break;
                    }
                    if (foo < 0)
                    {
                        throw new IOException();
                    }
                    if (count == 0)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                try
                {
                    if (Socket != null)
                    {
                        Socket.Close();
                    }
                }
                catch (Exception) { }

                String message = "ProxyHTTP: " + e;
                throw e;
            }
        }