Пример #1
0
        public void Test_Base64()
        {
            var bytes = Encoding.ASCII.GetBytes("");
            var s     = TextConversion.ToBase64String(bytes);

            bytes = Encoding.ASCII.GetBytes("H");
            s     = TextConversion.ToBase64String(bytes);
            Assert.AreEqual("SA==", s);
            Assert.AreEqual(TextConversion.FromBase64String(s), bytes);

            bytes = Encoding.ASCII.GetBytes("He");
            s     = TextConversion.ToBase64String(bytes);
            Assert.AreEqual("SGU=", s);
            Assert.AreEqual(TextConversion.FromBase64String(s), bytes);

            bytes = Encoding.ASCII.GetBytes("Hel");
            s     = TextConversion.ToBase64String(bytes);
            Assert.AreEqual("SGVs", s);
            Assert.AreEqual(TextConversion.FromBase64String(s), bytes);

            bytes = Encoding.ASCII.GetBytes("Hell");
            s     = TextConversion.ToBase64String(bytes);
            Assert.AreEqual("SGVsbA==", s);
            Assert.AreEqual(TextConversion.FromBase64String(s), bytes);

            bytes = Encoding.ASCII.GetBytes("Hello");
            s     = TextConversion.ToBase64String(bytes);
            Assert.AreEqual("SGVsbG8=", s);
            Assert.AreEqual(TextConversion.FromBase64String(s), bytes);
        }
Пример #2
0
        public override void Process(Command command)
        {
            var options = this.LoadOptions <CommandOptions>((TextCommand)command);

            if (options.Modulus != null && options.Exponent != null && options.Algorithm != null)
            {
                var symmetric = new RijndaelManaged();
                symmetric.GenerateKey();

                var modulus  = TextConversion.FromBase64String(options.Modulus);
                var exponent = TextConversion.FromBase64String(options.Exponent);

                var rsaparameters = new RSAParameters();
                rsaparameters.Modulus  = modulus;
                rsaparameters.Exponent = exponent;

                var rsa = new RSACryptoServiceProvider();
                rsa.ImportParameters(rsaparameters);

                var myrsa = new RSACryptoServiceProvider();

                rsaparameters = myrsa.ExportParameters(false);

                Connection.WriteOk
                (
                    "Modulus",
                    TextConversion.ToBase64String(rsaparameters.Modulus),
                    "Exponent",
                    TextConversion.ToBase64String(rsaparameters.Exponent),
                    "IV",
                    TextConversion.ToBase64String(new RSAPKCS1KeyExchangeFormatter(rsa).CreateKeyExchange(symmetric.IV)),
                    "KeyExchange",
                    TextConversion.ToBase64String(new RSAPKCS1KeyExchangeFormatter(rsa).CreateKeyExchange(symmetric.Key))
                );

                Connection.Flush();

                if (options.WaitForReady)
                {
                    Connection.ReadReady();
                }
            }
            else if (options.Compress)
            {
                Connection.WriteOk();
                Connection.Flush();

                this.Connection.ReadStream  = new InteractiveInflaterInputStream(this.Connection.RawReadStream, new Inflater(true));
                this.Connection.WriteStream = new InteractiveDeflaterOutputStream(this.Connection.RawWriteStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true), 512);

                if (options.WaitForReady)
                {
                    Connection.ReadReady();
                }
            }
        }
        public static string ToString(object value)
        {
            if (Array.IndexOf(m_TypeNames, value.GetType()) < 0)
            {
                throw new NotSupportedException(value.GetType().ToString());
            }

            if (value is DateTime)
            {
                return(((DateTime)value).ToUniversalTime().ToString(DateTimeFormats.SortableUtcDateTimeFormatWithFractionSecondsString));
            }
            else if (value is bool)
            {
                return(((bool)value) ? "t" : "f");
            }
            else if (value is byte[])
            {
                return(TextConversion.ToBase64String((byte[])value));
            }
            else
            {
                return(Convert.ToString(value));
            }
        }