You must Sign In to post a response.
  • Category: ASP.NET

    How to Encryption a string


    Are you looking for a way to Encrypt a string ? want to keep encrypt string length should be the same as the input string ? then read this thread to know more about it



    hi,
    i want to code for the encrypted string length should be the same as the input(plain) text string length in asp.net c#
    give me sample code
  • #728258
    The following code snippet shows how to encrypt and decrypt the password using C#.
    Encryption

    public string encryptPassword(string strText)
    {
    return Encrypt(strText, "&%#@?,:*");
    }

    Decryption

    public string decryptPassword(string str)
    {
    return Decrypt(str, "&%#@?,:*");
    }

    Encryption using byte array

    private string Encrypt(string strText, string strEncrypt)
    {
    byte[] byKey = new byte[20];
    byte[] dv ={ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    try
    {
    byKey = System.Text.Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    byte[] inputArray = System.Text.Encoding.UTF8.GetBytes(strText);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, dv), CryptoStreamMode.Write);
    cs.Write(inputArray, 0, inputArray.Length);
    cs.FlushFinalBlock();
    return Convert.ToBase64String(ms.ToArray());
    }
    catch (Exception ex)
    {
    throw ex;
    }

    Decryption using byte array

    public string decryptPassword(string str)
    {
    return Decrypt(str, "&%#@?,:*");
    }

    Method 2:

    private string Decrypt(string strText, string strEncrypt)
    {
    byte[] bKey = new byte[20];
    byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
    try
    {
    bKey = System.Text.Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    Byte[] inputByteArray = inputByteArray = Convert.FromBase64String(strText);
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(bKey, IV), CryptoStreamMode.Write);
    cs.Write(inputByteArray, 0, inputByteArray.Length);
    cs.FlushFinalBlock();
    System.Text.Encoding encoding = System.Text.Encoding.UTF8;
    return encoding.GetString(ms.ToArray());
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #728265
    Hi Ramadevi,

    Here I've given an example of encrypt and decrypt string.

            public static string EncryptMyString(string MyString, string MySecurity)
    {
    byte[] inputArray = UTF8Encoding.UTF8.GetBytes(MyString);
    TripleDESCryptoServiceProvider tdspMyPro = new TripleDESCryptoServiceProvider();
    tdspMyPro.Key = UTF8Encoding.UTF8.GetBytes(MySecurity);
    tdspMyPro.Mode = CipherMode.ECB;
    tdspMyPro.Padding = PaddingMode.PKCS7;
    ICryptoTransform ictMyPro = tdspMyPro.CreateEncryptor();
    byte[] resultArray = ictMyPro.TransformFinalBlock(inputArray, 0, inputArray.Length);
    tdspMyPro.Clear();
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }

    public static string DecryptMyString(string MyString, string MySecurity)
    {
    byte[] inputArray = Convert.FromBase64String(MyString);
    TripleDESCryptoServiceProvider tdspMyPro = new TripleDESCryptoServiceProvider();
    tdspMyPro.Key = UTF8Encoding.UTF8.GetBytes(MySecurity);
    tdspMyPro.Mode = CipherMode.ECB;
    tdspMyPro.Padding = PaddingMode.PKCS7;
    ICryptoTransform ictMyPro = tdspMyPro.CreateDecryptor();
    byte[] resultArray = ictMyPro.TransformFinalBlock(inputArray, 0, inputArray.Length);
    tdspMyPro.Clear();
    return UTF8Encoding.UTF8.GetString(resultArray);
    }



    Best regards
    Bashar

  • #728289
    You can encrypt a string with the help of Encryption algorithm but the length may be get short or longer depend uoon the key used for encryption.
    i have write an article for encryption, same code snippet i have paste it for you.

    public static string Encrypt_text(string input, string key)
    {
    byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
    TripleDESCryptoServiceProvider objtripleDESProvider = new TripleDESCryptoServiceProvider();
    //objtripleDESProvider.GenerateKey();
    //we can provide a key or we can generate a key by using GenerateKey() method of TripleDES provider class
    tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
    //MessageBox.Show(Convert.ToBase64String(objtripleDESProvider.Key,0,objtripleDESProvider.Key.Length));
    objtripleDESProvider.Mode = CipherMode.ECB;
    objtripleDESProvider.Padding = PaddingMode.PKCS7;
    ICryptoTransform cTransform = objtripleDESProvider.CreateEncryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
    objtripleDESProvider.Clear();
    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #728368
    I used this code under button click event . You need to set your event as program
    private void btnt_Click(object sender, EventArgs e)
    {
    myOutputTxt.Text = setCryptographer.Encrypt(myInputTxt.Text,SetKey());
    }
    public static string Encrypt(string stringToEncryption)
    {
    try
    {
    return Encrypt(stringToEncryption, _key);
    }

    catch (Exception exa)
    {
    return "Invalid Input. " + exa.Message;
    }
    }
    I refer to link http://www.devx.com/tips/Tip/5676


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.