Solved

client-server application encrypt/decrypt

Posted on 2014-12-26
8
207 Views
Last Modified: 2015-01-03
Hi,

I have client server application, I am trying to encrypt all data communication between client and server. Please suggest me how can i do it? Should I use System.Security.Cryptography class or there is any easy way too.

Please guide,

Thanks
0
Comment
Question by:Bertrand Russell
  • 4
  • 3
8 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40518403
Well, it depends on your concrete type of communications and the concrete requirements.

Do you need protocol or message encryption? The first could be done by tunneling, e.g. SSL/SSH. So this can be done by the OS.
Message encryption means that you application needs to handled by you application.

So without further information, it's hard to guess.
0
 

Author Comment

by:Bertrand Russell
ID: 40518433
thanks for message. I need message level encryption
Please guide

Thanks
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40518454
What kind of communications? Web Services (WCF) has already a built-in mechanism for that:

E.g.

<bindings>
	<wsHttpBinding>
		<binding name="netTcpEndpointBinding">
			<security mode="message"></security>
		</binding>
	</wsHttpBinding>
</bindings>

Open in new window


See also:

* Message Security in WCF
* Chapter 7: Message and Transport Security
0
 

Author Comment

by:Bertrand Russell
ID: 40518485
It is windows service, so client is c# interface/application and server is a windows service. Please guide how to make message level encryption! thx
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Expert Comment

by:ste5an
ID: 40518535
In this general case you need to encrypt/decrypt each message. Here you use the System.Security.Cryptography namspace of the .Net framework.

See Encrypting and Decrypting Data.
0
 

Author Comment

by:Bertrand Russell
ID: 40518565
I have tried this code, it is encrypting and decrypting data well on local machine but when I send data to remote machine, the decryption fails. What is wrong in this code that make it not working on remote machine while working on local is fine!

  public static byte[] Key = new byte[]{0x43, 0x72, 0x6e, 0x6d, 0x54, 0x4d, 0x65,
                                      0x94, 0x16, 0x32, 0x44, 0x84, 0x7e, 0x18,
                                      0x64, 0x76, 0x6e, 0x63, 0x64, 0x7a, 0x5f,
                                      0x84, 0x7f, 0x9a};

        public static string Encrypt(string toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);


            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                //keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                keyArray = hashmd5.ComputeHash(Key);

                hashmd5.Clear();
            }
            else
                keyArray = Key;

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray =
              cTransform.TransformFinalBlock(toEncryptArray, 0,
              toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor
            tdes.Clear();
            //Return the encrypted data into unreadable string format
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        public static string Decrypt(string cipherString, bool useHashing)
        {
            byte[] keyArray;
            //get the byte code of the string

            byte[] toEncryptArray = Convert.FromBase64String(cipherString);



            if (useHashing)
            {
                //if hashing was used get the hash code with regards to your key
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                //keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                keyArray = hashmd5.ComputeHash(Key);
                //release any resource held by the MD5CryptoServiceProvider

                hashmd5.Clear();
            }
            else
            {
                keyArray = Key;
            }

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            //set the secret key for the tripleDES algorithm
            tdes.Key = keyArray;

            tdes.Mode = CipherMode.ECB;
            //padding mode(if any extra byte added)
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(
                                 toEncryptArray, 0, toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor                
            tdes.Clear();
            //return the Clear decrypted TEXT
            return UTF8Encoding.UTF8.GetString(resultArray);
        }

        public static Byte[] Encrypt(Byte[] toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            // byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
            byte[] toEncryptArray = toEncrypt;

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                //keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                keyArray = hashmd5.ComputeHash(Key);

                hashmd5.Clear();
            }
            else
                keyArray = Key;

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            //set the secret key for the tripleDES algorithm
            tdes.Key = keyArray;
            //mode of operation. there are other 4 modes.
            //We choose ECB(Electronic code Book)
            tdes.Mode = CipherMode.ECB;
            //padding mode(if any extra byte added)

            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            //transform the specified region of bytes array to resultArray
            byte[] resultArray =
              cTransform.TransformFinalBlock(toEncryptArray, 0,
              toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor
            tdes.Clear();
            //Return the encrypted data into unreadable string format
            // return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            return resultArray;
        }

        public static Byte[] Decrypt(Byte[] cipherString, bool useHashing)
        {
            byte[] keyArray;
            //get the byte code of the string

            // byte[] toEncryptArray = Convert.FromBase64String(cipherString);
            byte[] toEncryptArray = cipherString;



            if (useHashing)
            {
                //if hashing was used get the hash code with regards to your key
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                //keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                keyArray = hashmd5.ComputeHash(Key);
                //release any resource held by the MD5CryptoServiceProvider

                hashmd5.Clear();
            }
            else
            {
                //if hashing was not implemented get the byte code of the key
                //keyArray = UTF8Encoding.UTF8.GetBytes(key);
                keyArray = Key;
            }

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            //set the secret key for the tripleDES algorithm
            tdes.Key = keyArray;
            //mode of operation. there are other 4 modes. 
            //We choose ECB(Electronic code Book)

            tdes.Mode = CipherMode.ECB;
            //padding mode(if any extra byte added)
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(
                                 toEncryptArray, 0, toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor                
            tdes.Clear();
            //return the Clear decrypted TEXT
            // return UTF8Encoding.UTF8.GetString(resultArray);
            return resultArray;
        }

Open in new window

0
 
LVL 1

Accepted Solution

by:
mkk3939 earned 500 total points
ID: 40529576
key on both sides should be same, otherwise it can make problem. Otherwise, code is fine, I checked it at my end.
0
 

Author Closing Comment

by:Bertrand Russell
ID: 40529577
Yes, thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now