public static string ToSOAP(this object Object, string FileToSaveTo = "", Encoding EncodingUsing = null)
        {
            if (Object == null)
            {
                throw new ArgumentException("Object can not be null");
            }
            if (EncodingUsing == null)
            {
                EncodingUsing = new ASCIIEncoding();
            }
            string SOAP = "";

            using (MemoryStream Stream = new MemoryStream())
            {
                SoapFormatter Serializer = new SoapFormatter();
                Serializer.Serialize(Stream, Object);
                Stream.Flush();
                SOAP = EncodingUsing.GetString(Stream.GetBuffer(), 0, (int)Stream.Position);
            }
            if (!string.IsNullOrEmpty(FileToSaveTo))
            {
                new FileInfo(FileToSaveTo).Save(SOAP);
            }
            return(SOAP);
        }
        /// <summary>
        /// Converts an object to JSON
        /// </summary>
        /// <param name="Object">Object to convert</param>
        /// <param name="FileToSaveTo">File to save the XML to (optional)</param>
        /// <param name="EncodingUsing">Encoding that the XML should be saved/returned as (defaults to ASCII)</param>
        /// <returns>The object converted to a JSON string</returns>
        public static string ToJSON(this object Object, string FileToSaveTo = "", Encoding EncodingUsing = null)
        {
            if (Object == null)
            {
                throw new ArgumentNullException("Object");
            }
            if (EncodingUsing == null)
            {
                EncodingUsing = new ASCIIEncoding();
            }
            string JSON = "";

            using (MemoryStream Stream = new MemoryStream())
            {
                DataContractJsonSerializer Serializer = new DataContractJsonSerializer(Object.GetType());
                Serializer.WriteObject(Stream, Object);
                Stream.Flush();
                JSON = EncodingUsing.GetString(Stream.GetBuffer(), 0, (int)Stream.Position);
            }
            if (!string.IsNullOrEmpty(FileToSaveTo))
            {
                new FileInfo(FileToSaveTo).Save(JSON);
            }
            return(JSON);
        }
        /// <summary>
        /// Converts an object to XML
        /// </summary>
        /// <param name="Object">Object to convert</param>
        /// <param name="FileToSaveTo">File to save the XML to (optional)</param>
        /// <param name="EncodingUsing">Encoding that the XML should be saved/returned as (defaults to ASCII)</param>
        /// <returns>string representation of the object in XML format</returns>
        public static string ToXML(this object Object, string FileToSaveTo = "", Encoding EncodingUsing = null)
        {
            if (Object == null)
            {
                throw new ArgumentNullException("Object");
            }
            if (EncodingUsing == null)
            {
                EncodingUsing = new ASCIIEncoding();
            }
            string XML = "";

            using (MemoryStream Stream = new MemoryStream())
            {
                XmlSerializer Serializer = new XmlSerializer(Object.GetType());
                Serializer.Serialize(Stream, Object);
                Stream.Flush();
                XML = EncodingUsing.GetString(Stream.GetBuffer(), 0, (int)Stream.Position);
            }
            if (!string.IsNullOrEmpty(FileToSaveTo))
            {
                new FileInfo(FileToSaveTo).Save(XML);
            }
            return(XML);
        }
示例#4
0
        /// <summary>
        /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
        /// </summary>
        /// <param name="decryptString">待解密密文</param>
        /// <param name="decryptKey">解密密钥</param>
        /// <param name="key">初始化向量</param>
        /// <returns></returns>
        public static string AESDecrypt(string decryptString, string decryptKey, string key = "", Encoding EncodingUsing = null)
        {
            if (EncodingUsing == null)
            {
                EncodingUsing = new UTF8Encoding();
            }

            //将密钥填充或截取,以达到32位
            decryptKey = decryptKey.PadRight(32, ' ');
            RijndaelManaged rijndaelProvider = new RijndaelManaged();

            //获取或设置对称算法密钥
            rijndaelProvider.Key = EncodingUsing.GetBytes(decryptKey.Substring(0, 32));
            if (!string.IsNullOrWhiteSpace(key))
            {
                _aesKeys = Convert.FromBase64String(key);
            }
            rijndaelProvider.IV = _aesKeys;
            //创建对称密器对象
            ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();

            byte[] inputData     = Convert.FromBase64String(decryptString);
            byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);

            return(EncodingUsing.GetString(decryptedData));
        }
示例#5
0
        /// <summary>
        /// 加密实现
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        public static string MD5ToHexString(string strText, Encoding EncodingUsing = null)
        {
            if (EncodingUsing == null)
            {
                EncodingUsing = new UTF8Encoding();
            }
            byte[] data = EncodingUsing.GetBytes(strText);
            MD5    md5  = new MD5CryptoServiceProvider();

            byte[] result = md5.ComputeHash(data);
            string t      = "";

            for (int i = 0; i < result.Length; i++)
            {
                if (result[i] > 0xf)
                {
                    t += Convert.ToString(result[i], 16);
                }
                else
                {
                    t += "0";
                    t += Convert.ToString(result[i], 16);
                }
            }
            return(t);
        }
示例#6
0
 /// <summary>
 /// Converts a string to a byte array
 /// </summary>
 /// <param name="Input">input string</param>
 /// <param name="EncodingUsing">The type of encoding the string is using (defaults to UTF8)</param>
 /// <returns>the byte array representing the string</returns>
 public static byte[] ToByteArray(this string Input, Encoding EncodingUsing = null)
 {
     if (EncodingUsing == null)
     {
         EncodingUsing = new UTF8Encoding();
     }
     return(string.IsNullOrEmpty(Input) ? null : EncodingUsing.GetBytes(Input));
 }
示例#7
0
        /// <summary>
        /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合)
        /// </summary>
        /// <param name="DecryptString">待解密的密文</param>
        /// <param name="DecryptKey">解密的密钥</param>
        /// <returns>returns</returns>
        public static string DESDecrypt(string DecryptString, string DecryptKey, Encoding EncodingUsing = null)
        {
            if (string.IsNullOrEmpty(DecryptString))
            {
                throw (new Exception("密文不得为空"));
            }

            if (string.IsNullOrEmpty(DecryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }

            if (DecryptKey.Length != 8)
            {
                throw (new Exception("密钥必须为8位"));
            }

            if (EncodingUsing == null)
            {
                EncodingUsing = new UTF8Encoding();
            }

            byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            string m_strDecrypt = "";

            DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();

            try
            {
                byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);

                MemoryStream m_stream = new MemoryStream();

                CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(EncodingUsing.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);

                m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);

                m_cstream.FlushFinalBlock();

                m_strDecrypt = EncodingUsing.GetString(m_stream.ToArray());

                m_stream.Close();
                m_stream.Dispose();

                m_cstream.Close();
                m_cstream.Dispose();
            }
            catch (IOException ex) { throw ex; }
            catch (CryptographicException ex) { throw ex; }
            catch (ArgumentException ex) { throw ex; }
            catch (Exception ex) { throw ex; }
            finally { m_DESProvider.Clear(); }

            return(m_strDecrypt);
        }
 /// <summary>
 /// Appends a string to a file
 /// </summary>
 /// <param name="File">File to append to</param>
 /// <param name="Content">Content to save to the file</param>
 /// <param name="EncodingUsing">The type of encoding the string is using (defaults to ASCII)</param>
 /// <returns>The FileInfo object</returns>
 public static FileInfo Append(this FileInfo File, string Content, Encoding EncodingUsing = null)
 {
     if (File == null)
     {
         throw new ArgumentNullException("File");
     }
     if (EncodingUsing == null)
     {
         EncodingUsing = new ASCIIEncoding();
     }
     byte[] ContentBytes = EncodingUsing.GetBytes(Content);
     return(File.Append(ContentBytes));
 }
 /// <summary>
 /// Saves a string to a file (asynchronously)
 /// </summary>
 /// <param name="File">File to save to</param>
 /// <param name="Content">Content to save to the file</param>
 /// <param name="CallBack">Call back function</param>
 /// <param name="StateObject">State object</param>
 /// <param name="EncodingUsing">Encoding that the content is using (defaults to ASCII)</param>
 /// <returns>The FileInfo object</returns>
 public static FileInfo SaveAsync(this FileInfo File, string Content, AsyncCallback CallBack, object StateObject, Encoding EncodingUsing = null)
 {
     if (File == null)
     {
         throw new ArgumentNullException("File");
     }
     if (EncodingUsing == null)
     {
         EncodingUsing = new ASCIIEncoding();
     }
     byte[] ContentBytes = EncodingUsing.GetBytes(Content);
     return(File.SaveAsync(ContentBytes, CallBack, StateObject));
 }
示例#10
0
 /// <summary>
 /// Converts a byte array to a string
 /// </summary>
 /// <param name="Input">input array</param>
 /// <param name="EncodingUsing">The type of encoding the string is using (defaults to UTF8)</param>
 /// <param name="Count">Number of bytes starting at the index to convert (use -1 for the entire array starting at the index)</param>
 /// <returns>string of the byte array</returns>
 public static string ToEncodedString(this byte[] Input, Encoding EncodingUsing = null, int Index = 0, int Count = -1)
 {
     if (Input == null)
     {
         return("");
     }
     if (Count == -1)
     {
         Count = Input.Length - Index;
     }
     if (EncodingUsing == null)
     {
         EncodingUsing = new UTF8Encoding();
     }
     return(EncodingUsing.GetString(Input, Index, Count));
 }
 /// <summary>
 /// Converts a string to an object of the specified type
 /// </summary>
 /// <param name="ObjectType">Object type to return</param>
 /// <param name="Content">The string to convert</param>
 /// <param name="EncodingUsing">Encoding to use (defaults to ASCII)</param>
 /// <returns>The string converted to the specified object type</returns>
 public static object XMLToObject(this string Content, Type ObjectType, Encoding EncodingUsing = null)
 {
     if (string.IsNullOrEmpty(Content))
     {
         return(null);
     }
     if (EncodingUsing == null)
     {
         EncodingUsing = new ASCIIEncoding();
     }
     using (MemoryStream Stream = new MemoryStream(EncodingUsing.GetBytes(Content)))
     {
         XmlSerializer Serializer = new XmlSerializer(ObjectType);
         return(Serializer.Deserialize(Stream));
     }
 }
 public static object SOAPToObject(this string Content, Type ObjectType, Encoding EncodingUsing = null)
 {
     if (string.IsNullOrEmpty(Content))
     {
         return(null);
     }
     if (EncodingUsing == null)
     {
         EncodingUsing = new ASCIIEncoding();
     }
     using (MemoryStream Stream = new MemoryStream(EncodingUsing.GetBytes(Content)))
     {
         SoapFormatter Formatter = new SoapFormatter();
         return(Formatter.Deserialize(Stream));
     }
 }
 /// <summary>
 /// Converts a JSON string to an object of the specified type
 /// </summary>
 /// <param name="ObjectType">Object type to return</param>
 /// <param name="Content">The string to convert</param>
 /// <param name="EncodingUsing">Encoding to use (defaults to ASCII)</param>
 /// <returns>The string converted to the specified object type</returns>
 public static object JSONToObject(this string Content, Type ObjectType, Encoding EncodingUsing = null)
 {
     if (string.IsNullOrEmpty(Content))
     {
         return(null);
     }
     if (EncodingUsing == null)
     {
         EncodingUsing = new ASCIIEncoding();
     }
     using (MemoryStream Stream = new MemoryStream(EncodingUsing.GetBytes(Content)))
     {
         DataContractJsonSerializer Serializer = new DataContractJsonSerializer(ObjectType);
         return(Serializer.ReadObject(Stream));
     }
 }
示例#14
0
        /// <summary>
        /// RC2 加密(用变长密钥对大量数据进行加密)
        /// </summary>
        /// <param name="EncryptString">待加密密文</param>
        /// <param name="EncryptKey">加密密钥</param>
        /// <returns>returns</returns>
        public static string RC2Encrypt(string EncryptString, string EncryptKey, Encoding EncodingUsing = null)
        {
            if (string.IsNullOrEmpty(EncryptString))
            {
                throw (new Exception("密文不得为空"));
            }
            if (string.IsNullOrEmpty(EncryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }
            if (EncryptKey.Length < 5 || EncryptKey.Length > 16)
            {
                throw (new Exception("密钥必须为5-16位"));
            }
            if (EncodingUsing == null)
            {
                EncodingUsing = new UTF8Encoding();
            }
            string m_strEncrypt = "";

            byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();

            try
            {
                byte[]       m_btEncryptString = EncodingUsing.GetBytes(EncryptString);
                MemoryStream m_stream          = new MemoryStream();
                CryptoStream m_cstream         = new CryptoStream(m_stream, m_RC2Provider.CreateEncryptor(EncodingUsing.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
                m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
                m_cstream.FlushFinalBlock();
                m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
                m_stream.Close();
                m_stream.Dispose();
                m_cstream.Close();
                m_cstream.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                m_RC2Provider.Clear();
            }
            return(m_strEncrypt);
        }