コード例 #1
0
        public void TestBase64StreamWrite()
        {
            byte[] data = new byte[256];
            for (int i = 0; i < data.Length; i++)
                data[i] = (byte)i;

            using (Stream mem = new MemoryStream())
            {
                using (Stream io = new Base64Stream(new NonClosingStream(mem), CryptoStreamMode.Write))
                    io.Write(data, 0, data.Length);


                Assert.AreEqual(((data.Length + 2) / 3 * 4), mem.Position);
                mem.Position = 0;
                string test = new StreamReader(mem).ReadToEnd();
                Assert.AreEqual(Convert.ToBase64String(data), test);
            }
        }
コード例 #2
0
        public void TestBase64StreamRead()
        {
            string encoded = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/9w==";
            byte[] data = Convert.FromBase64String(encoded);
            byte[] test;
            using (Stream io = new Base64Stream(new MemoryStream(Encoding.ASCII.GetBytes(encoded)), CryptoStreamMode.Read))
                test = IOStream.Read(io, data.Length);
            Assert.AreEqual(data, test);

            using (Stream io = new Base64Stream(new MemoryStream(Encoding.ASCII.GetBytes(encoded.TrimEnd('='))), CryptoStreamMode.Read))
                test = IOStream.Read(io, data.Length);
            Assert.AreEqual(data, test);
        }