Google
 

Wednesday, May 23, 2007

How to convert between string and byte array in c#?

 internal class EncryptionController
    {
        public static string Encrypt(string data)
        {
            try
            {
                byte[] bytes = Encoding.Unicode.GetBytes(data);
                return Convert.ToBase64String(bytes);
            }
            catch (Exception ex)
            {
                Exceptions.LogException( new Exception(string.Format("To encrpt the '{0}' data is failed!",data)));
                return null;
            }
        }
      
        public static string Decrypt(string encryptedData)
        {
            try
            {
                byte[] bytes = Convert.FromBase64String (encryptedData);
                return Encoding.Unicode.GetString(bytes);               
            }
            catch (Exception ex)
            {
                Exceptions.LogException(new Exception( string.Format("To dencrpt the '{0}' data is failed!", encryptedData)));
                return null;
            }
           
        }

    }

--
Happy day, happy life!

No comments: