zabbix
+ Reply to Thread
Results 1 to 9 of 9

Thread: How to encrypt string in c#...

  1. #1
    svr2112 Guest

    How to encrypt string in c#...

    You can use following code for encryption

    private string encryptString(string strToEncrypt)
    {
    System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
    byte[] bytes = ue.GetBytes(strToEncrypt);

    // encrypt bytes
    System.Security.Cryptography.MD5CryptoR*0; der md5 = new System.Security.Cryptography.MD5CryptoSeR*0; der();
    byte[] hashBytes = md5.ComputeHash(bytes);

    // Convert the encrypted bytes back to a string (base *6)
    string hashString = "";

    for(int i=0;i
    {
    hashString += Convert.ToString(hashBytes[i],*6).PadLefR*0;
    }

    return hashString.PadLeft(*2,'0');
    }

    _____________
    Last edited by DougN; 06-09-2015 at 06:10 PM.

  2. #2
    Join Date
    Oct 2010
    Posts
    3
    if u dont want the user to be able to see the querystring, just use action=POST instead of get in ur form tag (by default action=GET). this would hide the querystring from the user. or you could write functions in js to encrypt and decrypt the request url. (use any of the encryption algos.) but i'd still s***est using post action
    ............................................................
    Last edited by DougN; 06-09-2015 at 06:10 PM.

  3. #3
    Join Date
    May 2012
    Posts
    1
    i like this forum very much because i have get a lot of required data from this forum.
    now i want to buy some net tools will provide me some tools or guide me about that???
    thanks..

  4. #4
    Join Date
    Jun 2012
    Posts
    1

    # How to encrypt string in c

    You can use following code for encryption

    private string encryptString(string strToEncrypt)
    {
    System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
    byte[] bytes = ue.GetBytes(strToEncrypt);





    thanks
    Last edited by DougN; 06-09-2015 at 06:10 PM.

  5. #5
    Join Date
    Nov 2013
    Posts
    6
    thanks for sharing and i am good with getting those all and by the way have got a fair clue towards the proper implementation of the same method
    www.healthisgod.com/tevida-testosterone-booster

  6. #6
    Join Date
    Sep 2014
    Posts
    16
    You can use following code for encryption

    private string encryptString(string strToEncrypt)
    {
    System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
    byte[] bytes = ue.GetBytes(strToEncrypt);

    // encrypt bytes
    System.Security.Cryptography.MD5Crypto... der md5 = new System.Security.Cryptography.MD5CryptoSe... der();
    byte[] hashBytes = md5.ComputeHash(bytes);

    // Convert the encrypted bytes back to a string (base *6)
    string hashString = "";

    for(int i=0;i
    {
    hashString += Convert.ToString(hashBytes[i],*6).PadLef...
    }

    return hashString.PadLeft(*2,'0');
    }

  7. #7
    Join Date
    Apr 2015
    Posts
    110
    thanks for sharing.. ideas are priceless on this forum, especially in the area of programming.

  8. #8
    Join Date
    Aug 2015
    Posts
    1

    encrypt and decrypt

    using System;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Security.Cryptography;
    namespace WindowsFormsApplication*
    {
    public partial class Form* : Form
    {
    public Form*()
    {
    InitializeComponent();
    }
    private void button*_Click(object sender, EventArgs e)
    {
    //You should not hard code the encryption key here
    string EncryptionKey = "encryptionkey";
    string eStr = passwordEncrypt("yourpassword", EncryptionKey);
    MessageBox.Show(eStr);
    string dStr = passwordDecrypt(eStr, EncryptionKey);
    MessageBox.Show(dStr);
    }
    //Encrypting a string
    public static string passwordEncrypt(string inText, string key)
    {
    byte[] bytesBuff = Encoding.Unicode.GetBytes(inText);
    using (Aes aes = Aes.Create())
    {
    Rfc28*8DeriveBytes crypto = new Rfc28*8DeriveBytes(key, new byte[] { 0x4*, 0x76, 0x6*, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
    aes.Key = crypto.GetBytes(*2);
    aes.IV = crypto.GetBytes(*6);
    using (MemoryStream mStream = new MemoryStream())
    {
    using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
    {
    cStream.Write(bytesBuff, 0, bytesBuff.Length);
    cStream.Close();
    }
    inText = Convert.ToBase64String(mStream.ToArray());
    }
    }
    return inText;
    }
    //Decrypting a string
    public static string passwordDecrypt(string cryptTxt,string key)
    {
    cryptTxt = cryptTxt.Replace(" ", "+");
    byte[] bytesBuff = Convert.FromBase64String(cryptTxt);
    using (Aes aes = Aes.Create())
    {
    Rfc28*8DeriveBytes crypto = new Rfc28*8DeriveBytes(key, new byte[] { 0x4*, 0x76, 0x6*, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
    aes.Key = crypto.GetBytes(*2);
    aes.IV = crypto.GetBytes(*6);
    using (MemoryStream mStream = new MemoryStream())
    {
    using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
    {
    cStream.Write(bytesBuff, 0, bytesBuff.Length);
    cStream.Close();
    }
    cryptTxt = Encoding.Unicode.GetString(mStream.ToArray());
    }
    }
    return cryptTxt;
    }
    }
    }

    Source........http://net-informations.com/q/faq/encrypt.html

    Bang

  9. #9
    Join Date
    May 2015
    Posts
    31
    First of all, your application needs an entrance to let a user in, missing the Main() method is a compile-time error.


    static void Main(string[] args)
    {
    try
    {
    Console.WriteLine("Original String: ");
    string originalString = Console.ReadLine();
    string cryptedString = Encrypt(originalString);
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine("\nEncrypt Result: {0}", cryptedString);
    Console.WriteLine("Decrypt Result: {0}", Decrypt(cryptedString));
    }
    catch (Exception ex)
    {
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("From: {0}.\nDetail: {*}", ex.Source, ex.Message);
    }
    finally
    {
    Console.ReadLine();
    }
    }
    You can also make it as a Windows Forms application if you like, but this is not our key point. Let's go through the code. There are two ********-defined methods using DES, Encrypt and Decrypt, both receive a string and return another string. Let's take a look at the details. BTW, the Console.ReadLine method in the finally block aims to pause the screen.

    Let's see the Encrypt method first:

    /// <span class="code-SummaryComment"><summary></span>
    /// Encrypt a string.
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="originalString">The original string.</param></span>
    /// <span class="code-SummaryComment"><returns>The encrypted string.</returns></span>
    /// <span class="code-SummaryComment"><exception cref="ArgumentNullException">This exception will be </span>
    /// thrown when the original string is null or empty.<span class="code-SummaryComment"></exception></span>
    public static string Encrypt(string originalString)
    {
    if (String.IsNullOrEmpty(originalString))
    {
    throw new ArgumentNullException
    ("The string which needs to be encrypted can not be null.");
    }
    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    MemoryStream memoryStream = new MemoryStream();
    CryptoStream cryptoStream = new CryptoStream(memoryStream,
    cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
    StreamWriter writer = new StreamWriter(cryptoStream);
    writer.Write(originalString);
    writer.Flush();
    cryptoStream.FlushFinalBlock();
    writer.Flush();
    return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
    }
    First, you should verify whether the input string is valid. Then, you need to define an instance of the DESCryptoServiceProvider class and use its methods, and you need a stream to keep the result. In my sample, I used MemoryStream. If you want to store your result into a file directly, you should use FileStream, or something similar. The CryptoStream links data streams to cryptographic transformations, and there is a variable named bytes in its parameter. The first one is used as specified key, and the second one is used as initialization vector, both of them can use the same one. I'll show you how to define it. Then, we also need a StreamWriter to write the result to the stream and keep it. After that, we can return the result to the user.


    static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");
    The ASCIIEncoding class is in the System.Text namespace.

    Okay, let's see the last method Decrypt.

    /// <span class="code-SummaryComment"><summary></span>
    /// Decrypt a crypted string.
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="cryptedString">The crypted string.</param></span>
    /// <span class="code-SummaryComment"><returns>The decrypted string.</returns></span>
    /// <span class="code-SummaryComment"><exception cref="ArgumentNullException">This exception will be thrown </span>
    /// when the crypted string is null or empty.<span class="code-SummaryComment"></exception></span>
    public static string Decrypt(string cryptedString)
    {
    if (String.IsNullOrEmpty(cryptedString))
    {
    throw new ArgumentNullException
    ("The string which needs to be decrypted can not be null.");
    }
    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    MemoryStream memoryStream = new MemoryStream
    (Convert.FromBase64String(cryptedString));
    CryptoStream cryptoStream = new CryptoStream(memoryStream,
    cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
    StreamReader reader = new StreamReader(cryptoStream);
    return reader.ReadToEnd();
    }
    Like Encrypt, we need to verify the input string first, and use DescryptoServiceProvider, MemoryStream, CryptoStream and StreamReader to deal with our job. Obviously, the difference is cryptoProvider.CreateDecryptor in there, and when we want to encrypt a string, we use the cryptoProvider.CreateEncryptor method. Secondly, we use StreamReader when we decrypt an encrypted string.

    Ok, that's all, let your code run!


    http://www.codeproject.com/Articles/**5*8/Encrypt-Decrypt-String-using-DES-in-C
    Last edited by gordo; 08-30-2015 at 08:23 AM. Reason: credit

+ Reply to Thread

Similar Threads

  1. how to encrypt private photos?
    By meiling277869 in forum Security & Encryption
    Replies: 5
    Last Post: 12-14-2009, 04:02 AM
  2. My Encrypt program help
    By Moonbat in forum Security & Encryption
    Replies: 8
    Last Post: 09-23-2006, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts