Exemplo n.º 1
0
            // Encrypt the string.
            public static byte[] Encrypt(string PlainText)
            {
                try
                {
                    if (PlainText == null)
                    {
                        return(null);
                    }
                    s_des = new TripleDESCryptoServiceProvider();
                    int block = s_des.BlockSize;
                    int j     = s_des.KeySize;
                    //s_iv = RandomString(block);


                    byte[] k  = ConvertStringToByteArray(s_key);
                    byte[] iv = ConvertStringToByteArray(s_iv);
                    //s_des.Key = ConvertStringToByteArray(s_key);
                    //s_des.IV = ConvertStringToByteArray(s_iv);

                    // Create a memory stream.
                    ClusteredMemoryStream ms = new ClusteredMemoryStream();

                    // Create a CryptoStream using the memory stream and the
                    // CSP DES key.
                    CryptoStream encStream = new CryptoStream(ms, s_des.CreateEncryptor(k, iv), CryptoStreamMode.Write);

                    // Create a StreamWriter to write a string
                    // to the stream.
                    StreamWriter sw = new StreamWriter(encStream);

                    // Write the plaintext to the stream.
                    sw.WriteLine(PlainText);

                    // Close the StreamWriter and CryptoStream.
                    sw.Close();
                    encStream.Close();

                    // Get an array of bytes that represents
                    // the memory stream.
                    byte[] buffer = ms.ToArray();

                    // Close the memory stream.
                    ms.Close();

                    // Return the encrypted byte array.
                    return(buffer);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                return(null);
            }
Exemplo n.º 2
0
        public bool SendMessage(object message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            //first serialize the message using channel formatter
            byte[] serailizedMessage = _formatter.Serialize(message);

            byte[] msgLength = UTF8Encoding.UTF8.GetBytes(serailizedMessage.Length.ToString());

            //message is written in a specific order as expected by Socket server
            ClusteredMemoryStream stream = new ClusteredMemoryStream();

            //stream.Position = 20;//skip discarding buffer
            stream.Write(msgLength, 0, msgLength.Length);
            stream.Position = 10;
            stream.Write(serailizedMessage, 0, serailizedMessage.Length);

            byte[] finalBuffer = stream.ToArray();
            stream.Close();

            try
            {
                if (EnsureConnected())
                {
                    try
                    {
                        _connection.Send(finalBuffer, 0, finalBuffer.Length);
                        return(true);
                    }
                    catch (ConnectionException)
                    {
                        if (EnsureConnected())
                        {
                            _connection.Send(finalBuffer, 0, finalBuffer.Length);
                            return(true);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new ChannelException(e.Message, e);
            }

            return(false);
        }
Exemplo n.º 3
0
            /// <summary>
            /// Encrypt user provided key with the default key stored; This key is obfuscated
            /// </summary>
            /// <param name="key">Key</param>
            /// <returns>encrypted string</returns>
            internal static byte[] EncryptData(byte[] data)
            {
                try
                {
                    //byte[] data = ASCIIEncoding.ASCII.GetBytes(key);
                    s_des = new TripleDESCryptoServiceProvider();
                    int    i  = s_des.BlockSize;
                    int    j  = s_des.KeySize;
                    byte[] k  = ConvertStringToByteArray(s_key);
                    byte[] IV = ConvertStringToByteArray(s_iv);
                    //s_des.Key = ConvertStringToByteArray(key);
                    //s_des.IV = ConvertStringToByteArray(s_iv);

                    // Create a memory stream.
                    ClusteredMemoryStream ms = new ClusteredMemoryStream();

                    // Create a CryptoStream using the memory stream and the
                    // CSP DES key.
                    CryptoStream encStream = new CryptoStream(ms, s_des.CreateEncryptor(k, IV), CryptoStreamMode.Write);

                    encStream.Write(data, 0, data.Length);
                    encStream.FlushFinalBlock();

                    // Get an array of bytes from the
                    // MemoryStream that holds the
                    // encrypted data.
                    byte[] ret = ms.ToArray();

                    // Close the memory stream.
                    ms.Close();
                    encStream.Close();

                    // Return the encrypted byte array.

                    //string temp = Convert.ToBase64String(ret, Base64FormattingOptions.None);
                    // temp = System.Security.SecurityElement.Escape(temp);
                    return(ret);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
Exemplo n.º 4
0
            public static byte[] DecryptData(byte[] encodedData)
            {
                try
                {
                    // byte[] data = Convert.FromBase64String(encodedkey);
                    s_des = new TripleDESCryptoServiceProvider();
                    byte[] k  = ConvertStringToByteArray(s_key);
                    byte[] IV = ConvertStringToByteArray(s_iv);


                    // Create a memory stream to the passed buffer.
                    ClusteredMemoryStream ms = new ClusteredMemoryStream(encodedData);

                    // Create a CryptoStream using the memory stream and the
                    // CSP DES key.
                    CryptoStream encStream = new CryptoStream(ms, s_des.CreateDecryptor(k, IV), CryptoStreamMode.Read);

                    byte[] fromEncrypt = new byte[encodedData.Length];

                    // Read the decrypted data out of the crypto stream
                    // and place it into the temporary buffer.
                    int length = encStream.Read(fromEncrypt, 0, fromEncrypt.Length);

                    // Close the streams.
                    encStream.Close();
                    ms.Close();

                    byte[] ret = new byte[length];
                    Array.Copy(fromEncrypt, ret, length);
                    return(ret);//ASCIIEncoding.ASCII.GetString(ret);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }